1. 系統準備:更新與基礎優化
在部署前,確保系統及軟件包為最新版本,減少安全漏洞并提升兼容性:
sudo apt update && sudo apt upgrade -y
優化系統內核參數(編輯/etc/sysctl.conf
),提升網絡性能:
net.ipv4.tcp_tw_reuse = 1 # 復用TIME-WAIT連接
net.core.somaxconn = 4096 # 增加最大連接隊列長度
net.core.netdev_max_backlog = 4096 # 提高網絡設備接收隊列容量
應用配置:sudo sysctl -p
。
2. Web服務器選擇與安裝
根據需求選擇服務器:
Nginx安裝步驟:
sudo apt install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
Apache安裝步驟:
sudo apt install apache2 -y
sudo systemctl start apache2
sudo systemctl enable apache2
驗證服務狀態:sudo systemctl status nginx
(或apache2
)。
3. 配置虛擬主機(多站點支持)
Nginx虛擬主機配置:
/etc/nginx/sites-available/example.com
):server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
# 啟用Gzip壓縮
gzip on;
gzip_types text/plain text/css application/json application/javascript;
}
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t # 檢查配置語法
sudo systemctl reload nginx
Apache虛擬主機配置:
/etc/apache2/sites-available/example.com.conf
):<VirtualHost *:80>
ServerAdmin webmaster@example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/html
<Directory /var/www/example.com/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
sudo a2ensite example.com.conf
sudo a2dissite 000-default.conf
sudo systemctl reload apache2
創建網站根目錄并添加測試頁面:
sudo mkdir -p /var/www/example.com/html
echo "<h1>Welcome to Example.com!</h1>" | sudo tee /var/www/example.com/html/index.html
4. 性能優化:關鍵措施
啟用緩存:
/etc/nginx/nginx.conf
):location ~* \.(jpg|jpeg|png|gif|css|js)$ {
expires 30d;
add_header Cache-Control "public";
}
mod_cache
模塊(/etc/apache2/mods-enabled/cache.conf
):<IfModule mod_cache.c>
CacheQuickHandler off
CacheLock on
CacheLockPath /tmp/mod_cache-lock
CacheLockMaxAge 5
CacheIgnoreHeaders Set-Cookie
<IfModule mod_disk_cache.c>
CacheRoot /var/cache/apache2
CacheEnable disk /
CacheDirLevels 2
CacheDirLength 1
</IfModule>
</IfModule>
壓縮傳輸:
/etc/nginx/nginx.conf
):gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
mod_deflate
(/etc/apache2/mods-enabled/deflate.conf
):AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/json
調整進程/連接數:
worker
進程(/etc/nginx/nginx.conf
):worker_processes auto; # 自動匹配CPU核心數
events {
worker_connections 1024; # 每個進程的最大連接數
}
mpm_prefork
(/etc/apache2/mods-enabled/mpm_prefork.conf
,適用于傳統場景):<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxRequestWorkers 150
MaxConnectionsPerChild 1000
</IfModule>
PHP加速(若使用PHP):
sudo apt install php-opcache
),編輯/etc/php/8.1/fpm/php.ini
:opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
5. 安全增強:防范風險
配置防火墻:使用ufw
限制端口訪問:
sudo ufw allow 'Nginx Full' # 或 'Apache Full'(端口80+443)
sudo ufw enable
啟用SSL/TLS:使用Let’s Encrypt免費證書(安裝Certbot):
sudo apt install certbot python3-certbot-nginx -y # Nginx
# sudo apt install certbot python3-certbot-apache -y # Apache
sudo certbot --nginx -d example.com -d www.example.com # 自動配置HTTPS
證書自動續期(Let’s Encrypt有效期90天):
sudo certbot renew --dry-run # 測試續期
禁用不必要的模塊:
autoindex
(避免目錄列表泄露):<Directory /var/www/example.com/html>
Options -Indexes
</Directory>
/etc/nginx/sites-enabled/000-default.conf
)。6. 監控與維護:持續優化
htop
(進程)、netdata
(網絡/磁盤)查看系統狀態:sudo apt install htop netdata -y
sudo systemctl start netdata
/var/log/nginx/access.log
、/var/log/apache2/access.log
),識別高頻請求或錯誤。rsync
或BorgBackup
定期備份網站文件和數據庫(如MySQL)。