概述
Nginx 是世界上最受欢迎的web服务器,许多大流量的主机都采用Nginx作为服务器。在大多数场景下作为web服务器的Nginx比Apache更加节省资源,它也可当作反向代理服务器。
本文主要介绍如何在ubuntu16.04上安装Nginx
前提条件
开始以前,你需要有一个安装好的ubuntu16.04,并且你需要有一个拥有sudo权限的非root普通用户。
第一步:安装Nginx
Ubuntu默认的源中就有Nginx,所以安装是比较简单的。
首先,更新apt源,以便软件是最新的,然后就可以安装nginx:
sudo apt-get update
sudo apt-get install nginx
执行这两个命令之后,apt-get就会安装好Nginx和它依赖的软件。
第二步:配置防火墙
开始测试Nginx前,我们需要配置防火墙,以便允许外界访问nginx服务。Nginx在安装的时候使用ufw注册自己作为一个服务,这样对nginx的访问就会变得很容易。
显示所有ufw应用的配置:
sudo ufw app list
你可以得到一个配置的输出列表:
Available applications:
Nginx Full
Nginx HTTP
Nginx HTTPS
OpenSSH
我们可以看到,有三个Nginx的配置:
- Nginx Full: 这个配置打开 80端口和443端口
- Nginx HTTP: 这个配置只打开80 (普通, 未加密通信)
- Nginx HTTPS: 这个配置只打开 443 (TLS/SSL 加密通信 )
一般来说我们应该配置最严的限制,因为本文我们还没有配置SSL,所以我们只打开80端口。
我们执行:
sudo ufw allow 'Nginx HTTP'
验证修改状态:
sudo ufw status
我们可以看到HTTP是被打开的:
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Nginx HTTP ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Nginx HTTP (v6) ALLOW Anywhere (v6)
第三步: 检查你的web server
安装完成后,Ubuntu 16.04 会自动启动 Nginx. 我们可以使用systemd
检查运行状态:
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: <span class="highlight">active (running)</span> since Mon 2016-04-18 16:14:00 EDT; 4min 2s ago
Main PID: 12857 (nginx)
CGroup: /system.slice/nginx.service
├─12857 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
└─12858 nginx: worker process
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: <span class="highlight">active (running)</span> since Mon 2016-04-18 16:14:00 EDT; 4min 2s ago
Main PID: 12857 (nginx)
CGroup: /system.slice/nginx.service
├─12857 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
└─12858 nginx: worker process
服务已经正常启动,当然最好的确认方法是通过访问web页面的方式。
如果我们能访问到默认加载页就证明启动成功了。
如果你不知道服务器的ip可以使用如下命令:
ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'
有了IP之后,在浏览器里输入:
http://server_domain_or_IP
你就能看到Nginx的默认加载页了:
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.
第四步: 管理 Nginx 进程
现在我们已经有nginx在运行了,我们可以再试一些管理命令:
停止nginx:
sudo systemctl stop nginx
启动nginx:
sudo systemctl start nginx
重启nginx:
sudo systemctl restart nginx
修改配置文件后,平滑加载配置命令(不会断开用户访问):
sudo systemctl reload nginx
默认,nginx是随着系统启动的时候自动运行。如果你不想开机启动,那么你可以禁止nginx开机启动:
sudo systemctl disable nginx
重新配置nginx开机自动启动:
sudo systemctl enable nginx
第五步: 熟悉Nginx的文件和目录
现在我们已经管理nginx了,接下来可以熟悉一下nginx的目录结构和一些重要的文件:
网站文件位置
/var/www/html
: 网站文件存放的地方, 默认只有我们上面看到nginx页面,可以通过改变nginx配置文件的方式来修改这个位置。
服务器配置
/etc/nginx
: nginx配置文件目录。所有的nginx配置文件都在这里。/etc/nginx/snippets
: 这个目录主要可以包含在其它nginx配置文件中的配置片段。重复的配置都可以重构为配置片段。/etc/nginx/sites-enabled/
: 这个目录存储生效的 "server blocks" 配置. 通常,这个配置都是链接到sites-available目录中的配置文件。
/etc/nginx/sites-available/
: 这个目录存储每一个网站的"server blocks"。nginx通常不会使用这些配置,除非它们陪连接到sites-enabled
目录 (see below)。一般所有的server block 配置都在这个目录中设置,然后软连接到别的目录 。/etc/nginx/nginx.conf
: Nginx的主配置文件. 可以修改他来改变nginx的全局配置。
日志文件
/var/log/nginx/access.log
: 每一个访问请求都会记录在这个文件中,除非你做了其它设置。/var/log/nginx/error.log
: 任何Nginx的错误信息都会记录到这个文件中。
第六步:修改Nginx web服务器默认目录
1. 在 /etc/nginx/conf路径下添加自己的.conf文件例如:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/local/iotserver/server/iotscs; #改成自己的项目路径
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
2.修改/etc/nginx路径下的 nginx.conf,注释掉http下的最后一行(不然默认还是会启动nginx默认页面)。
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
#include /etc/nginx/sites-enabled/*;
3.重启nginx
--完--