搜尋
首頁運維Nginx如何用nginx和WordPress建立個人博客

0x01 前置條件

  • 有一個域名,我自己的域名是nomansky.xyz

  • 一台vps或雲端主機,如果是國內的ip需要備案

  • 具有sudo權限或root權限的用戶,這裡我新建一個wordpress用戶來運行程序,並且使用下列命令設定為nologin

    • a. sudo useradd -s /sbin/nologin wordpress

  • ##使用sudo yum install -y epel- release安裝了epel來源

  • 關閉firewalld,我更喜歡用iptables來做安全加固

    • a. sudo systemctl stop firewalld

    • b. sudo systemctl disable firewalld

0x02 安裝nginx

  • #執行sudo yum install nginx安裝nginx

  • #啟動nginx守護程式並設定為開機自啟動

    • a. sudo systemctl start nginx

    • b. sudo systemctl enable nginx

    ##將wordpress用戶加入nginx群組usermod -a -g nginx wordpress,同時設定目錄權限chmod 770 -r /var/lib/nginx/
  • 此時造訪http://nomansky.xyz 即可看到如下頁面,則說明nginx安裝成功了

如何用nginx和WordPress建立個人博客

0x03安裝mariadb

mariadb作為mysql的一個開源的分支,已經成為了centos用來取代mysql的預設的資料庫,所以我這裡也使用mariadb作為資料庫。

    執行sudo yum install mariadb-server -y來安裝mariadb
  • 啟動mariadb並設定為開機自啟動
    • a. sudo systemctl start mariadb
    • b. sudo systemctl enable mariadb
    #執行sudo mysql_secure_installation來加強mariadb。你會看到要求設定資料庫root密碼、移除匿名使用者、限制只能透過localhost登陸資料庫root使用者和移除test資料庫,這裡推薦全部選y(yes),如下圖所示,預設的資料庫root密碼為空

如何用nginx和WordPress建立個人博客除此之外,還要把mariadb監聽的位址改為

127.0.0.1:3306


a. 

vim /etc/my.cnf.d/server.cnf

開啟mariadb的設定檔
b. 在

[mysqld]

下面加上bind=127.0.0.1,如下圖

如何用nginx和WordPress建立個人博客##c.執行
systemctl restart mariadb

重啟資料庫

d.執行
netstat -lntp

可以看到已經監聽為本地回環位址了

# 0x04 創建資料庫

在安裝完mariadb資料庫,並對其進行加固後,我們自然需要新建一個資料庫來存放數據,這裡首先我們用之前設定的root帳號密碼來登陸資料庫mysql -uroot -p

,並執行以下幾個語句

create database wordpress character set utf8mb4 collate utf8mb4_general_ci; # 创建数据库
grant all on wordpress.* to 'wordpress'@'localhost' identified by '你的密码'; # 创建用户
flush privileges;                              # 刷新数据库权限
exit;
#0x05 安裝php

centos的php預設版本為5.4,但w​​ordpress推薦的版本是7.2,所以我們這裡安裝php7.2的版本執行下列指令安裝php和所有需要的php擴充

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,具體怎麼修改查看下面的步驟

#打開

/ etc/php-fpm.d/www.conf

,並修改以下地方

...
user = wordpress
...
group = wordpress
...
listen = /run/php-fpm/www.sock
...
listen.owner = wordpress
listen.group = wordpress
用指令sudo chown -r root:wordpress /var/lib/php

確保目錄的所有群組權限為wordpress

重啟並開啟自啟動php fpm

a. 
sudo systemctl restart php-fpm

#b. sudo systemctl enable php-fpm
0x06 申請免費證書

作為一個技(qiong)術(bi)宅,自然有免費的證書就肯定用免費的。因此我們可以申請免費的let's encrypt證書,這個證書不但免費,而且操作非常簡單,雖然每次只有90天的有效期,但可以透過腳本配置crontab定期更新。

a. 
mkdir -p /etc/nginx/ssl

目錄存放憑證

##b. openssl genrsa 4096 > account. key
進入這個目錄,建立一個rsa 私鑰用於let's encrypt 識別你的身分

c. openssl genrsa 4096 > domain.key
建立域名rsa私鑰

d. openssl req -new -sha256 -key domain.key -out domain.csr有了私钥文件,就可以生成 csr 文件了。生成csr会要求填入一些东西信息,这里common name为你的域名

如何用nginx和WordPress建立個人博客

我们知道,ca 在签发 dv(domain validation)证书时,需要验证域名所有权。传统 ca 的验证方式一般是往 admin@yoursite.com 发验证邮件,而 let's encrypt 是在你的服务器上生成一个随机验证文件,再通过创建 csr 时指定的域名访问,如果可以访问则表明你对这个域名有控制权。所以首先创建用于存放验证文件的目录,例如:
mkdir /home/wordpress/challenges

然后配置一个http服务,以nginx为例:

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 地址。这个验证服务以后更新证书还要用到,要一直保留。

接下来把acme-tiny保存到ssl目录wget https://raw.githubusercontent.com/diafygi/acme-tiny/master/acme_tiny.py

然后指定账户私钥、csr 以及验证目录,执行脚本python acme_tiny.py --account-key ./account.key --csr ./domain.csr --acme-dir /home/wordpress/challenges/ > ./signed.crt,看到如下图所示,则说明生成成功了

如何用nginx和WordPress建立個人博客

最后还要下载let's encrypt 的中间证书,配置https证书时既不要漏掉中间证书,也不要包含根证书。在 nginx 配置中,需要把中间证书和网站证书合在一起:

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

在crontabl中配置定时任务0 0 1 * * /etc/nginx/ssl/renew_cert.sh >/dev/null 2>&1

0x07 下载wordpress并配置nginx

将wordpress下载到/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;
···

然后这里我把处于解耦合的目的,把主配置文件nginx.conf里的server配置块注释掉

新建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

接下来看到wordpress页面成功打开了,就此大功告成啦

如何用nginx和WordPress建立個人博客

以上是如何用nginx和WordPress建立個人博客的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:亿速云。如有侵權,請聯絡admin@php.cn刪除
終極攤牌:nginx vs. apache終極攤牌:nginx vs. apacheApr 18, 2025 am 12:02 AM

NGINX適合處理高並發請求,Apache適合需要復雜配置和功能擴展的場景。 1.NGINX採用事件驅動、非阻塞架構,適用於高並發環境。 2.Apache採用進程或線程模型,提供豐富的模塊生態系統,適合複雜配置需求。

nginx行動:示例和現實應用程序nginx行動:示例和現實應用程序Apr 17, 2025 am 12:18 AM

NGINX可用於提升網站性能、安全性和可擴展性。 1)作為反向代理和負載均衡器,NGINX可優化後端服務和分擔流量。 2)通過事件驅動和異步架構,NGINX高效處理高並發連接。 3)配置文件允許靈活定義規則,如靜態文件服務和負載均衡。 4)優化建議包括啟用Gzip壓縮、使用緩存和調整worker進程。

NGINX單元:支持不同的編程語言NGINX單元:支持不同的編程語言Apr 16, 2025 am 12:15 AM

NGINXUnit支持多種編程語言,通過模塊化設計實現。 1.加載語言模塊:根據配置文件加載相應模塊。 2.應用啟動:調用語言運行時執行應用代碼。 3.請求處理:將請求轉發給應用實例。 4.響應返回:將處理後的響應返回給客戶端。

在Nginx和Apache之間進行選擇:適合您的需求在Nginx和Apache之間進行選擇:適合您的需求Apr 15, 2025 am 12:04 AM

NGINX和Apache各有優劣,適合不同場景。 1.NGINX適合高並發和低資源消耗場景。 2.Apache適合需要復雜配置和豐富模塊的場景。通過比較它們的核心特性、性能差異和最佳實踐,可以幫助你選擇最適合需求的服務器軟件。

nginx怎麼啟動nginx怎麼啟動Apr 14, 2025 pm 01:06 PM

問題:如何啟動 Nginx?答案:安裝 Nginx啟動 Nginx驗證 Nginx 是否已啟動探索其他啟動選項自動啟動 Nginx

怎麼查看nginx是否啟動怎麼查看nginx是否啟動Apr 14, 2025 pm 01:03 PM

確認 Nginx 是否啟動的方法:1. 使用命令行:systemctl status nginx(Linux/Unix)、netstat -ano | findstr 80(Windows);2. 檢查端口 80 是否開放;3. 查看系統日誌中 Nginx 啟動消息;4. 使用第三方工具,如 Nagios、Zabbix、Icinga。

nginx怎麼關閉nginx怎麼關閉Apr 14, 2025 pm 01:00 PM

要關閉 Nginx 服務,請按以下步驟操作:確定安裝類型:Red Hat/CentOS(systemctl status nginx)或 Debian/Ubuntu(service nginx status)停止服務:Red Hat/CentOS(systemctl stop nginx)或 Debian/Ubuntu(service nginx stop)禁用自動啟動(可選):Red Hat/CentOS(systemctl disable nginx)或 Debian/Ubuntu(syst

nginx在windows中怎麼配置nginx在windows中怎麼配置Apr 14, 2025 pm 12:57 PM

如何在 Windows 中配置 Nginx?安裝 Nginx 並創建虛擬主機配置。修改主配置文件並包含虛擬主機配置。啟動或重新加載 Nginx。測試配置並查看網站。選擇性啟用 SSL 並配置 SSL 證書。選擇性設置防火牆允許 80 和 443 端口流量。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前By尊渡假赌尊渡假赌尊渡假赌
威爾R.E.P.O.有交叉遊戲嗎?
1 個月前By尊渡假赌尊渡假赌尊渡假赌

熱工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具