一、準(zhǔn)備工作
在開始搭建之前,您需要準(zhǔn)備以下正文:
- 一臺獨立服務(wù)器(推薦配置至少2核CPU、4GB內(nèi)存)
- 域名一個(已做好DNS解析)
- SSH連接工具(如PuTTY或Terminal)
- 基礎(chǔ)Linux操作知識
二、服務(wù)器環(huán)境配置
1. 系統(tǒng)更新
sudo apt update && sudo apt upgrade -y
2. 安裝LAMP環(huán)境
# 安裝Apache
sudo apt install apache2 -y
# 安裝MySQL
sudo apt install mysql-server -y
# 安裝PHP及相關(guān)擴展
sudo apt install php libapache2-mod-php php-mysql php-curl php-gd php-mbstring php-xml php-xmlrpc php-soap php-intl php-zip -y
3. 配置MySQL安全
sudo mysql_secure_installation
三、創(chuàng)建WordPress數(shù)據(jù)庫
- 登錄MySQL:
sudo mysql -u root -p
- 創(chuàng)建數(shù)據(jù)庫和用戶:
CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
CREATE USER 'wordpressuser'@'localhost' IDENTIFIED BY 'your_strong_password';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpressuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
四、安裝WordPress
- 下載最新版WordPress:
cd /tmp
wget https://wordpress.org/latest.tar.gz
tar -xzvf latest.tar.gz
- 移動文件到網(wǎng)站目錄:
sudo mv wordpress /var/www/html/
sudo chown -R www-data:www-data /var/www/html/wordpress
sudo chmod -R 755 /var/www/html/wordpress
五、配置Apache虛擬主機
- 創(chuàng)建配置文件:
sudo nano /etc/apache2/sites-available/wordpress.conf
- 添加以下內(nèi)容(替換your_domain.com為您的域名):
<VirtualHost *:80>
ServerAdmin admin@your_domain.com
ServerName your_domain.com
ServerAlias www.your_domain.com
DocumentRoot /var/www/html/wordpress
<Directory /var/www/html/wordpress>
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
- 啟用配置:
sudo a2ensite wordpress.conf
sudo a2enmod rewrite
sudo systemctl restart apache2
六、完成WordPress安裝
- 在瀏覽器訪問您的域名
- 按照向?qū)瓿砂惭b
- 填寫之前創(chuàng)建的數(shù)據(jù)庫信息
- 設(shè)置管理員賬號和密碼
七、安全加固建議
- 安裝防火墻:
sudo apt install ufw
sudo ufw allow 80
sudo ufw allow 22
sudo ufw enable
- 定期更新系統(tǒng)和WordPress
- 安裝安全插件如Wordfence
- 配置SSL證書(推薦使用Let’s Encrypt)
八、常見問題解決
- 權(quán)限問題:確保/var/www/html/wordpress目錄權(quán)限正確
- 404錯誤:檢查Apache的mod_rewrite是否啟用
- 數(shù)據(jù)庫連接錯誤:確認數(shù)據(jù)庫用戶名密碼正確
通過以上步驟,您已成功在獨立服務(wù)器上搭建了WordPress網(wǎng)站。后續(xù)可以根據(jù)需求安裝主題、插件,并優(yōu)化網(wǎng)站性能。