一、LAMP环境准备(Linux + Apache + MySQL/MariaDB + PHP)
在安装WordPress前,需先搭建LAMP基础环境。以下以CentOS 7/8(Apache 2.4、MariaDB 10.5、PHP 8.0+)和Ubuntu 22.04(Apache 2.4、MySQL 8.0、PHP 8.1+)为例说明:
1. 更新系统软件包
确保系统包为最新版本,避免兼容性问题:
# CentOS
sudo yum update -y
# Ubuntu
sudo apt update && sudo apt upgrade -y
2. 安装Apache Web服务器
Apache是LAMP的核心Web服务,负责处理HTTP请求:
# CentOS
sudo yum install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd # 开机自启
# Ubuntu
sudo apt install apache2 -y
sudo systemctl start apache2
sudo systemctl enable apache2
验证安装:浏览器访问服务器IP,若看到Apache默认页面则说明成功。
3. 安装MySQL/MariaDB数据库
WordPress需要数据库存储内容(如文章、用户信息),推荐使用MariaDB(MySQL分支,兼容性更好)或MySQL:
# CentOS(MariaDB)
sudo yum install mariadb-server mariadb -y
sudo systemctl start mariadb
sudo systemctl enable mariadb
# Ubuntu(MySQL)
sudo apt install mysql-server mysql-client -y
sudo systemctl start mysql
sudo systemctl enable mysql
安全配置:运行安全脚本设置root密码、移除匿名用户、禁用远程root登录:
# MariaDB/MySQL通用
sudo mysql_secure_installation
按提示操作(如设置root密码、移除匿名用户、禁止root远程登录)。
4. 安装PHP及必要扩展
WordPress依赖PHP处理动态内容,需安装PHP核心及常用扩展(如MySQL驱动、GD库、MBstring):
# CentOS
sudo yum install php php-mysqlnd php-gd php-mbstring php-xml php-cli -y
sudo systemctl restart httpd
# Ubuntu
sudo apt install php php-mysql php-gd php-mbstring php-xml php-cli -y
sudo systemctl restart apache2
注:PHP版本需≥7.4(推荐8.0+),避免WordPress兼容性问题。
二、WordPress安装步骤
1. 下载并解压WordPress
从WordPress官网获取最新版本,解压到Apache默认Web目录(/var/www/html):
cd /tmp
wget https://wordpress.org/latest.tar.gz
tar -xzf latest.tar.gz
sudo cp -a wordpress/. /var/www/html/ # 复制文件到Web目录
sudo rm -rf wordpress latest.tar.gz # 清理临时文件
2. 配置WordPress数据库
WordPress需要连接数据库存储数据,需创建专用数据库和用户:
# 登录数据库(MariaDB/MySQL通用)
sudo mysql -u root -p
# 创建数据库(如wordpress_db)
CREATE DATABASE wordpress_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
# 创建用户(如wp_user)并设置密码(如StrongPassword123)
CREATE USER 'wp_user'@'localhost' IDENTIFIED BY 'StrongPassword123';
# 授权用户对数据库的所有权限
GRANT ALL PRIVILEGES ON wordpress_db.* TO 'wp_user'@'localhost';
# 刷新权限使更改生效
FLUSH PRIVILEGES;
# 退出数据库
EXIT;
3. 配置WordPress参数
复制WordPress配置模板并修改数据库信息:
cd /var/www/html
sudo cp wp-config-sample.php wp-config.php
sudo nano wp-config.php
修改以下参数(对应步骤2中的数据库信息):
define('DB_NAME', 'wordpress_db'); // 数据库名
define('DB_USER', 'wp_user'); // 数据库用户
define('DB_PASSWORD', 'StrongPassword123'); // 数据库密码
define('DB_HOST', 'localhost'); // 数据库主机(本地为localhost)
保存并退出(Ctrl+O→Enter→Ctrl+X)。
4. 设置文件权限
确保Apache用户(apache/www-data)对WordPress目录有读写权限:
# CentOS(Apache用户为apache)
sudo chown -R apache:apache /var/www/html/
# Ubuntu(Apache用户为www-data)
sudo chown -R www-data:www-data /var/www/html/
# 设置目录权限(755为目录,644为文件)
sudo find /var/www/html/ -type d -exec chmod 755 {} \;
sudo find /var/www/html/ -type f -exec chmod 644 {} \;
5. 完成WordPress安装
浏览器访问服务器IP或域名(如http://your_server_ip),将进入WordPress安装向导:
- 选择语言(如中文简体)→ 点击“继续”。
- 输入站点标题、管理员账号(如
admin)、密码、电子邮件→ 点击“安装WordPress”。 - 安装完成后,使用管理员账号登录后台(
http://your_server_ip/wp-admin)。
三、配置优化与安全加固
1. 配置Apache虚拟主机(可选,多站点必备)
若需绑定域名或多站点,需创建虚拟主机配置:
# CentOS(配置文件路径:/etc/httpd/conf.d/yourdomain.conf)
sudo nano /etc/httpd/conf.d/yourdomain.conf
添加以下内容(替换yourdomain.com为你的域名):
:80 >
ServerName yourdomain.com
ServerAlias www.yourdomain.com
DocumentRoot /var/www/html
AllowOverride All # 允许.htaccess覆盖(用于WordPress permalinks)
Require all granted
ErrorLog /var/log/httpd/yourdomain_error.log
CustomLog /var/log/httpd/yourdomain_access.log combined
重启Apache使配置生效:
sudo systemctl restart httpd
2. 启用HTTPS(强制安全连接)
使用Let’s Encrypt免费SSL证书,实现HTTPS加密:
# 安装Certbot(证书管理工具)
sudo yum install epel-release -y # CentOS需先安装EPEL仓库
sudo yum install certbot python3-certbot-apache -y
# 获取并安装证书(自动配置Apache)
sudo certbot --apache -d yourdomain.com -d www.yourdomain.com
# 按提示操作(如同意协议、输入邮箱),完成后会自动重定向HTTP到HTTPS
证书有效期为90天,设置自动续期:
sudo systemctl enable certbot.timer
3. WordPress安全优化
- 修改
wp-config.php增强安全性:添加以下代码(在标签后):define('WP_SECURITY_KEY', 'your_random_string_here'); // 生成随机字符串(如使用https://api.wordpress.org/secret-key/1.1/salt/) define('DISALLOW_FILE_EDIT', true); // 禁止通过后台编辑主题/插件文件 - 禁用危险函数:编辑
php.ini,添加以下内容(防止恶意代码执行):disable_functions = exec,passthru,shell_exec,system - 定期备份:使用插件(如UpdraftPlus)或手动备份数据库(
mysqldump)及文件。
四、常见问题排查
- 无法访问WordPress:检查Apache是否启动(
sudo systemctl status httpd)、防火墙是否放行80/443端口(sudo firewall-cmd --add-service=http --permanent; sudo firewall-cmd --add-service=https --permanent; sudo firewall-cmd --reload)。 - 数据库连接错误:确认
wp-config.php中的数据库信息是否正确,数据库用户是否有权限。 - 页面显示403 Forbidden:检查文件权限(
chown -R apache:apache /var/www/html/)或Apache配置(AllowOverride All是否开启)。
按照以上步骤操作,即可在LAMP环境中成功安装并配置WordPress,搭建属于自己的网站。
以上就是关于“如何在LAMP中安装和配置WordPress”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm