一、環(huán)境準(zhǔn)備
在開始構(gòu)建LNMP( Linux + Nginx + MySQL + PHP )環(huán)境并安裝WordPress之前,需要確保擁有以下條件:
- 一臺Linux服務(wù)器(推薦使用Ubuntu 20.04/CentOS 8)
- 服務(wù)器root權(quán)限或sudo權(quán)限賬戶
- 穩(wěn)定的網(wǎng)絡(luò)連接
- 已解析到服務(wù)器IP的域名(可選)
二、安裝LNMP環(huán)境
1. 更新系統(tǒng)軟件包
sudo apt update && sudo apt upgrade -y # Ubuntu/Debian
# 或
sudo yum update -y # CentOS/RHEL
2. 安裝Nginx
sudo apt install nginx -y # Ubuntu/Debian
# 或
sudo yum install nginx -y # CentOS/RHEL
啟動Nginx并設(shè)置開機(jī)自啟:
sudo systemctl start nginx
sudo systemctl enable nginx
3. 安裝MySQL
sudo apt install mysql-server -y # Ubuntu/Debian
# 或
sudo yum install mysql-server -y # CentOS/RHEL
啟動MySQL并設(shè)置開機(jī)自啟:
sudo systemctl start mysql
sudo systemctl enable mysql
運行安全配置腳本:
sudo mysql_secure_installation
4. 安裝PHP及相關(guān)擴(kuò)展
sudo apt install php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-xmlrpc php-soap php-intl php-zip -y # Ubuntu/Debian
# 或
sudo yum install php-fpm php-mysqlnd php-curl php-gd php-mbstring php-xml php-soap php-intl php-zip -y # CentOS/RHEL
啟動PHP-FPM并設(shè)置開機(jī)自啟:
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
三、配置Nginx支持PHP
- 編輯Nginx默認(rèn)配置文件:
sudo nano /etc/nginx/sites-available/default # Ubuntu/Debian
# 或
sudo nano /etc/nginx/conf.d/default.conf # CentOS/RHEL
- 在server塊中添加以下正文:
location ~ \.php$ {
include snippets/fastcgi-php.conf; # Ubuntu/Debian
# 或 include fastcgi_params; # CentOS/RHEL
fastcgi_pass unix:/var/run/php/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
- 測試Nginx配置并重啟:
sudo nginx -t
sudo systemctl restart nginx
四、創(chuàng)建WordPress數(shù)據(jù)庫
- 登錄MySQL:
sudo mysql -u root -p
- 創(chuàng)建數(shù)據(jù)庫和用戶:
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
- 下載最新版WordPress:
cd /tmp
curl -O https://wordpress.org/latest.tar.gz
- 解壓文件并移動到網(wǎng)站目錄:
tar xzvf latest.tar.gz
sudo mv wordpress /var/www/html/
- 設(shè)置權(quán)限:
sudo chown -R www-data:www-data /var/www/html/wordpress # Ubuntu/Debian
# 或 sudo chown -R nginx:nginx /var/www/html/wordpress # CentOS/RHEL
sudo find /var/www/html/wordpress/ -type d -exec chmod 750 {} \;
sudo find /var/www/html/wordpress/ -type f -exec chmod 640 {} \;
六、配置WordPress
- 復(fù)制示例配置文件:
cd /var/www/html/wordpress
sudo cp wp-config-sample.php wp-config.php
- 編輯配置文件:
sudo nano wp-config.php
更新數(shù)據(jù)庫連接信息:
define( 'DB_NAME', 'wordpress' );
define( 'DB_USER', 'wordpressuser' );
define( 'DB_PASSWORD', 'your_strong_password' );
define( 'DB_HOST', 'localhost' );
七、完成WordPress安裝
- 在瀏覽器中訪問您的服務(wù)器IP或域名:
http://your_server_ip_or_domain/wordpress
- 按照屏幕上的提示完成安裝:
- 選擇語言
- 輸入站點標(biāo)題、管理員用戶名、密碼和電子郵件
- 點擊”安裝WordPress”
- 登錄后臺管理界面:
http://your_server_ip_or_domain/wordpress/wp-admin
八、優(yōu)化和安全設(shè)置(可選)
- 設(shè)置Nginx虛擬主機(jī)
- 配置SSL證書(推薦使用Let’s Encrypt)
- 安裝WordPress安全插件
- 設(shè)置定期備份
- 配置緩存插件提高性能
通過以上步驟,您已成功在LNMP環(huán)境下搭建了WordPress網(wǎng)站。接下來可以根據(jù)需要安裝主題、插件,并開始發(fā)布內(nèi)容。