常见的Linux Web服务器软件: - Apache HTTP Server - 最流行的开源Web服务器 - Nginx - 高性能、轻量级的Web服务器和反向代理 - Lighttpd - 轻量级Web服务器,适合资源受限环境
# Debian/Ubuntu
sudo apt update
sudo apt install apache2
# RHEL/CentOS
sudo yum install httpd
sudo systemctl start httpd
sudo systemctl enable httpd
# Debian/Ubuntu
sudo apt install nginx
# RHEL/CentOS
sudo yum install epel-release
sudo yum install nginx
sudo systemctl start nginx
sudo systemctl enable nginx
主配置文件通常位于:
- /etc/apache2/apache2.conf
(Debian/Ubuntu)
- /etc/httpd/conf/httpd.conf
(RHEL/CentOS)
常用命令:
# 检查配置语法
sudo apachectl configtest
# 重新加载配置
sudo systemctl reload apache2
主配置文件通常位于:
- /etc/nginx/nginx.conf
常用命令:
# 检查配置语法
sudo nginx -t
# 重新加载配置
sudo systemctl reload nginx
<VirtualHost *:80>
ServerAdmin webmaster@example.com
ServerName example.com
DocumentRoot /var/www/example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
启用站点:
sudo a2ensite example.com.conf
sudo systemctl reload apache2
server {
listen 80;
server_name example.com;
root /var/www/example.com/public_html;
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
location / {
try_files $uri $uri/ =404;
}
}
启用站点:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
bash
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
bash
sudo chown -R www-data:www-data /var/www/example.com
sudo chmod -R 755 /var/www/example.com
sudo apt install certbot python3-certbot-nginx # 对于Nginx
sudo apt install certbot python3-certbot-apache # 对于Apache
# 获取证书
sudo certbot --nginx -d example.com -d www.example.com
# 或对于Apache
sudo certbot --apache -d example.com -d www.example.com
# 设置自动续订
sudo certbot renew --dry-run
KeepAlive
设置MPM
模块配置apache
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript
</IfModule>
nginx
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
# Apache
tail -f /var/log/apache2/access.log
# Nginx
tail -f /var/log/nginx/access.log
sudo apt install goaccess
服务器无法启动
sudo netstat -tulnp | grep :80
sudo apachectl configtest
或 sudo nginx -t
403禁止访问错误
500内部服务器错误
502 Bad Gateway错误
通过以上步骤,您应该能够成功配置和管理Linux上的Web服务器。根据您的具体需求,可能需要进一步调整配置以获得最佳性能和安全性。