Ubuntu Server环境下WordPress安装及Apache配置技术咨询
Hey Wayne, let's tackle this step by step—since you're stuck on the Apache test page and need a fully updated WordPress setup with proper Apache configuration, I'll break down every detail you need, including beyond just virtual hosts and /etc/hosts.
一、基础WordPress安装(以Ubuntu/Debian为例;CentOS/RHEL流程类似)
First, let's get the core environment and WordPress files set up:
- Update your system packages to ensure you're working with the latest versions:
sudo apt update && sudo apt upgrade -y - Install the LAMP stack (Apache, MariaDB/MySQL, PHP + required extensions):
sudo apt install apache2 mariadb-server php php-mysql php-curl php-gd php-mbstring php-xml php-xmlrpc php-soap php-intl php-zip -y - Secure your MariaDB/MySQL instance (follow the prompts to set a root password, remove anonymous users, and lock down access):
sudo mysql_secure_installation - Create a dedicated database and user for WordPress (this is more secure than using the root account):
Log into MariaDB/MySQL:
Run these SQL commands (replacesudo mysql -u root -pyour_strong_passwordwith a secure password of your choice):CREATE DATABASE wp_db DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci; CREATE USER 'wp_user'@'localhost' IDENTIFIED BY 'your_strong_password'; GRANT ALL PRIVILEGES ON wp_db.* TO 'wp_user'@'localhost'; FLUSH PRIVILEGES; EXIT; - Download and extract the latest WordPress files:
This creates acd /var/www/html sudo wget https://wordpress.org/latest.tar.gz sudo tar -xzf latest.tar.gzwordpresssubdirectory under/var/www/htmlwhere all your site files live. - Set correct file permissions so Apache can read/write to the WordPress directory:
sudo chown -R www-data:www-data /var/www/html/wordpress sudo chmod -R 755 /var/www/html/wordpress
二、Apache详细配置
Let's cover virtual hosts, /etc/hosts, and all the extra critical settings you're asking about.
1. 虚拟主机文件(分步配置)
Virtual hosts let you host multiple sites on one server, and they're key to pointing your domain to WordPress.
- Create a new virtual host config file (replace
yourdomain.confwith a name matching your site):sudo nano /etc/apache2/sites-available/yourdomain.conf - Paste the following configuration, replacing placeholders with your actual domain and WordPress path:
<VirtualHost *:80> ServerAdmin webmaster@yourdomain.com ServerName yourdomain.com ServerAlias www.yourdomain.com DocumentRoot /var/www/html/wordpress <Directory /var/www/html/wordpress/> Options FollowSymLinks AllowOverride All # 关键配置,让WordPress固定链接生效 Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/yourdomain_error.log CustomLog ${APACHE_LOG_DIR}/yourdomain_access.log combined </VirtualHost> - Enable the new virtual host and disable Apache's默认测试站点:
sudo a2ensite yourdomain.conf sudo a2dissite 000-default.conf
2. /etc/hosts配置(仅本地测试用)
如果你在本地测试(未使用公网域名),编辑hosts文件将域名映射到服务器IP:
- 打开hosts文件:
sudo nano /etc/hosts - 在文件末尾添加一行(替换
192.168.1.100为你的服务器本地IP,yourdomain.com为测试域名):192.168.1.100 yourdomain.com www.yourdomain.com - 保存退出后,本地机器访问
yourdomain.com就会指向你的服务器。
3. 其他关键Apache/PHP配置项
除了以上内容,这些设置是WordPress正常运行的必要条件:
- 启用
mod_rewrite: WordPress固定链接功能必须依赖此模块:sudo a2enmod rewrite - 调整PHP设置: 编辑PHP配置文件(路径随PHP版本变化,示例为PHP 8.2):
更新以下值以避免常见的WordPress错误:sudo nano /etc/php/8.2/apache2/php.inimemory_limit = 256M # 给WordPress足够的运行内存 upload_max_filesize = 64M # 允许上传大媒体文件 post_max_size = 64M max_execution_time = 300 # 避免插件/主题安装时超时 - 重启Apache使所有配置生效:
sudo systemctl restart apache2
三、将服务器IP从Apache测试页重定向到WordPress子目录
目前你的IP加载测试页,是因为默认站点仍指向/var/www/html。这里有两种简单的修复方法:
方法1:更新默认站点直接指向WordPress
- 编辑Apache默认站点配置:
sudo nano /etc/apache2/sites-available/000-default.conf - 将
DocumentRoot /var/www/html替换为:DocumentRoot /var/www/html/wordpress - 在下方添加目录权限块(启用固定链接):
<Directory /var/www/html/wordpress/> Options FollowSymLinks AllowOverride All Require all granted </Directory> - 保存并重启Apache:
现在访问服务器IP就会加载WordPress安装向导。sudo systemctl restart apache2
方法2:使用Alias(保留/var/www/html目录)
如果你想保留默认的/var/www/html目录用于其他文件,但仍让IP流量指向WordPress:
- 编辑默认站点配置:
sudo nano /etc/apache2/sites-available/000-default.conf - 在
DocumentRoot /var/www/html下方添加一行别名配置:Alias / /var/www/html/wordpress/ - 添加与方法1相同的目录权限块,然后重启Apache即可。
最后一步:完成WordPress安装
在浏览器中访问你的域名或服务器IP,跟随WordPress设置向导操作:
- 输入之前创建的数据库详情(数据库名
wp_db、用户名wp_user、密码、主机localhost) - 设置站点标题、管理员用户名和密码
- 点击"安装WordPress",完成配置!
内容的提问来源于stack exchange,提问作者wayne

