Ubuntu 20安装Nginx

2 篇文章 0 订阅

环境

Ubuntu 20.04

➜  ~ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 20.04 LTS
Release:	20.04
Codename:	focal

安装

sudo apt-get install -y nginx

验证

➜  ~ nginx -v
nginx version: nginx/1.17.10 (Ubuntu)

启动/停止

  • 启动
sudo nginx

注:如果Nginx已经在运行,则该命令会报错。

  • 查看Nginx进程
➜  ~ ps -ef | grep -i nginx | grep -v grep
root        3531    1526  0 21:44 ?        00:00:00 nginx: master process nginx
www-data    5558    3531  0 22:10 ?        00:00:00 nginx: worker process
www-data    5559    3531  0 22:10 ?        00:00:00 nginx: worker process
www-data    5560    3531  0 22:10 ?        00:00:00 nginx: worker process
www-data    5562    3531  0 22:10 ?        00:00:00 nginx: worker process

可见有1个master进程和N个(本例中是4个)worker进程。

Nginx启动后,默认使用80端口。

➜  ~ sudo lsof -i :80
COMMAND  PID     USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   3531     root    7u  IPv4  74711      0t0  TCP *:http (LISTEN)
nginx   3531     root    8u  IPv6  74712      0t0  TCP *:http (LISTEN)
nginx   5558 www-data    7u  IPv4  74711      0t0  TCP *:http (LISTEN)
nginx   5558 www-data    8u  IPv6  74712      0t0  TCP *:http (LISTEN)
nginx   5559 www-data    7u  IPv4  74711      0t0  TCP *:http (LISTEN)
nginx   5559 www-data    8u  IPv6  74712      0t0  TCP *:http (LISTEN)
nginx   5560 www-data    7u  IPv4  74711      0t0  TCP *:http (LISTEN)
nginx   5560 www-data    8u  IPv6  74712      0t0  TCP *:http (LISTEN)
nginx   5562 www-data    7u  IPv4  74711      0t0  TCP *:http (LISTEN)
nginx   5562 www-data    8u  IPv6  74712      0t0  TCP *:http (LISTEN)
  • 通过HTTP方式访问localhost
    在这里插入图片描述
  • 停止Nginx: nginx -s quit
sudo nginx -s quit 

如果成功,则返回消息为空。

注:如果Nginx并没有在运行,则该命令会报错:

➜  ~ sudo nginx -s quit
nginx: [error] open() "/run/nginx.pid" failed (2: No such file or directory)

常用命令

  • 测试Nginx配置: nginx -t
➜  ~ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

如果一切OK,则显示如上信息。否则将会定位到具体哪一行有错误。

注:每次修改完Nginx配置,建议先运行 nginx -t 测试一下。

  • 重启Nginx: nginx -s reload
sudo nginx -s reload

如果成功,则返回消息为空。

注:如果Nginx并没有在运行,则该命令会报错:

➜  ~ sudo nginx -s reload
nginx: [error] open() "/run/nginx.pid" failed (2: No such file or directory)

注: nginx -s <signal> 是给Nginx发送signal信号,比如 stopquitreloadreopen 。其中 stop 会立即停止Nginx,而 quitreload 操作则没那么“暴力”,而是会妥善处理当前正在运行的请求。

添加 HTTP service

Nginx的配置文件是 /etc/nginx/nginx.conf ,打开该文件,里面有

......
http {
......
        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}
......

/etc/nginx/conf.d 目录新建一个后缀名为 conf 的文件,比如 ding.conf ,内容如下:

server {
    listen       6688;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
}

这段代码表明在6688端口开启HTTP服务。

  • listen 指定了监听端口
  • location 后面的 / 指明匹配所有request
  • root 指定了静态文件的根目录
  • index 指明了默认文件。当request里面没有指定文件名时,会尝试使用默认文件

比如,现在当我访问6688端口的 / 目录时,实际上就是访问了servers上的 /usr/share/nginx/html/index.html 文件:

在这里插入图片描述
注:该html文件是伴随Nginx安装自动生成的:

➜  ~ ll /usr/share/nginx/html 
total 4.0K
-rw-r--r-- 1 root root 612 Apr 14 22:19 index.html

注:在安装好Nginx,没有任何自定义配置的时候,访问 localhost:80 ,得到的其实也是一个类似的html文件,这是Nginx default配置的HTTP service,详见下面。

缺省配置

上面有提到,在 nginx.conf 中:

......
        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
......

其中 /etc/nginx/conf.d 目录默认是空的,而 /etc/nginx/sites-enabled 目录则包含了一个软链接文件 default

➜  ~ ll /etc/nginx/sites-enabled
total 0
lrwxrwxrwx 1 ding ding 34 Jun 29 23:01 default -> /etc/nginx/sites-available/default

/etc/nginx/sites-available/default 文件中包含了缺省设置:

......
server {
	listen 80 default_server;
......
	root /var/www/html;
......
	index index.html index.htm index.nginx-debian.html;
......

其中,设置了根目录为 /var/www/html

➜  ~ ll /var/www/html 
total 4.0K
-rw-r--r-- 1 root root 612 Jun 29 23:01 index.nginx-debian.html

可见,直接访问80端口时,得到的就是这个 index.nginx-debian.html 文件。

注意, index.html 是排在最前面的,我们可以创建该文件,内容随便写,随后再访问80端口(不需重启Nginx),得到就是 index.html

➜  ~ curl localhost             
haha, hello!
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值