一、LNMP環(huán)境簡(jiǎn)介
LNMP是指Linux+Nginx+MySQL+PHP組成的網(wǎng)站服務(wù)器架構(gòu),是目前搭建WordPress等PHP程序的常用環(huán)境組合。相較于傳統(tǒng)的LAMP(Apache代替Nginx),LNMP架構(gòu)具有資源占用少、并發(fā)能力強(qiáng)、配置靈活等優(yōu)勢(shì)。
二、準(zhǔn)備工作
在開(kāi)始搭建前,您需要準(zhǔn)備:
- 一臺(tái)Linux服務(wù)器(推薦CentOS 7/8或Ubuntu 18.04/20.04)
- 服務(wù)器root權(quán)限或sudo權(quán)限賬戶
- 已解析到服務(wù)器IP的域名(可選)
- 基本的Linux命令行操作知識(shí)
三、LNMP環(huán)境安裝
1. 安裝Nginx
對(duì)于CentOS系統(tǒng):
yum install epel-release -y
yum install nginx -y
systemctl start nginx
systemctl enable nginx
對(duì)于Ubuntu系統(tǒng):
apt update
apt install nginx -y
systemctl start nginx
systemctl enable nginx
2. 安裝MySQL/MariaDB
CentOS安裝MariaDB:
yum install mariadb-server mariadb -y
systemctl start mariadb
systemctl enable mariadb
mysql_secure_installation
Ubuntu安裝MySQL:
apt install mysql-server -y
systemctl start mysql
systemctl enable mysql
mysql_secure_installation
3. 安裝PHP
CentOS 7安裝PHP 7.4:
yum install -y http://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum install -y yum-utils
yum-config-manager --enable remi-php74
yum install php php-fpm php-mysqlnd php-gd php-mbstring php-xml -y
systemctl start php-fpm
systemctl enable php-fpm
Ubuntu安裝PHP 7.4:
apt install php-fpm php-mysql php-gd php-mbstring php-xml -y
systemctl start php7.4-fpm
systemctl enable php7.4-fpm
四、配置WordPress數(shù)據(jù)庫(kù)
登錄MySQL創(chuàng)建數(shù)據(jù)庫(kù)和用戶:
CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
CREATE USER 'wordpressuser'@'localhost' IDENTIFIED BY 'your_strong_password';
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
mv wordpress /var/www/html/
chown -R nginx:nginx /var/www/html/wordpress
chmod -R 755 /var/www/html/wordpress
六、配置Nginx虛擬主機(jī)
創(chuàng)建配置文件/etc/nginx/conf.d/wordpress.conf
:
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
root /var/www/html/wordpress;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
}
測(cè)試并重載Nginx配置:
nginx -t
systemctl reload nginx
七、完成WordPress安裝
- 在瀏覽器訪問(wèn)您的域名或服務(wù)器IP
- 按照向?qū)нx擇語(yǔ)言
- 填寫(xiě)之前創(chuàng)建的數(shù)據(jù)庫(kù)信息
- 設(shè)置網(wǎng)站標(biāo)題、管理員賬戶等信息
- 完成安裝并登錄后臺(tái)
八、安全優(yōu)化建議
- 配置SSL證書(shū)(可使用Let’s Encrypt免費(fèi)證書(shū))
- 限制WordPress后臺(tái)登錄嘗試次數(shù)
- 定期備份網(wǎng)站和數(shù)據(jù)庫(kù)
- 保持WordPress核心、主題和插件更新
- 使用安全插件如Wordfence
九、常見(jiàn)問(wèn)題解決
- 502 Bad Gateway錯(cuò)誤:檢查PHP-FPM是否運(yùn)行,socket路徑是否正確
- 文件上傳權(quán)限問(wèn)題:確保
wp-content
目錄有正確權(quán)限 - 內(nèi)存不足:可編輯
wp-config.php
增加define('WP_MEMORY_LIMIT', '256M');
通過(guò)以上步驟,您已成功在LNMP環(huán)境下搭建了WordPress網(wǎng)站。后續(xù)可根據(jù)需求進(jìn)行主題安裝、插件配置等個(gè)性化設(shè)置。