一、LNMP環(huán)境簡(jiǎn)介
LNMP代表Linux、Nginx、MySQL和PHP,是一種常見(jiàn)的Web服務(wù)器架構(gòu)組合。與傳統(tǒng)的LAMP(Linux+Apache+MySQL+PHP)相比,LNMP架構(gòu)具有更高的性能和更低的資源消耗,特別適合中小型網(wǎng)站和應(yīng)用。
二、準(zhǔn)備工作
- 系統(tǒng)要求:
- Linux服務(wù)器(推薦Ubuntu或CentOS)
- 至少1GB內(nèi)存(WordPress運(yùn)行最低要求)
- 10GB以上磁盤空間
- 軟件準(zhǔn)備:
- Nginx 1.18+
- MySQL 5.7+或MariaDB 10.3+
- PHP 7.4+(推薦PHP 8.0)
三、LNMP環(huán)境安裝
1. 安裝Nginx
# Ubuntu/Debian
sudo apt update
sudo apt install nginx -y
# CentOS/RHEL
sudo yum install epel-release -y
sudo yum install nginx -y
啟動(dòng)Nginx并設(shè)置開(kāi)機(jī)自啟:
sudo systemctl start nginx
sudo systemctl enable nginx
2. 安裝MySQL/MariaDB
# Ubuntu/Debian
sudo apt install mysql-server -y
# CentOS/RHEL
sudo yum install mariadb-server -y
啟動(dòng)數(shù)據(jù)庫(kù)服務(wù):
sudo systemctl start mysql # 或mariadb
sudo systemctl enable mysql
運(yùn)行安全配置:
sudo mysql_secure_installation
3. 安裝PHP
# Ubuntu/Debian
sudo apt install php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-xmlrpc php-soap php-intl php-zip -y
# CentOS/RHEL
sudo yum install php-fpm php-mysqlnd php-curl php-gd php-mbstring php-xml php-soap php-intl php-zip -y
啟動(dòng)PHP-FPM:
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
四、配置Nginx支持WordPress
- 創(chuàng)建Nginx配置文件:
sudo nano /etc/nginx/conf.d/wordpress.conf
- 添加以下內(nèi)容(替換your_domain.com為你的域名):
server {
listen 80;
server_name your_domain.com www.your_domain.com;
root /var/www/wordpress;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
}
location ~ /\.ht {
deny all;
}
}
- 測(cè)試并重載Nginx配置:
sudo nginx -t
sudo systemctl reload nginx
五、安裝WordPress
- 創(chuàng)建數(shù)據(jù)庫(kù)和用戶:
mysql -u root -p
CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'wordpressuser'@'localhost' IDENTIFIED BY 'strongpassword';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpressuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
- 下載并安裝WordPress:
cd /tmp
wget https://wordpress.org/latest.tar.gz
tar -xzvf latest.tar.gz
sudo mv wordpress /var/www/
sudo chown -R www-data:www-data /var/www/wordpress
sudo chmod -R 755 /var/www/wordpress
- 配置WordPress:
cd /var/www/wordpress
cp wp-config-sample.php wp-config.php
nano wp-config.php
修改以下數(shù)據(jù)庫(kù)配置:
define('DB_NAME', 'wordpress');
define('DB_USER', 'wordpressuser');
define('DB_PASSWORD', 'strongpassword');
define('DB_HOST', 'localhost');
六、完成安裝
在瀏覽器中訪問(wèn)你的域名,按照WordPress安裝向?qū)瓿砂惭b。
建議安裝后:
- 設(shè)置固定鏈接
- 安裝必要插件(如緩存、安全插件)
- 配置備份方案
七、常見(jiàn)問(wèn)題解決
- 502 Bad Gateway錯(cuò)誤:
- 檢查PHP-FPM是否運(yùn)行:
sudo systemctl status php-fpm
- 確認(rèn)Nginx配置中的PHP socket路徑正確
- 文件權(quán)限問(wèn)題:
sudo chown -R www-data:www-data /var/www/wordpress
sudo find /var/www/wordpress -type d -exec chmod 755 {} \;
sudo find /var/www/wordpress -type f -exec chmod 644 {} \;
- 內(nèi)存不足:
- 編輯
wp-config.php
添加:define('WP_MEMORY_LIMIT', '256M');
通過(guò)以上步驟,你已成功在LNMP環(huán)境下部署了WordPress網(wǎng)站。后續(xù)可以根據(jù)需求進(jìn)行性能優(yōu)化和安全加固。