# 更新软件包列表
sudo apt update
# 安装Nginx
sudo apt install nginx
# 启动Nginx服务
sudo systemctl start nginx
# 设置开机自启
sudo systemctl enable nginx
# 添加EPEL仓库(CentOS/RHEL 7及以下可能需要)
sudo yum install epel-release
# 安装Nginx
sudo yum install nginx
# 启动Nginx服务
sudo systemctl start nginx
# 设置开机自启
sudo systemctl enable nginx
sudo systemctl status nginx
打开浏览器访问 http://服务器IP
,应该能看到Nginx欢迎页面。
/etc/nginx/nginx.conf
/etc/nginx/conf.d/
或 /etc/nginx/sites-available/
/usr/share/nginx/html
或 /var/www/html
sudo mkdir -p /var/www/example.com/html
sudo chown -R $USER:$USER /var/www/example.com/html
sudo chmod -R 755 /var/www/example.com
echo "<h1>Welcome to Example.com</h1>" > /var/www/example.com/html/index.html
sudo nano /etc/nginx/conf.d/example.com.conf
添加以下内容:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
sudo nginx -t # 测试配置语法
sudo systemctl restart nginx
sudo ufw allow 'Nginx HTTP'
sudo ufw enable
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
sudo systemctl reload nginx
sudo nginx -t
nginx -v
或 nginx -V
(详细版本)/var/log/nginx/access.log
/var/log/nginx/error.log
在/etc/nginx/nginx.conf
的http块中添加:
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
# 安装Certbot
sudo apt install certbot python3-certbot-nginx # Ubuntu
sudo yum install certbot python3-certbot-nginx # CentOS
# 获取证书
sudo certbot --nginx -d example.com -d www.example.com
# 设置自动续期
sudo certbot renew --dry-run
完成以上步骤后,您就成功在Linux系统上安装并配置了Nginx服务器。根据您的具体需求,可以进一步调整配置参数。