nginx 基本配置

一篇比较好的参考文

https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-18-04

 

1. 在 Ubuntu 上安装 Nginx

sudo apt-get nginx

2. 启动 Nginx 之前先测试

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

3. 启动 Nginx

$ sudo nginx

4. 关闭 Nginx

$ ps -ef|grep nginx
root     23670     1  0 07:38 ?        00:00:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data 23673 23670  0 07:38 ?        00:00:00 nginx: worker process
ubuntu   23892 17276  0 08:05 pts/0    00:00:00 grep --color=auto nginx
$ sudo kill -QUIT 23670
-bash: kill: (23670) - Operation not permitted
$ sudo kill -QUIT 23670
$ ps -ef|grep nginx
ubuntu   23910 17276  0 08:06 pts/0    00:00:00 grep --color=auto nginx

5. 重启 Nginx

$ sudo service nginx restart
Job for nginx.service failed because the control process exited with error code.
See "systemctl status nginx.service" and "journalctl -xe" for details.
$ systemctl status nginx.service
● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Mon 2020-01-06 09:27:18 UTC; 16s ago
     Docs: man:nginx(8)
  Process: 23897 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exi
  Process: 24919 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=1/FAILURE)
  Process: 24909 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCES
 Main PID: 23670 (code=exited, status=0/SUCCESS)

Jan 06 09:27:17 ip-172-31-26-101 nginx[24919]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in 
Jan 06 09:27:17 ip-172-31-26-101 nginx[24919]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use
Jan 06 09:27:17 ip-172-31-26-101 nginx[24919]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in 
Jan 06 09:27:17 ip-172-31-26-101 nginx[24919]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use
Jan 06 09:27:18 ip-172-31-26-101 nginx[24919]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in 
Jan 06 09:27:18 ip-172-31-26-101 nginx[24919]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use
Jan 06 09:27:18 ip-172-31-26-101 nginx[24919]: nginx: [emerg] still could not bind()
Jan 06 09:27:18 ip-172-31-26-101 systemd[1]: nginx.service: Control process exited, code=exited status=1
Jan 06 09:27:18 ip-172-31-26-101 systemd[1]: nginx.service: Failed with result 'exit-code'.
Jan 06 09:27:18 ip-172-31-26-101 systemd[1]: Failed to start A high performance web server and a reverse proxy ser

6. Fix "98: Address already in use"

$ sudo fuser -k 80/tcp
80/tcp:              24855 24872
$ sudo service nginx restart
$ systemctl status nginx.service
● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2020-01-06 09:31:49 UTC; 16s ago
     Docs: man:nginx(8)
  Process: 23897 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exi
  Process: 24966 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 24956 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCES
 Main PID: 24969 (nginx)
    Tasks: 2 (limit: 1152)
   CGroup: /system.slice/nginx.service
           ├─24969 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           └─24973 nginx: worker process

Jan 06 09:31:49 ip-172-31-26-101 systemd[1]: Starting A high performance web server and a reverse proxy server...
Jan 06 09:31:49 ip-172-31-26-101 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid 
Jan 06 09:31:49 ip-172-31-26-101 systemd[1]: Started A high performance web server and a reverse proxy server.

7. Fix "Failed to parse PID from file /run/nginx.pid: Invalid"

$ sudo mkdir /etc/systemd/system/nginx.service.d
$ printf "[Service]\nExecStartPost=/bin/sleep 0.1\n" | \
    sudo tee /etc/systemd/system/nginx.service.d/override.conf
$ sudo systemctl daemon-reload
$ sudo systemctl restart nginx

8. 查看网络状态

$ lsof -i:80
COMMAND     PID USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
AliYunDun 15674 root   18u  IPv4 10525686      0t0  TCP 172.18.195.126:35792->100.100.30.25:http (ESTABLISHED)
nginx     32052 root    8u  IPv4  5792173      0t0  TCP *:http (LISTEN)
nginx     32062  www    8u  IPv4  5792173      0t0  TCP *:http (LISTEN)
nginx     32063  www    8u  IPv4  5792173      0t0  TCP *:http (LISTEN)

$ netstat -plunt | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      27452/nginx: master 
tcp6       0      0 :::80                   :::*                    LISTEN      27452/nginx: master 

7. 查看 Nginx 状态

$ service nginx status
● nginx.service - LSB: starts the nginx web server
   Loaded: loaded (/etc/init.d/nginx; bad; vendor preset: enabled)
   Active: active (exited) since Mon 2018-12-03 20:16:57 CST; 1 years 1 months ago
     Docs: man:systemd-sysv-generator(8)
    Tasks: 0
   Memory: 0B
      CPU: 0

Dec 03 20:16:57 iZwz9cx35156qjuf3z1zuyZ systemd[1]: Starting LSB: starts the nginx web server...
Dec 03 20:16:57 iZwz9cx35156qjuf3z1zuyZ nginx[565]: Starting nginx...  done
Dec 03 20:16:57 iZwz9cx35156qjuf3z1zuyZ systemd[1]: Started LSB: starts the nginx web server.

10. 防火墙设置

$ sudo ufw enable
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup

$ sudo ufw app list
Available applications:
  Nginx Full
  Nginx HTTP
  Nginx HTTPS
  OpenSSH

$ sudo ufw allow 'Nginx HTTP'
Rules updated
Rules updated (v6)

sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
Nginx HTTP                 ALLOW       Anywhere                  
Nginx HTTP (v6)            ALLOW       Anywhere (v6) 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值