如何在Debian Linux 9上安装和设置Nginx
如何在云上运行的Debian Linux 9服务器上安装和配置Nginx Web服务器?
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 2016-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:测试
打开浏览器并输入url:
http://your-domain/
或者使用ip命令/ifconfig命令找到您的公共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)
打开浏览器并输入url:
http://45.33.115.105/
步骤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.theitroad.local域创建一个名为cbzwww的用户:
$ sudo useradd -c "theitroad.local user" -d /home/nginx-www.theitroad.local -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.theitroad.local
要创建名为www.theitroad.local的虚拟主机,请输入;
$ sudo nano /etc/nginx/sites-available/www.theitroad.local.conf
追加基本配置:
server { listen 80; listen [::]:80; server_name www.theitroad.local; root /home/nginx-www.theitroad.local; index index.html; location / { try_files $uri $uri/ =404; } }
保存并关闭文件。
您必须创建一个软链接,如下所示:
$ sudo ln -s /etc/nginx/sites-available/www.theitroad.local.conf /etc/nginx/sites-enabled/www.theitroad.local.conf
最后测试并重新加载服务器:
$ sudo nginx -t $ sudo systemctl reload nginx
现在在以下位置创建一个测试文件
$ sudo nano /home/nginx-www.theitroad.local/index.html
追加以下内容:
<!DOCTYPE html> <html> <head> <title>Welcome to theitroad!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to www.theitroad.local!</h1> <p>This is a test for Nginx virtual host.</p> <p><em>Thank you for using theitroad tutorials.</em></p> </body> </html>
保存并关闭文件。
使用chmod命令和chown命令设置正确的权限:
$ sudo chown cbzwww:cbzwww /home/nginx-www.theitroad.local/index.html $ sudo chmod 0444 /home/nginx-www.theitroad.local/index.html $ ls -l /home/nginx-www.theitroad.local/index.html `-r--r--r-- 1 cbzwww cbzwww 374 Aug 3 10:52 /home/nginx-www.theitroad.local/index.html`
通过执行url进行测试:
http://www.theitroad.local/