WordPress作為全球最受歡迎的內(nèi)容管理系統(tǒng)(CMS),在Linux服務(wù)器上部署能夠獲得最佳性能和穩(wěn)定性。本文將詳細(xì)介紹在Linux環(huán)境下配置WordPress的全過程。
一、準(zhǔn)備工作
- 服務(wù)器要求:
- Linux操作系統(tǒng)(推薦Ubuntu或CentOS)
- Apache/Nginx Web服務(wù)器
- PHP 7.4或更高版本
- MySQL 5.7/MariaDB 10.3或更高版本
- 安裝LAMP/LEMP環(huán)境:
# Ubuntu/Debian安裝LAMP
sudo apt update
sudo apt install apache2 mysql-server php libapache2-mod-php php-mysql
# CentOS安裝LEMP
sudo yum install nginx mariadb-server php php-fpm php-mysqlnd
二、數(shù)據(jù)庫配置
- 登錄MySQL并創(chuàng)建WordPress數(shù)據(jù)庫:
CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'strongpassword';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
三、安裝WordPress
- 下載并解壓最新版WordPress:
cd /var/www/html
sudo wget https://wordpress.org/latest.tar.gz
sudo tar -xzvf latest.tar.gz
sudo chown -R www-data:www-data wordpress
sudo chmod -R 755 wordpress
- 配置WordPress:
cd wordpress
cp wp-config-sample.php wp-config.php
nano wp-config.php
修改數(shù)據(jù)庫連接信息:
define('DB_NAME', 'wordpress');
define('DB_USER', 'wpuser');
define('DB_PASSWORD', 'strongpassword');
define('DB_HOST', 'localhost');
四、Web服務(wù)器配置
Apache配置:
sudo nano /etc/apache2/sites-available/wordpress.conf
添加以下正文:
<VirtualHost *:80>
ServerAdmin admin@example.com
DocumentRoot /var/www/html/wordpress
ServerName yourdomain.com
<Directory /var/www/html/wordpress/>
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
啟用配置:
sudo a2ensite wordpress.conf
sudo a2enmod rewrite
sudo systemctl restart apache2
Nginx配置:
sudo nano /etc/nginx/conf.d/wordpress.conf
添加以下內(nèi)容:
server {
listen 80;
server_name 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/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
重啟Nginx:
sudo systemctl restart nginx
五、完成安裝
- 在瀏覽器訪問您的域名,按照WordPress安裝向?qū)瓿勺詈笈渲?/li>
- 建議安裝后立即:
- 更新永久鏈接結(jié)構(gòu)
- 安裝必要插件(如緩存、安全插件)
- 設(shè)置定期備份
六、安全加固建議
- 限制wp-admin目錄訪問:
sudo nano /var/www/html/wordpress/wp-admin/.htaccess
添加:
AuthType Basic
AuthName "Restricted Area"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
- 禁用文件編輯: 在wp-config.php中添加:
define('DISALLOW_FILE_EDIT', true);
- 定期更新系統(tǒng)和WordPress核心:
sudo apt update && sudo apt upgrade
通過以上步驟,您已在Linux系統(tǒng)上成功部署了WordPress網(wǎng)站。根據(jù)實(shí)際需求,您還可以進(jìn)一步優(yōu)化服務(wù)器配置,如啟用OPcache、配置CDN等,以提升網(wǎng)站性能。