如何在 Ubuntu 16.04 上安装和配置 Nginx
在本文中,我们将学习如何安装和配置 Nginx Web 服务器。Nginx 是最流行的 Web 服务器,一些最受欢迎和流量最大的网站都托管在上面,它比 Apache 更友好、更安全,也可用作 Web 服务器。
先决条件
在机器上安装 Ubuntu 16.04,并拥有一个具有 sudo 权限的非 root 用户。
在机器上安装 Nginx
我们可以安装 Nginx,它在 Ubuntu 的默认存储库中可用,因此可以直接使用 apt 命令安装 Nginx。
在安装 Nginx 之前,我们需要使用以下命令更新机器
$ sudo apt-get update Get:1 http://security.ubuntu.com/ubuntu xenial-security InRelease [94.5 kB] Hit:2 http://in.archive.ubuntu.com/ubuntu xenial InRelease Get:3 http://in.archive.ubuntu.com/ubuntu xenial-updates InRelease [95.7 kB] Hit:4 http://in.archive.ubuntu.com/ubuntu xenial-backports InRelease Fetched 190 kB in 12s (15.5 kB/s) Reading package lists... Done Building dependency tree Reading state information... Done 19 packages can be upgraded. Run 'apt list --upgradable' to see them.
更新 Linux 后,我们需要使用以下命令安装 Nginx
$ sudo apt-get install nginx Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0 libjpeg-turbo8 libjpeg8 libtiff5 libvpx3 libxpm4 libxslt1.1 nginx-common nginx-core Suggested packages: libgd-tools fcgiwrap nginx-doc ssl-cert The following NEW packages will be installed: fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0 libjpeg-turbo8 libjpeg8 libtiff5 libvpx3 libxpm4 libxslt1.1 nginx nginx-common nginx-core 0 upgraded, 14 newly installed, 0 to remove and 19 not upgraded. Need to get 2,994 kB of archives. After this operation, 9,789 kB of additional disk space will be used. Do you want to continue? [Y/n] y Get:1 http://in.archive.ubuntu.com/ubuntu xenial/main amd64 libjpeg-turbo8 amd6 1.4.2-0ubuntu3 [111 kB] Get:2 http://in.archive.ubuntu.com/ubuntu xenial/main amd64 libxpm4 amd64 1:3.511-1 [33.1 kB] Get:3 http://in.archive.ubuntu.com/ubuntu xenial/main amd64 libjbig0 amd64 2.1-.1 [26.6 kB] Get:4 http://in.archive.ubuntu.com/ubuntu xenial/main amd64 fonts-dejavu-core al 2.35-1 [1,039 kB] Get:5 http://in.archive.ubuntu.com/ubuntu xenial/main amd64 fontconfig-config al 2.11.94-0ubuntu1 [49.9 kB]… … .. Setting up libjpeg-turbo8:amd64 (1.4.2-0ubuntu3) ... Setting up libxpm4:amd64 (1:3.5.11-1) ... Setting up libjbig0:amd64 (2.1-3.1) ... Setting up fonts-dejavu-core (2.35-1) ... Setting up fontconfig-config (2.11.94-0ubuntu1) ... Setting up libfontconfig1:amd64 (2.11.94-0ubuntu1) ... Setting up libjpeg8:amd64 (8c-2ubuntu8) ... Setting up libtiff5:amd64 (4.0.6-1) ... Setting up libvpx3:amd64 (1.5.0-2ubuntu1) ... Setting up libgd3:amd64 (2.1.1-4ubuntu0.16.04.3) ... Setting up libxslt1.1:amd64 (1.1.28-2.1) ... Setting up nginx-common (1.10.0-0ubuntu0.16.04.2) ... Setting up nginx-core (1.10.0-0ubuntu0.16.04.2) ... Setting up nginx (1.10.0-0ubuntu0.16.04.2) ... Processing triggers for libc-bin (2.23-0ubuntu3) ... Processing triggers for systemd (229-4ubuntu7) ... Processing triggers for ureadahead (0.100.0-19) ... Processing triggers for ufw (0.35-0ubuntu2) ...
配置防火墙以允许 Nginx
我们需要配置防火墙以允许 **Nginx** 服务,可以使用 **ufw** 允许 Nginx 访问。我们可以检查防火墙知道的应用程序配置列表,使用以下命令列出应用程序 –
$ sudo ufw app list Available applications: Nginx Full Nginx HTTP Nginx HTTPS OpenSSH
我们需要使用以下命令启用 Nginx
$ sudo ufw allow 'Nginx HTTP' Rules updated Rules updated (v6) root@ubuntu16:~# sudo ufw status Status: active To Action From -- ------ ---- Nginx HTTP ALLOW Anywhere OpenSSH ALLOW Anywhere OpenSSH (v6) ALLOW Anywhere (v6) Nginx HTTP (v6) ALLOW Anywhere (v6)
检查 Nginx Web 服务器
我们将使用以下命令检查 Nginx Web 服务器的状态
$ systemctl status nginxnginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: e Active: active (running) since Sun 2016-08-14 16:44:20 IST; 8min ago Main PID: 4983 (nginx) CGroup: /system.slice/nginx.service ├─4983 nginx: master process /usr/sbin/nginx -g daemon on; master_pr └─4984 nginx: worker process Aug 14 16:44:19 ubuntu16 systemd[1]: Starting A high performance web server and Aug 14 16:44:20 ubuntu16 systemd[1]: nginx.service: Failed to read PID from fil Aug 14 16:44:20 ubuntu16 systemd[1]: Started A high performance web server and lines 1-11/11 (END)
如我们从以上输出中看到的,Nginx 服务已成功启动并运行,我们可以在 Web 浏览器中通过 http://服务器的IP地址 打开 nginx。

管理 Nginx 进程
我们可以使用以下命令管理 Nginx Web 服务器
要停止 Nginx Web 服务器,请使用以下命令
$ sudo systemctl stop nginx
要启动 Nginx Web 服务器,请使用以下命令
$ sudo systemctl start nginx
要重新启动 Nginx Web 服务器,请使用以下命令
$ sudo systemctl restart nginx
要加载配置更改而不中断现有连接,我们可以使用以下命令
$ sudo systemctl reload nginx
要在启动时启动/停止 Nginx Web 服务器,我们可以使用以下命令
启动
$ sudo systemctl enable nginx
停止
$ sudo systemctl disable nginx
Nginx Web 服务器的重要文件和目录
文件位置
Nginx Web 服务器文档根目录的默认位置是 /var/www/html,我们可以通过更改配置文件来更改位置。
Nginx Web 服务器配置文件
/etc/nginx : The nginx configuration folders, as all the configuration files are located at this place. /etc/nginx/nginx.conf : This configuration file is the main files where we can change the global configuration of the Nginx Web Server. /etc/nginx/sites-available: This folder is for to configure the per site i.e., Server Blocks and the configuration files are found in the folders unless they are linked to sites-enabled folder. /etc/nginx/sites-enabled: This folders is used to enable the per-site i.e., Server blocks and links are created by using the files in the sites-available folder. /etc/nignx/snippets: Ths snipest folder contains the configuration included in the Nginx, these are repeatable configurations segments on refactoring into snippets.
Nginx Web 服务器日志位置
/var/log/nignx/access.log : All the request for the Nginx Web Server are recorded in the log file, untile we change the location in the Nginx configuration file. /var/log/nignx/error.log: Any Nginx Web Server errors are recorded in the log file.
在本文中,我们学习了如何配置 Nginx Web 服务器,如何更改配置,配置文件的位置以及如何通过 ufw 防火墙允许 Nginx Web 服务器,并且我们还有许多其他选项来服务技术。
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP