Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

CentOS 6.x 配置基於 Nginx 的 WordPress 運行環境(LNMP 快速部署)

編輯:Linux 教學

一、LNMP 快速安裝(YUM 源)

1. 新增 EPEL 與 Nginx 官方源


rpm -ivh https://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm
rpm -ivh https://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
yum update -y

2. 一鍵安裝核心套件


yum install -y nginx php-fpm php-mysql mysql-server \
php-mbstring php-gd php-pear php-mcrypt php-mhash \
php-eaccelerator php-suhosin php-tidy php-curl

二、服務啟動與開機自啟


chkconfig nginx on
chkconfig mysqld on
chkconfig php-fpm on

service nginx start
service mysqld start
service php-fpm start

三、MySQL 初始化與 WordPress 資料庫建立

1. 設定 root 密碼


mysqladmin -u root password 'StrongRootPass!'

2. 建立 WordPress 資料庫與用戶


mysql -u root -p
CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
GRANT ALL PRIVILEGES ON wordpress.* TO 'wp_user'@'localhost' IDENTIFIED BY 'WpPassw0rd!';
FLUSH PRIVILEGES;
EXIT;

四、PHP-FPM 配置調整(從 Apache 使用者改為 Nginx)


vim /etc/php-fpm.d/www.conf
# 找到並修改:
user = nginx
group = nginx
# 原為 apache

# 可選:調整進程模型
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500

# 重啟
service php-fpm restart

五、Nginx 虛擬主機配置(支援 WordPress 固定連結)


vim /etc/nginx/conf.d/wordpress.conf

server {
    listen 80;
    server_name your-domain.com;
    root /usr/share/nginx/html/wordpress;
    index index.php index.html;

    access_log /var/log/nginx/wordpress_access.log;
    error_log /var/log/nginx/wordpress_error.log;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_param HTTPS off;
    }

    # 安全:禁止存取敏感檔案
    location ~* \.(htaccess|htpasswd|ini|log|sh|sql|bak)$ {
        deny all;
    }
    location ~ /\. {
        deny all;
    }
}

# 部署 WordPress 程式碼
mkdir -p /usr/share/nginx/html/wordpress
cd /usr/local/src
wget https://wordpress.org/latest.tar.gz
tar zxvf latest.tar.gz
cp -r wordpress/* /usr/share/nginx/html/wordpress/
chown -R nginx:nginx /usr/share/nginx/html/wordpress

# 重啟 Nginx
nginx -t && service nginx restart

六、瀏覽器完成安裝

  1. 訪問 https://your-domain.com/
  2. 填寫資料庫資訊(localhost、wordpress、wp_user、WpPassw0rd!)。
  3. 設定站點標題、管理員帳號、密碼、郵箱。
  4. 安裝完成,登入後台。

七、HTTPS 升級(可選)

  • 申請憑證(Let's Encrypt certbot --nginx 或購買商業憑證)。
  • 在 server 區塊加入 listen 443 ssl;、ssl_certificate、ssl_certificate_key。
  • 將 HTTP 重導向 HTTPS:return 301 https://$host$request_uri;。
  • WordPress 後台「設定 → 一般」將站點地址改為 HTTPS。
  • wp-config.php 加入 define('FORCE_SSL_ADMIN', true);。

總結

利用 YUM 官方源快速構建 LNMP,核心調整點:PHP-FPM 用戶改為 nginx、Nginx try_files 支援固定連結、MySQL utf8mb4、權限統一歸屬 nginx。配合 Let's Encrypt 自動續簽,即可獲得安全、高效、易維護的 WordPress 生產環境。

Copyright © Windows教程網 All Rights Reserved