一、安装 Nginx(最常用两种)
1)CentOS / RHEL
bash
yum install -y nginx
2)Ubuntu / Debian
bash
apt update
apt install -y nginx
安装完查看版本
bash
nginx -v
二、Nginx 关键路径(必须记住)
plaintext
# 主配置
/etc/nginx/nginx.conf
# 子配置(站点放这里)
/etc/nginx/conf.d/*.conf
# 默认网站根目录
/usr/share/nginx/html
# 或
/var/www/html
# 日志目录
/var/log/nginx/
三、配置前必做:检查配置是否正确
改配置前、重启前,一定要先测试!
bash
nginx -t
出现 test is successful 才是对的。
四、最简单可用的站点配置
新建:/etc/nginx/conf.d/myweb.conf
nginx
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
五、启动、重启、重载(最常用)
1)启动
bash
systemctl start nginx
2)停止
bash
systemctl stop nginx
3)重启(会断一下连接)
bash
systemctl restart nginx
4)重载(推荐!不中断业务)
bash
systemctl reload nginx
线上改配置一律用 reload,不要用 restart
5)开机自启
bash
systemctl enable nginx
6)看状态
bash
运行
systemctl status nginx
六、排查 Nginx 起不来 / 访问不了
1)看服务状态
bash
systemctl status nginx
2)看错误日志
bash
tail -f /var/log/nginx/error.log
3)看 80 端口被谁占了
bash
netstat -tulpn | grep :80
ss -tulpn | grep :80
4)配置语法检查
bash
nginx -t
七、你可以直接背的万能流程
- 改配置
nginx -t检查systemctl reload nginx生效systemctl status nginx看是否正常