在 Ubuntu 上部署 WordPress 的标准流程
本指南提供在 Ubuntu 22.04/24.04 上使用 LAMP(Apache + MySQL + PHP) 部署 WordPress 的完整步骤,包含防火墙放行、目录权限、数据库创建与安装向导。若你偏好 Nginx + PHP‑FPM,文末也给出可选方案与关键配置片段。
一 准备与系统更新
sudo apt update && sudo apt upgrade -ysudo ufw allow 'Apache Full' && sudo ufw enable二 安装与配置 LAMP 组件
sudo apt install apache2 -ysudo systemctl start apache2 && sudo systemctl enable apache2sudo apt install mysql-server -ysudo systemctl start mysql && sudo systemctl enable mysqlsudo mysql_secure_installation(设置 root 密码、移除匿名用户、禁止远程 root 登录等)sudo apt install php libapache2-mod-php php-mysql php-curl php-gd php-xml php-mbstring php-xmlrpc php-zip php-soap php-intl -yindex.php 优先):编辑 /etc/apache2/mods-enabled/dir.conf,将 index.php 移到最前,保存后重启 Apache:sudo systemctl restart apache2sudo apt install software-properties-common -y && sudo add-apt-repository ppa:ondrej/php -y && sudo apt updatesudo apt install php8.3 php8.3-fpm php8.3-mysql php8.3-xml php8.3-curl php8.3-mbstring php8.3-zip php8.3-gd -ysudo systemctl start php8.3-fpm && sudo systemctl enable php8.3-fpm三 创建数据库与用户
sudo mysql -u root -pCREATE DATABASE wordpress CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'StrongPassword!';GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';FLUSH PRIVILEGES;EXIT;四 部署 WordPress 文件与权限
cd /tmp && curl -LO https://wordpress.org/latest.tar.gz && tar xzvf latest.tar.gzsudo cp -a /tmp/wordpress/. /var/www/html/wordpresssudo cp /var/www/html/wordpress/wp-config-sample.php /var/www/html/wordpress/wp-config.phpwp-config.php):
define('DB_NAME', 'wordpress');define('DB_USER', 'wpuser');define('DB_PASSWORD', 'StrongPassword!');define('DB_HOST', 'localhost');sudo chown -R www-data:www-data /var/www/html/wordpresssudo find /var/www/html/wordpress -type d -exec chmod 755 {} \;sudo find /var/www/html/wordpress -type f -exec chmod 644 {} \;sudo nano /etc/apache2/sites-available/wordpress.conf<VirtualHost *:80>
ServerAdmin webmaster@example.com
DocumentRoot /var/www/html/wordpress
ServerName your-domain.com
ServerAlias www.your-domain.com
<Directory /var/www/html/wordpress/>
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/wordpress.error.log
CustomLog ${APACHE_LOG_DIR}/wordpress.access.log combined
</VirtualHost>
sudo a2ensite wordpress.conf && sudo a2enmod rewrite && sudo systemctl reload apache2五 安全加固与常见问题
sudo rm /var/www/html/info.php(如曾创建)附 Nginx + PHP‑FPM 方案简要
sudo apt install nginx -ysudo apt install php8.3-fpm php8.3-mysql php8.3-curl php8.3-gd php8.3-mbstring php8.3-xml php8.3-zip -yserver {
listen 80;
server_name your-domain.com;
root /var/www/html/wordpress;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
location ~* \.(gif|jpg|jpeg|png|webp)$ { expires 86400; }
location ~* \.(htm|html|css|js)$ { expires 2592000; }
}
sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/ && sudo rm -f /etc/nginx/sites-enabled/default && sudo nginx -t && sudo systemctl reload nginx