Debian+Nginx+MariaDB+PHP+WordPress詳細完整雲端建站教程

上大學前一直籌劃自己建站當站長,然而在早就買好的雲服務器,一直只掛了一個靜態網站,也沒多少激情去維護,自己開發的動態網站,emmm,我自己都看不下去的,有點先去,那麼我就搬輪子吧。就選擇WordPress,有強大的社區支持。
開始吧

準備工作

服務器

無論是企業還是個人開發者,我都建議先使用雲服務器,國內建議阿里雲和騰訊雲,國外使用AWS是首選。政府網站建議使用物理服務器。

LNMP系統
我使用的是阿里雲的雲服務器, 1C2G物理配置,1Mbps的網絡和40G的高效硬盤。
系統採用Debian9.9
數據庫使用MariaDB10.4.12
webserver使用Nginx1.17.10
編程語言使用PHP7.4.5

阿里雲服務器購買鏈接:https://www.aliyun.com/product/ecs?source=5176.11533457&userCode=kuoc2mgi&type=copy

下面是優惠,需要的可以領取。
新用戶優惠每年102元/年起:點擊我
新用戶2000元優惠券:點擊我

域名

域名就是去各大域名廠家購買域名就行了,如果只是爲了玩玩,建議購買.top域名,後期續費比較便宜,性價比比較高,其他的看個人愛好、網站用途、經濟承受能力。

我的域名是在阿里雲的域名市場購買的,國內的網站要使用是需要備案的,個人備案的話準備材料也也比較簡單,安裝官網操作就行,基本就是填寫資料,幕布拍照,然後就是等待管局審覈,我首次申請就12天就完成了,阿里云爲了補償我,給了我12天的雲服務器使用,然後第二個域名申請就只是花了5天就弄好了。

一切準備好後,申請一個免費的SSL,這個是很有必要的,申請過程大概一個小時左右就可以下發證書,然後下載證書就可以部署了。

Nginx部署SSL的教程官網也有,我是照官網的教程然後稍微修改修改就可以了。

阿里雲域名官網:https://wanwang.aliyun.com/domain/searchresult/?source=5176.11533457&userCode=kuoc2mgi&type=copy

若是不懂的可以在下發評論或者直接郵件我

MariaDB

安裝MariaDB

將MariaDB添加到系統源

sudo apt-get install software-properties-common dirmngr
sudo apt-key adv --fetch-keys 'https://mariadb.org/mariadb_release_signing_key.asc'
sudo add-apt-repository 'deb [arch=amd64,i386,ppc64el] http://mirrors.coreix.net/mariadb/repo/10.4/debian stretch main'

使用apt安裝MariaDB

sudo apt-get update
sudo apt-get install mariadb-server

配置MariaDB

安裝頁面:https://downloads.mariadb.org/mariadb/repositories/#distro=Debian&distro_release=stretch–stretch&mirror=coreix&version=10.4

輸入如下命令配置root密碼:

mysql_secure_installation

配置WordPress數據庫

mysql -uroot -hlocalhost -ppassword

創建一個數據庫用戶:

CREATE USER 'wordpress'@'localhost' IDENTIFIED BY 'password';

創建一個數據庫

create database wordpress default charset utf8 collate utf8_general_ci;

授予權限

grant all privileges on wordpress.* to 'wordpress'@'localhost' identified by 'password';

刷新權限

flush privileges;

Nginx

安裝頁
http://nginx.org/en/linux_packages.html#Debian

安裝組件

sudo apt install curl gnupg2 ca-certificates lsb-release

設置存儲庫

echo "deb http://nginx.org/packages/mainline/debian `lsb_release -cs` nginx" \
    | sudo tee /etc/apt/sources.list.d/nginx.list

導入簽名密鑰

curl -fsSL https://nginx.org/keys/nginx_signing.key | sudo apt-key add -

驗證密鑰

sudo apt-key fingerprint ABF5BD827BD9BF62

輸出:

pub   rsa2048 2011-08-19 [SC] [expires: 2024-06-14]
      573B FD6B 3D8F BC64 1079  A6AB ABF5 BD82 7BD9 BF62
uid           [ unknown] nginx signing key <[email protected]>

安裝Nginx

sudo apt update
sudo apt install nginx

PHP

apt-get -y install apt-transport-https lsb-release ca-certificates curl
wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list
apt-get update
sudo apt install php7.4

安裝完後會顯示運行 Apache 服務失敗,這是正常情況,因爲我們先安裝並運行了Nginx,Nginx佔用了80端口,導致 Apache 服務運行失敗。

安裝必要包

apt install php7.4-fpm php7.4-cgi php7.4-curl php7.4-gd php7.4-xml php7.4-xmlrpc php7.4-mysql php7.4-bz2

檢測

php -v

配置Nginx和PHP

nginx文件分析

# nginx運行的用戶名
user nginx;
# nginx啓動進程,通常設置成和cpu的數量相等,這裏爲自動
worker_processes auto;

# errorlog文件位置
error_log /var/log/nginx/error.log;
# pid文件地址,記錄了nginx的pid,方便進程管理
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
# 用來加載其他動態模塊的配置
include /usr/share/nginx/modules/*.conf;

# 工作模式和連接數上限
events {
    # 每個worker_processes的最大併發鏈接數
    # 併發總數:worker_processes*worker_connections
    worker_connections 1024;
}

# 與提供http服務相關的一些配置參數類似的還有mail
http {
    # 設置日誌的格式
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    # access_log記錄訪問的用戶、頁面、瀏覽器、ip和其他的訪問信息
    access_log  /var/log/nginx/access.log  main;

    # 這部分下面會單獨解釋
    # 設置nginx是否使用sendfile函數輸出文件
    sendfile            on;
    # 數據包最大時發包(使用Nagle算法)
    tcp_nopush          on;
    # 立刻發送數據包(禁用Nagle算法)
    tcp_nodelay         on;
    # 鏈接超時時間
    keepalive_timeout   65;
    # 這個我也不清楚...
    types_hash_max_size 2048;

    # 引入文件擴展名與文件類型映射表
    include             /etc/nginx/mime.types;
    # 默認文件類型
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    # http服務上支持若干虛擬主機。
    # 每個虛擬主機一個對應的server配置項
    # 配置項裏面包含該虛擬主機相關的配置。
    server {
        # 端口
        listen       80 default_server;
        listen       [::]:80 default_server;
        # 訪問的域名
        server_name  _;
        # 默認網站根目錄(www目錄)
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.

        include /etc/nginx/default.d/*.conf;

        # 默認請求
        location / {
        }

        # 錯誤頁(404)
        error_page 404 /404.html;
            location = /40x.html {
        }

        # 錯誤頁(50X)
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}

/etc/php/7.4/cgi/php.ini設置:

cgi.fix_pathinfo=1

/etc/php/7.4/fpm/php.ini 設置

cgi.fix_pathinfo=0 

/etc/nginx/nginx.conf配置如下:

user  www-data; # 到/etc/php/7.4/fpm/pool.d/www.conf文件可以找到
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

cat /etc/nginx/conf.d/default.conf

server {
    listen       80;
    server_name  localhost;
    root /var/www/wordpress;

    #charset koi8-r;
    access_log  /var/log/nginx/host.access.log  main;

    location / {
        #root   /usr/share/nginx/html;
        index  index.html index.htm index.php;
    }

    error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
    #    root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        #root           html;
        fastcgi_pass   unix:/run/php/php7.4-fpm.sock;
        fastcgi_index  index.php;
        #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
	fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
        deny  all;
    }
}

別說我的博文是文字不夠代碼來湊,那還不是怕有的同學看不過來,直接貼代碼好複製啊。在這裏插入代碼片

WordPress

下載WordPress

wget https://wordpress.org/latest.tar.gz

解壓安裝後修改:

cp wp-config-sample.php wp-config.php

然後編輯wp-config.php文件

在相應地方寫入數據庫,數據庫用戶名以及密碼。

/** The name of the database for WordPress */
define( 'DB_NAME', 'wordpress' );

/** MySQL database username */
define( 'DB_USER', 'wordpress' );

/** MySQL database password */
define( 'DB_PASSWORD', 'wp^2020.' );

/** MySQL hostname */
define( 'DB_HOST', 'localhost' );

/** Database Charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8' );

/** The Database Collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', '' );

然後到瀏覽器輸入IP,設置好用戶名密碼郵箱,接下來就可以愉快地造作了。
在這裏插入圖片描述

有不懂的地方歡迎留言。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章