一、LNMP環(huán)境簡(jiǎn)介
LNMP是指Linux+Nginx+MySQL+PHP這一經(jīng)典網(wǎng)站服務(wù)器架構(gòu)組合。與傳統(tǒng)的LAMP(Linux+Apache+MySQL+PHP)相比,LNMP架構(gòu)具有更高的并發(fā)處理能力和更低的內(nèi)存消耗,特別適合中小型網(wǎng)站和博客系統(tǒng)。WordPress作為全球最流行的內(nèi)容管理系統(tǒng)(CMS),在LNMP環(huán)境下運(yùn)行可以獲得優(yōu)異的性能表現(xiàn)。
二、準(zhǔn)備工作
在開始構(gòu)建LNMP環(huán)境前,需要做好以下準(zhǔn)備:
- 服務(wù)器選擇:推薦使用CentOS 7/8或Ubuntu 18.04/20.04等主流Linux發(fā)行版
- 系統(tǒng)更新:執(zhí)行
sudo apt update && sudo apt upgrade -y
(Ubuntu)或sudo yum update -y
(CentOS) - 創(chuàng)建非root用戶:建議使用普通用戶操作,通過(guò)sudo獲取權(quán)限
三、安裝Nginx服務(wù)器
- 安裝Nginx:
# Ubuntu/Debian
sudo apt install nginx -y
# CentOS/RHEL
sudo yum install epel-release -y
sudo yum install nginx -y
- 啟動(dòng)并設(shè)置開機(jī)自啟:
sudo systemctl start nginx
sudo systemctl enable nginx
- 驗(yàn)證安裝:瀏覽器訪問(wèn)服務(wù)器IP,應(yīng)看到Nginx歡迎頁(yè)面
四、安裝MySQL數(shù)據(jù)庫(kù)
- 安裝MySQL服務(wù)器:
# Ubuntu/Debian
sudo apt install mysql-server -y
# CentOS/RHEL
sudo yum install mysql-server -y
- 安全配置MySQL:
sudo mysql_secure_installation
按照提示設(shè)置root密碼、移除匿名用戶、禁止root遠(yuǎn)程登錄等
- 啟動(dòng)并設(shè)置開機(jī)自啟:
sudo systemctl start mysqld
sudo systemctl enable mysqld
五、安裝PHP及必要擴(kuò)展
- 安裝PHP和常用擴(kuò)展:
# 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
- 配置PHP-FPM:
編輯
/etc/php-fpm.d/www.conf
(CentOS)或/etc/php/7.x/fpm/pool.d/www.conf
(Ubuntu):
user = nginx
group = nginx
listen = /run/php-fpm/www.sock
listen.owner = nginx
listen.group = nginx
- 啟動(dòng)PHP-FPM:
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
六、配置Nginx支持PHP
- 創(chuàng)建網(wǎng)站配置文件
/etc/nginx/conf.d/wordpress.conf
:
server {
listen 80;
server_name 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-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
}
- 測(cè)試配置并重啟Nginx:
sudo nginx -t
sudo systemctl restart nginx
七、安裝并配置WordPress
- 創(chuàng)建網(wǎng)站目錄并設(shè)置權(quán)限:
sudo mkdir -p /var/www/wordpress
sudo chown -R nginx:nginx /var/www/wordpress
sudo chmod -R 755 /var/www/wordpress
- 下載最新版WordPress:
cd /tmp
wget https://wordpress.org/latest.tar.gz
tar -xzvf latest.tar.gz
sudo cp -r wordpress/* /var/www/wordpress/
- 創(chuàng)建WordPress數(shù)據(jù)庫(kù):
mysql -u root -p
在MySQL命令行中執(zhí)行:
CREATE DATABASE wordpress;
CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'your_strong_password';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
- 配置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', 'wpuser');
define('DB_PASSWORD', 'your_strong_password');
define('DB_HOST', 'localhost');
- 設(shè)置文件權(quán)限:
sudo chown -R nginx:nginx /var/www/wordpress
sudo find /var/www/wordpress -type d -exec chmod 750 {} \;
sudo find /var/www/wordpress -type f -exec chmod 640 {} \;
八、完成WordPress安裝
- 瀏覽器訪問(wèn)服務(wù)器IP或域名,按照WordPress安裝向?qū)瓿砂惭b
- 設(shè)置網(wǎng)站標(biāo)題、管理員賬號(hào)等信息
- 登錄WordPress后臺(tái)(
your_domain.com/wp-admin
)進(jìn)行進(jìn)一步配置
九、安全加固建議
- 限制PHP函數(shù):編輯
php.ini
,添加:
disable_functions = exec,passthru,shell_exec,system,proc_open,popen
- 安裝防火墻:
# Ubuntu
sudo apt install ufw
sudo ufw allow 'Nginx Full'
sudo ufw enable
# CentOS
sudo yum install firewalld
sudo systemctl start firewalld
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
- 定期更新系統(tǒng)組件:
# Ubuntu
sudo apt update && sudo apt upgrade -y
# CentOS
sudo yum update -y
通過(guò)以上步驟,您已成功搭建了LNMP環(huán)境并部署了WordPress網(wǎng)站。后續(xù)可以根據(jù)需要安裝主題、插件,并進(jìn)一步優(yōu)化服務(wù)器性能和安全設(shè)置。