Nginx配置与踩坑

系统平台:CentOS 7

安装Nginx

1.下载Nginx:http://nginx.org/  ,选择需要的版本/download/nginx-1.20.2.tar.gz ,在a标签中查看

wget http://nginx.org/download/nginx-1.20.2.tar.gz
[root@localhost ~]# ls
anaconda-ks.cfg  nginx-1.20.2  nginx-1.20.2.tar.gz  res

2.解压

[root@bogon src]# tar zxvf nginx-1.20.2.tar.gz

3.编译配置

#编译安装
./configure --prefix=/usr/local/nginx
make
make install

如果报错请

安装PCRE库:让 Nginx 支持 Rewrite 功能。

安装zlib库:默认安装,无损数据压缩库

安装gcc:默认安装,编程语言译器

yum install -y gcc 
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel 

4.查看版本号

[root@localhost ~]# cd /usr/local/nginx/sbin
[root@localhost sbin]# ls
nginx
[root@localhost sbin]# nginx -v
nginx version: nginx/1.20.2

5.启动Nginx

./nginx 启动
./nginx -s stop 快速停止
./nginx -s quit 优雅关闭,在退出前完成已经接受的连接请求 
./nginx -s reload 重新加载配置
ps -ef |grep nginx 查看是否启动成功

  

6. 报错

[root@localhost sbin]# ./nginx
nginx: [emerg] bind() to 0.0.0.0:8081 failed (98: Address already in use)
这里网上说是端口被占用但此时我是修改了端口的为8081端口
解决方式2:直接杀进程
https://blog.csdn.net/yufeng_lai/article/details/88819981

如果你要修改端口 

[root@localhost nginx]# cd /usr/local/nginx/
[root@localhost nginx]# ls
client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp
[root@localhost nginx]# vi ./conf/nginx.conf


server {
        #修改此处
        listen     8081;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

检查配置文件nginx.conf的正确性命令:

[root@localhost sbin]# ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

防火墙 

#1.关闭防火墙 
[root@localhost sbin]# systemctl stop firewalld
[root@localhost sbin]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)

#2.开启防火墙 
[root@localhost sbin]# systemctl start firewalld
[root@localhost sbin]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: active (running) since 四 2022-03-31 22:57:11 CST; 35s ago
     Docs: man:firewalld(1)
 Main PID: 8951 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─8951 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid
#3.查看防火墙状态 
systemctl status firewalld

#4.重启防火墙
firewall-cmd --reload 

Nginx配置成系统服务 

1.在/usr/lib/systemd/system目录下添加nginx.service

vi /usr/lib/systemd/system/nginx.service

[Unit]
Description=nginx web service
Documentation=http://nginx.org/en/docs/
After=network.target

[Service]
Type=forking
#nginx pid文件位置
PIDFile=/usr/local/nginx/logs/nginx.pid
#-t 测试nginx配置文件  -c 指定nginx配置文件位置
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=default.target

2.这里可能有权限问题

chmod 755 /usr/lib/systemd/system/nginx.service

3.使用系统命令来操作Nginx服务

启动: systemctl start nginx
停止: systemctl stop nginx
重启: systemctl restart nginx
重新加载配置文件: systemctl reload nginx
查看nginx状态: systemctl status nginx
开机启动: systemctl enable nginx

Nginx配置与踩坑_第1张图片

 Nginx命令配置到系统环境

vi /etc/profile
在最后一行添加
export PATH=$PATH:/usr/local/nginx/sbin

使之立即生效
source /etc/profile

执行nginx命令
nginx -V

 到此结束

Nginx配置与踩坑_第2张图片

踩坑不易,点个赞吧

Nginx配置与踩坑_第3张图片

你可能感兴趣的:(nginx)