WordPress是世界上最受歡迎的CMS系統,它是基於php和MySQL技術棧的,而且還有不少插件,可擴展性很是強。正好最近有一臺空閒的ECS,因而來搭建一個玩玩。本教程是基於LEMP技術棧來搭建的,各個版本以下:php
此外,如今全面https已是趨勢了,天然咱們也不能落後,因此還會使用Let's Encrypt來生成免費的SSL證書進行配置css
nomansky.xyz
sudo useradd -s /sbin/nologin wordpress
sudo yum install -y epel-release
安裝了epel源sudo systemctl stop firewalld
sudo systemctl disable firewalld
sudo yum install nginx
安裝nginxsudo systemctl start nginx
sudo systemctl enable nginx
usermod -a -G nginx wordpress
,同時設置目錄權限chmod 770 -R /var/lib/nginx/
Mariadb做爲MySQL的一個開源的分支,已經成爲了CentOS用來替換MySQL的默認的數據庫,因此我這裏也使用Mariadb做爲數據庫。html
sudo yum install mariadb-server -y
來安裝mariadbsudo systemctl start mariadb
sudo systemctl enable mariadb
sudo mysql_secure_installation
來加固Mariadb。你會看到要求設置數據庫root密碼、移除匿名用戶、限制只能經過localhost登錄數據庫root用戶和移除test數據庫,這裏推薦所有選Y
(YES),以下圖所示,默認的數據庫root密碼爲空127.0.0.1:3306
vim /etc/my.cnf.d/server.cnf
打開Mariadb的配置文件[mysqld]
下面加上bind=127.0.0.1
,以下圖所示systemctl restart mariadb
重啓數據庫netstat -lntp
能夠看到已經監聽爲本地迴環地址了在安裝完mariadb數據庫,並對其進行加固後,咱們天然須要新建一個數據庫來存放數據,這裏首先咱們用以前設置的root帳號密碼來登錄數據庫mysql -uroot -p
,並執行如下幾條語句python
CREATE DATABASE wordpress CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; # 建立數據庫 GRANT ALL ON wordpress.* TO 'wordpress'@'localhost' IDENTIFIED BY '你的密碼'; # 建立用戶 FLUSH PRIVILEGES; # 刷新數據庫權限 EXIT;
CentOS的PHP默認版本爲5.4,可是WordPress推薦的版本爲7.2,因此咱們這裏安裝php7.2的版本
執行下列命令安裝php和全部須要的php擴展mysql
sudo yum install yum-utils sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm sudo yum-config-manager --enable remi-php72 sudo yum install php-cli php-fpm php-mysql php-json php-opcache php-mbstring php-xml php-gd php-curl
咱們安裝PHP FPM是由於咱們是用Nginx做爲web server,而Nginx並無自帶這個組件。此外,PHP FPM 默認是以apache用戶運行在9000端口,咱們把這個用戶改成wordpress而且把它從TCP Socket改成Unix Socket,具體怎麼修改查看下面的步驟linux
打開/etc/php-fpm.d/www.conf
,並修改以下地方nginx
... user = wordpress ... group = wordpress ... listen = /run/php-fpm/www.sock ... listen.owner = wordpress listen.group = wordpress
sudo chown -R root:wordpress /var/lib/php
確保目錄的全部組權限爲wordpresssudo systemctl restart php-fpm
sudo systemctl enable php-fpm
mkdir -p /etc/nginx/ssl
目錄存放證書openssl genrsa 4096 > account.key
進入這個目錄,建立一個 RSA 私鑰用於 Let's Encrypt 識別你的身份openssl genrsa 4096 > domain.key
建立域名RSA私鑰openssl req -new -sha256 -key domain.key -out domain.csr
有了私鑰文件,就能夠生成 CSR 文件了。生成CSR會要求填入一些東西信息,這裏Common Name爲你的域名咱們知道,CA 在簽發 DV(Domain Validation)證書時,須要驗證域名全部權。傳統 CA 的驗證方式通常是往 admin@yoursite.com 發驗證郵件,而 Let's Encrypt 是在你的服務器上生成一個隨機驗證文件,再經過建立 CSR 時指定的域名訪問,若是能夠訪問則代表你對這個域名有控制權。因此首先建立用於存放驗證文件的目錄,例如:mkdir /home/wordpress/challenges
而後配置一個HTTP服務,以Nginx爲例:git
server { server_name www.nomansky.xyz nomansky.xyz; location ^~ /.well-known/acme-challenge/ { alias /home/wordpress/challenges/; try_files $uri =404; } location / { rewrite ^/(.*)$ https://nomansky.xyz/$1 permanent; } }
以上配置表示查找 /home/wordpress/challenges/ 目錄下的文件,若是找不到就重定向到 HTTPS 地址。這個驗證服務之後更新證書還要用到,要一直保留。github
wget https://raw.githubusercontent.com/diafygi/acme-tiny/master/acme_tiny.py
python acme_tiny.py --account-key ./account.key --csr ./domain.csr --acme-dir /home/wordpress/challenges/ > ./signed.crt
,看到以下圖所示,則說明生成成功了最後還要下載Let's Encrypt 的中間證書,配置HTTPS證書時既不要漏掉中間證書,也不要包含根證書。在 Nginx 配置中,須要把中間證書和網站證書合在一塊兒:web
wget -O - https://letsencrypt.org/certs/lets-encrypt-x3-cross-signed.pem > intermediate.pem cat signed.crt intermediate.pem > chained.pem
爲了後續能順利啓用OCSP Stapling,咱們再把根證書和中間證書合在一塊兒(此步也可省略)
wget -O - https://letsencrypt.org/certs/isrgrootx1.pem > root.pem cat intermediate.pem root.pem > full_chained.pem
Let's Encrypt簽發的證書只有90天有效期,推薦使用腳本按期更新。建立一個renew_cert.sh
並經過chmod a+x renew_cert.sh
賦予執行權限。文件內容以下:
#!/bin/bash cd /etc/nginx/ssl/ python acme_tiny.py --account-key account.key --csr domain.csr --acme-dir /home/wordpress/challenges/ > signed.crt || exit wget -O - https://letsencrypt.org/certs/lets-encrypt-x3-cross-signed.pem > intermediate.pem cat signed.crt intermediate.pem > chained.pem systemctl restart nginx
0 0 1 * * /etc/nginx/ssl/renew_cert.sh >/dev/null 2>&1
/home/wordpress/
目錄下wget https://wordpress.org/latest.tar.gz
tar zxvf latest.tar.gz
解壓WordPress文件chown -R wordpress:wordpress wordpress
將wordpress目錄的全部者改成wordpress用戶接着,打開vim /etc/nginx/nginx.conf
將nginx的運行角色改成wordpress
··· user wordpress; worker_processes auto; ···
新建sudo mkdir /etc/nginx/snippets
目錄並vim letsencrypt.conf
來將如下配置粘貼到裏面
location ^~ /.well-known/acme-challenge/ { alias /home/wordpress/challenges/; try_files $uri =404; }
接下來新建vim /etc/nginx/conf.d/wordpress.conf
配置文件,修改爲以下配置
# Redirect HTTP -> HTTPS server { listen 80; server_name www.nomansky.xyz nomansky.xyz; include snippets/letsencrypt.conf; return 301 https://nomansky.xyz$request_uri; } # Redirect WWW -> NON WWW server { listen 443 ssl http2; server_name www.nomansky.xyz; ssl_certificate /etc/nginx/ssl/chained.pem; ssl_certificate_key /etc/nginx/ssl/domain.key; return 301 https://nomansky.com$request_uri; } server { listen 443 ssl http2; server_name nomansky.com; root /home/wordpress/wordpress; index index.php; # SSL parameters ssl_certificate /etc/nginx/ssl/chained.pem; ssl_certificate_key /etc/nginx/ssl/domain.key; # log files access_log /home/wordpress/log/nomansky.xyz.access.log; error_log /home/wordpress/log/nomansky.xyz.error.log; location = /favicon.ico { log_not_found off; access_log off; } location = /robots.txt { allow all; log_not_found off; access_log off; } location / { try_files $uri $uri/ /index.php?$args; } location ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/run/php-fpm/www.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ { expires max; log_not_found off; }
mkdir -p /home/wordpress/log
,並設置權限chown -R wordpress:wordpress /home/wordpress/log
nginx -t
查看是不是否語法檢查正常,如正常則nginx -s reload
重載nginx
How to install WordPress with Nginx on CentOS 7
免費好用的HTTPS證書