nginx服务详细配置

1 安装nginx

[root@web ~]# dnf install nginx -y
# 卸载本地仓库安装
[root@web ~]# dnf remove nginx -y

# ngin官方仓库安装
# 1. 配置nginx官方仓库
[root@web ~]# vim /etc/yum.repos.d/nginx.repo
[root@web ~]# cat /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
# 2. 安装nginx
[root@web ~]# dnf install nginx -y

2 .查看服务

查找 nginx 的启动服务文件:

[root@web ~]# find / -name nginx.service
/usr/lib/systemd/system/nginx.service

[root@web ~]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
#ExecStop=/usr/sbin/nginx -s stop | quit
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=mixed
PrivateTmp=true

[Install]
WantedBy=multi-user.target

3 .启动nginx

[root@web ~]# systemctl start nginx
[root@web ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
     Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; preset: disabled)
     Active: active (running) since Sun 2025-03-09 23:58:02 CST; 3s ago
    Process: 14937 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
    Process: 14940 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
    Process: 14944 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
   Main PID: 14945 (nginx)
      Tasks: 5 (limit: 21357)
     Memory: 11.1M ()
     CGroup: /system.slice/nginx.service
             ├─14945 "nginx: master process /usr/sbin/nginx"
             ├─14946 "nginx: worker process"
             ├─14947 "nginx: worker process"
             ├─14948 "nginx: worker process"
             └─14949 "nginx: worker process"

Mar 09 23:58:02 web systemd[1]: Starting The nginx HTTP and reverse proxy server...
Mar 09 23:58:02 web nginx[14940]: nginx: the configuration file /etc/nginx/nginx.conf syntax >
Mar 09 23:58:02 web nginx[14940]: nginx: configuration file /etc/nginx/nginx.conf test is suc>
Mar 09 23:58:02 web systemd[1]: Started The nginx HTTP and reverse proxy server.

4. 查看nginx.conf配置文件

[root@web ~]# vim /etc/nginx/nginx.conf

5. 配置web服务

[root@web ~]# vim /etc/nginx/conf.d/nfs.conf
[root@web ~]# cat /etc/nginx/conf.d/nfs.conf
server {
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        server_name 192.168.72.8;
        root /var/nginx/;
}

# 验证配置文件是否有效
[root@web ~]# /usr/sbin/nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

# 重启 nginx 服务
[root@web ~]# systemctl restart nginx

6 .放行80端口

[root@web ~]# firewall-cmd --permanent --add-port=80/tcp
success
[root@web ~]# firewall-cmd --reload
success
[root@web ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources:
  services: dhcpv6-client mdns nfs ssh
  ports: 80/tcp
  protocols:
  forward: yes
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

打开浏览器,输入 http://192.168.72.8 来访问:

hello nfs

或者使用 curl 来测试

[root@web ~]# curl http://192.168.72.8
hello nfs
[root@web ~]# curl http://192.168.72.8 -I
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Sun, 09 Mar 2025 16:40:23 GMT
Content-Type: text/html
Content-Length: 10
Last-Modified: Sun, 09 Mar 2025 07:33:28 GMT
Connection: keep-alive
ETag: "67cd4448-a"
Accept-Ranges: bytes

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值