WordPress作为全球最流行的内容管理系统(CMS),在LNMP(Linux+Nginx+MySQL+PHP)环境下运行能够发挥出色的性能表现。本文将详细介绍如何在LNMP环境中部署WordPress应用。
一、LNMP环境搭建
- Linux系统准备
- 推荐使用Ubuntu或CentOS等主流Linux发行版
- 确保系统已更新至最新版本:
sudo apt update && sudo apt upgrade -y
(Ubuntu)或sudo yum update -y
(CentOS)
- Nginx安装与配置
# Ubuntu
sudo apt install nginx -y
# CentOS
sudo yum install nginx -y
启动Nginx服务:sudo systemctl start nginx && sudo systemctl enable nginx
- MySQL/MariaDB安装
# Ubuntu
sudo apt install mysql-server -y
# CentOS
sudo yum install mariadb-server -y
运行安全安装脚本:sudo mysql_secure_installation
- PHP安装
# Ubuntu
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
sudo yum install php-fpm php-mysqlnd php-curl php-gd php-mbstring php-xml php-soap php-intl php-zip -y
二、WordPress安装配置
- 创建数据库
mysql -u root -p
CREATE DATABASE wordpress;
CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
- 下载并配置WordPress
cd /var/www
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
- 配置Nginx虚拟主机
在
/etc/nginx/sites-available/wordpress
创建配置文件:
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 snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
启用配置并重启Nginx:
sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
三、WordPress初始化
- 访问您的域名,按照WordPress安装向导完成安装
- 填写之前创建的数据库信息
- 设置管理员账号和站点信息
- 完成安装后登录后台
四、优化与安全配置
- SSL证书配置
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d your_domain.com
- 文件权限优化
sudo chown -R www-data:www-data /var/www/wordpress
sudo find /var/www/wordpress/ -type d -exec chmod 750 {} \;
sudo find /var/www/wordpress/ -type f -exec chmod 640 {} \;
- 性能优化插件
- WP Super Cache或W3 Total Cache
- Autoptimize
- Smush图片压缩
- 安全插件推荐
- Wordfence Security
- iThemes Security
- Sucuri Security
五、常见问题解决
- 502 Bad Gateway错误
- 检查PHP-FPM是否运行:
sudo systemctl status php7.4-fpm
- 确认Nginx配置中fastcgi_pass路径正确
- 内存不足问题
- 编辑
/etc/php/7.4/fpm/php.ini
增加内存限制:memory_limit = 256M
- 文件上传大小限制
- 修改PHP配置:
upload_max_filesize = 64M
和post_max_size = 64M
通过以上步骤,您已成功在LNMP环境下部署了WordPress应用。LNMP架构为WordPress提供了高性能的运行环境,结合适当的优化和安全措施,可以确保网站稳定高效运行。