0%

ubuntu 中使用 apt 安装 nginx

使用 apt 安装 nginx ,方便快捷,省去了编译失败的可能。

支持平台

Version Codename Supported Platforms
16.04 xenial x86_64, i386, ppc64el, aarch64/arm64
18.04 bionic x86_64, aarch64/arm64
19.04 disco x86_64
19.10 eoan x86_64

我这里的ubuntu的环境是 19.10 。通过上面的表格可以了解到我这里是支持安装的。

安装

首先,我们安装key

1
curl -fsSL https://nginx.org/keys/nginx_signing.key | sudo apt-key add -

接下来添加 apt repository:

安装主线版本执行以下命令:

1
echo "deb http://nginx.org/packages/mainline/ubuntu `lsb_release -cs` nginx" | sudo tee /etc/apt/sources.list.d/nginx.list

安装稳定版本执行以下命令:

1
echo "deb http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" | sudo tee /etc/apt/sources.list.d/nginx.list

接下来执行如下命令:

1
sudo apt update

下面就可以进行安装了,只需执行以下命令:

1
sudo apt install nginx

执行完成之后,这样就安装完成了,是不是非常方便呢。

相关目录

下面看下配置文件夹的相关目录

1
$ tree /etc/nginx/
2
/etc/nginx/
3
├── conf.d
4
│   └── default.conf
5
├── fastcgi_params
6
├── koi-utf
7
├── koi-win
8
├── mime.types
9
├── modules -> /usr/lib/nginx/modules
10
├── nginx.conf
11
├── scgi_params
12
├── uwsgi_params
13
└── win-utf
14
15
2 directories, 9 files

日志相关:

1
$ tree /var/log/nginx/
2
/var/log/nginx/
3
├── access.log
4
└── error.log
5
6
0 directories, 2 files

缓存相关:

1
$ sudo tree /var/cache/nginx/
2
/var/cache/nginx/
3
├── client_temp
4
├── fastcgi_temp
5
├── proxy_temp
6
├── scgi_temp
7
└── uwsgi_temp
8
9
5 directories, 0 files

管理命令

下面看看支持的命令:

1
$ sudo /etc/init.d/nginx
2
Usage: /etc/init.d/nginx {start|stop|status|restart|reload|force-reload|upgrade|configtest|check-reload}

这里介绍几个常用的

启动:

1
$ sudo /etc/init.d/nginx start
2
[ ok ] Starting nginx (via systemctl): nginx.service.

重启:

1
$ sudo /etc/init.d/nginx restart
2
[ ok ] Restarting nginx (via systemctl): nginx.service.

状态:

1
$ sudo /etc/init.d/nginx status
2
● nginx.service - nginx - high performance web server
3
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
4
   Active: active (running) since Sun 2019-12-08 09:51:20 UTC; 18s ago
5
     Docs: http://nginx.org/en/docs/
6
  Process: 15032 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
7
 Main PID: 15033 (nginx)
8
    Tasks: 2 (limit: 4591)
9
   Memory: 1.7M
10
   CGroup: /system.slice/nginx.service
11
           ├─15033 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
12
           └─15034 nginx: worker process
13
14
Dec 08 09:51:20 baoguoxiao systemd[1]: Starting nginx - high performance web server...
15
Dec 08 09:51:20 baoguoxiao systemd[1]: Started nginx - high performance web server.

停止:

1
$ sudo /etc/init.d/nginx stop
2
[ ok ] Stopping nginx (via systemctl): nginx.service.

检测配置:

1
$ sudo /etc/init.d/nginx configtest
2
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
3
nginx: configuration file /etc/nginx/nginx.conf test is successful

其他的我就不列举了。大家可以自己去试验。