如何在 Debian Linux 9 上安装和设置 Nginx
Nginx 是一个免费的开源 HTTP/HTTPS Web 服务器。它也可以用作代理服务器。Nginx 用作 HTTP、HTTPS 和其他协议的反向代理服务器。您可以使用 Nginx 代替传统的 Apache HTTP 2 服务器。本教程向您展示如何在 Debian 9 服务器上安装和设置 Nginx Web 服务器。
步骤 1:安装 Nginx HTTP 服务器
输入以下apt-get 命令/ apt 命令来更新您的系统:
示例输出:
$ sudo apt-get update
$ sudo apt-get upgrade
Reading package lists... Done Building dependency tree Reading state information... Done Calculating upgrade... Done The following packages will be upgraded: libdns-export162 libisc-export160 2 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. Need to get 1186 kB of archives. After this operation, 0 B of additional disk space will be used. Do you want to continue? [Y/n] y Get:1 http://security.debian.org stretch/updates/main amd64 libisc-export160 amd64 1:9.10.3.dfsg.P4-12.3+deb9u2 [334 kB] Get:2 http://security.debian.org stretch/updates/main amd64 libdns-export162 amd64 1:9.10.3.dfsg.P4-12.3+deb9u2 [851 kB] Fetched 1186 kB in 0s (3744 kB/s) debconf: delaying package configuration, since apt-utils is not installed (Reading database ... 8419 files and directories currently installed.) Preparing to unpack .../libisc-export160_1%3a9.10.3.dfsg.P4-12.3+deb9u2_amd64.deb ... Unpacking libisc-export160 (1:9.10.3.dfsg.P4-12.3+deb9u2) over (1:9.10.3.dfsg.P4-12.3+deb9u1) ... Preparing to unpack .../libdns-export162_1%3a9.10.3.dfsg.P4-12.3+deb9u2_amd64.deb ... Unpacking libdns-export162 (1:9.10.3.dfsg.P4-12.3+deb9u2) over (1:9.10.3.dfsg.P4-12.3+deb9u1) ... Setting up libisc-export160 (1:9.10.3.dfsg.P4-12.3+deb9u2) ... Setting up libdns-export162 (1:9.10.3.dfsg.P4-12.3+deb9u2) ... Processing triggers for libc-bin (2.24-11+deb9u1) ...
要安装 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 geoip-database libexpat1 libfontconfig1 libfreetype6 libgd3 libgeoip1 libicu57 libjbig0 libjpeg62-turbo libnginx-mod-http-auth-pam libnginx-mod-http-dav-ext libnginx-mod-http-echo libnginx-mod-http-geoip libnginx-mod-http-image-filter libnginx-mod-http-subs-filter libnginx-mod-http-upstream-fair libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream libpng16-16 libssl1.1 libtiff5 libwebp6 libx11-6 libx11-data libxau6 libxcb1 libxdmcp6 libxml2 libxpm4 libxslt1.1 nginx-common nginx-full sgml-base xml-core Suggested packages: libgd-tools geoip-bin fcgiwrap nginx-doc ssl-cert sgml-base-doc debhelper The following NEW packages will be installed: fontconfig-config fonts-dejavu-core geoip-database libexpat1 libfontconfig1 libfreetype6 libgd3 libgeoip1 libicu57 libjbig0 libjpeg62-turbo libnginx-mod-http-auth-pam libnginx-mod-http-dav-ext libnginx-mod-http-echo libnginx-mod-http-geoip libnginx-mod-http-image-filter libnginx-mod-http-subs-filter libnginx-mod-http-upstream-fair libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream libpng16-16 libssl1.1 libtiff5 libwebp6 libx11-6 libx11-data libxau6 libxcb1 libxdmcp6 libxml2 libxpm4 libxslt1.1 nginx nginx-common nginx-full sgml-base xml-core 0 upgraded, 38 newly installed, 0 to remove and 0 not upgraded. Need to get 18.5 MB of archives. After this operation, 58.9 MB of additional disk space will be used. Do you want to continue? [Y/n] y Get:1 http://security.debian.org stretch/updates/main amd64 libexpat1 amd64 2.2.0-2+deb9u1 [83.4 kB] Get:2 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 libxau6 amd64 1:1.0.8-1 [20.7 kB] .... .. .. Setting up libxpm4:amd64 (1:3.5.12-1) ... Setting up libgd3:amd64 (2.2.4-2) ... Setting up libnginx-mod-http-image-filter (1.10.3-1+deb9u1) ... Setting up nginx-full (1.10.3-1+deb9u1) ... Setting up nginx (1.10.3-1+deb9u1) ... Processing triggers for libc-bin (2.24-11+deb9u1) ... Processing triggers for sgml-base (1.29) ...
默认情况下,Nginx 服务器在 Debian 9 上启动,可以使用 netstat 命令/ss 命令和 ps 命令进行验证,如下所示:
$ ps aux | grep nginx
$ pgrep nginx
www-data 1099 0.0 0.0 159836 3100 ? S 10:11 0:00 nginx: worker process www-data 1100 0.0 0.0 159836 3100 ? S 10:11 0:00 nginx: worker process www-data 1101 0.0 0.0 159836 3100 ? S 10:11 0:00 nginx: worker process
使用如下 netstat 命令验证端口 80 是否打开:
$ netstat -tulpn | grep :80
示例输出:
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1098/nginx: master tcp6 0 0 :::80 :::* LISTEN 1098/nginx: master
第 2 步:启动/停止/重启 nginx 命令
要在启动时启用 Nginx,请运行以下 systemctl 命令:
$ sudo systemctl enable nginx
Synchronizing state of nginx.service with SysV service script with /lib/systemd/systemd-sysv-install. Executing: /lib/systemd/systemd-sysv-install enable nginx
重启nginx服务器命令
$ sudo systemctl restart nginx
停止nginx服务器命令
$ sudo systemctl stop nginx
启动nginx服务器命令
$ sudo systemctl start nginx
查找 nginx 服务器命令的状态
$ sudo systemctl status nginx
示例输出:
* 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 Thu 2017-08-03 10:18:49 UTC; 32s ago Docs: man:nginx(8) Process: 1361 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS) Process: 1363 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Process: 1362 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Main PID: 1364 (nginx) Tasks: 21 (limit: 4915) CGroup: /system.slice/nginx.service ??1364 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; ??1365 nginx: worker process ??1366 nginx: worker process ??1367 nginx: worker process ??1368 nginx: worker process ??1369 nginx: worker process ??1370 nginx: worker process ??1371 nginx: worker process ??1372 nginx: worker process ??1373 nginx: worker process ??1374 nginx: worker process ??1375 nginx: worker process ??1376 nginx: worker process ??1377 nginx: worker process ??1378 nginx: worker process ??1379 nginx: worker process ??1380 nginx: worker process ??1381 nginx: worker process ??1382 nginx: worker process ??1383 nginx: worker process ??1384 nginx: worker process Aug 03 10:18:49 debian9-www systemd[1]: Starting A high performance web server and a reverse proxy server... Aug 03 10:18:49 debian9-www systemd[1]: Started A high performance web server and a reverse proxy server.
步骤 3:测试
启动 Web 浏览器并输入 url:或者使用ip 命令/ifconfig 命令
http://your-domain/
查找你的公共 IP 地址:
或者
示例输出:
$ ip a
$ ifconfig eth0
eth0 Link encap:Ethernet HWaddr f2:3c:91:60:1d:cb
inet addr:45.33.115.105 Bcast:45.33.115.255 Mask:255.255.255.0
inet6 addr: 2600:3c00::f03c:91ff:fe60:1dcb/64 Scope:Global
inet6 addr: fe80::f03c:91ff:fe60:1dcb/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:152860323 errors:0 dropped:0 overruns:0 frame:0
TX packets:194050118 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:555971036475 (555.9 GB) TX bytes:107798769402 (107.7 GB)
启动 Web 浏览器并输入 url:
http://45.33.115.105/
示例输出:
图 01:在 Debian 9 上测试你的 Nginx Web 服务器
步骤 4:配置 Nginx Web 服务器
默认情况下,所有 HTML/css/images 文件都存储在 /var/www/html/ 目录中。这被称为 Web 服务器的根目录。它在 /etc/nginx/sites-enabled/default 配置文件中定义。您可以使用 vim 命令/nano 命令编辑它,
$ sudo vi /etc/nginx/sites-enabled/default
或者
$ sudo nano /etc/nginx/sites-enabled/default
/etc/nginx/nginx.conf 是您的主要 nginx 配置文件,/etc/nginx/sites-enabled/default 是默认配置文件。
$ sudo nano /etc/nginx/nginx.conf
创建自己的配置文件
首先使用 useradd 命令为 www.example.com 域创建一个名为 cbzwww 的用户:
$ sudo useradd -c "example.com user" -d /home/nginx-www.example.com -s /usr/sbin/nologin -m cbzwww
要锁定 cbzwww 帐户的密码,请输入:
验证:
示例输出:
$ sudo passwd -l cbzwww
passwd: password expiry information changed.
$ ls -l /home/
total 4 drwxr-xr-x 2 cbzwww cbzwww 4096 Aug 3 10:37 nginx-www.example.com
要创建名为 www.example.com 的虚拟主机,请输入;
$ sudo nano /etc/nginx/sites-available/www.example.com.conf
附加基本配置:
server { listen 80; listen [::]:80; server_name www.example.com; root /home/nginx-www.example.com; index index.html; location / { try_files $uri $uri/ =404; } }
保存并关闭文件。您必须按如下方式创建软链接:
$ sudo ln -s /etc/nginx/sites-available/www.example.com.conf /etc/nginx/sites-enabled/www.example.com.conf
最后测试并重新加载服务器:现在在
附加以下内容处
创建一个测试文件:
$ sudo nginx -t
$ sudo systemctl reload nginx
$ sudo nano /home/nginx-www.example.com/index.html
<!DOCTYPE html> <html> <head> <title>Welcome to example!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to www.example.com!</h1> <p>This is a test for Nginx virtual host.</p> <p><em>Thank you for using example tutorials.</em></p> </body> </html>
保存并关闭文件。使用 chmod 命令和 chown 命令设置正确的权限:
通过输入 url 进行测试:
示例输出:
$ sudo chown cbzwww:cbzwww /home/nginx-www.example.com/index.html
$ sudo chmod 0444 /home/nginx-www.example.com/index.html
$ ls -l /home/nginx-www.example.com/index.html
-r--r--r-- 1 cbzwww cbzwww 374 Aug 3 10:52 /home/nginx-www.example.com/index.html
http://www.example.com/
图 2:Nginx Web 服务器虚拟域测试
至此,您已经在 Debian 9 服务器上安装并配置了 Nginx 服务器。