Ubuntu系统下安装Nginx服务器的方法


Nginx简介

Nginx是一个非常轻量级的HTTP服务器,Nginx,它的发音为“engine x”,是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP 代理服务器。

Nginx安装

系统环境

操作系统 :ubuntu-16.04(64)

在线安装

首先因为我并不是项目应用,而且也不是很频繁的使用Linux系统,所以选择更容易上手的Ubuntu系统作为本文Demo的对象。如果选择Debian等Linux系统或者使用源代码安装,在安装Nginx服务器之前需要手动安装依赖的库,但是在Ubuntu系统下,使用在线安装相对来说简单一点。在这里要提醒一下,在线安装和源代码安装的一些命令行和安装位置是不一样的,这个需要具体对待。

使用apt-get在线安装命令:

1
$sudo apt-get install nginx

nginx安装

从上图我们可以看出,安装的Nginx的版本是1.10.0。Ubuntu安装Nginx之后的文件结构大致如下表:

文件 路径
配置文件 /etc/nginx
虚拟主机 /etc/nginx/sites-available
启动程序 /usr/sbin/nginx
日志 /var/log/nginx
nginx启动脚本 /etc/init.d/
默认虚拟主机 /usr/share/nginx/www

启动Nginx

在线安装的启动过程

1
$sudo /etc/init.d/nginx start

如果出现[ ok ] Starting nginx (via systemctl): nginx.service.的提示,说明Nginx服务器启动成功,这个时候我们在浏览器中访问一下http://localhost/,正常情况下会出现Nginx的欢迎页面如下图。

常用Nginx命令

启动Nginx服务器

1
$sudo /etc/init.d/nginx start

关闭Nginx服务器

1
sudo service nginx stop

重启Nginx服务器

1
sudo service nginx restart

重新加载配置文件

1
nginx -s reload

检查配置文件是否出错

1
nginx -t

Nginx配置

主要配置文件

通过上面的表格我们知道Nginx的配置文件是/etc/nginx/nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}

其中设置了一些必要的参数,我们发现62行有这样的语句include /etc/nginx/sites-enabled/*.可以看出/etc/nginx/sites-enabled/default文件也是一个核心的配置文件,其中包含了服务器跟目录、服务器名称、location信息和server信息等配置信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php7.0-fpm:
# fastcgi_pass unix:/run/php/php7.0-fpm.sock;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
# listen 80;
# listen [::]:80;
#
# server_name example.com;
#
# root /var/www/example.com;
# index index.html;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}

参考&引用

nginx documentation
Ubuntu中Nginx的安装与配置
Ubuntu下安装nginx的步骤分享

更新时间

发布时间 : 2016-08-20

看你的了!