一、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+)为例说明:
确保系统包为最新版本,避免兼容性问题:
# CentOS
sudo yum update -y
# Ubuntu
sudo apt update && sudo apt upgrade -y
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默认页面则说明成功。
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远程登录)。
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安装步骤
从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 # 清理临时文件
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;
复制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)。
确保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 {} \;
浏览器访问服务器IP或域名(如http://your_server_ip),将进入WordPress安装向导:
admin)、密码、电子邮件→ 点击“安装WordPress”。http://your_server_ip/wp-admin)。三、配置优化与安全加固
若需绑定域名或多站点,需创建虚拟主机配置:
# CentOS(配置文件路径:/etc/httpd/conf.d/yourdomain.conf)
sudo nano /etc/httpd/conf.d/yourdomain.conf
添加以下内容(替换yourdomain.com为你的域名):
<VirtualHost *:80>
ServerName yourdomain.com
ServerAlias www.yourdomain.com
DocumentRoot /var/www/html
<Directory /var/www/html>
AllowOverride All # 允许.htaccess覆盖(用于WordPress permalinks)
Require all granted
</Directory>
ErrorLog /var/log/httpd/yourdomain_error.log
CustomLog /var/log/httpd/yourdomain_access.log combined
</VirtualHost>
重启Apache使配置生效:
sudo systemctl restart httpd
使用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
wp-config.php增强安全性:添加以下代码(在<?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
mysqldump)及文件。四、常见问题排查
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中的数据库信息是否正确,数据库用户是否有权限。chown -R apache:apache /var/www/html/)或Apache配置(AllowOverride All是否开启)。按照以上步骤操作,即可在LAMP环境中成功安装并配置WordPress,搭建属于自己的网站。