▪ 环境
操作系统:Ubuntu 16
软件版本:Nginx 1.13
▪ 安装
1. 基础软件
通过 sudo apt-cache search all | grep [name]
的方式验证以下软件包是否已全部安装
openssl libssl-dev libxml2 libxml2-dev libxslt-dev libperl-dev
如果缺少相关的软件包,可通过 sudo apt-get install [name]
的方式在线安装。
如果遇到部分依赖库版本不对导致不能安装时,建议将 apt-get 源恢复为系统默认的源试试。
2. 下载 Nginx 源码包
$ cd /usr/local/src/
$ sudo wget http://mirrors.sohu.com/nginx/nginx-1.13.6.tar.gz
$ sudo tar zxvf nginx-1.13.6.tar.gz
$ cd nginx-1.13.6
3. 创建用户、用户组和目录
$ sudo groupadd nginx
$ sudo useradd -M -s /bin/false -g nginx nginx
$ sudo mkdir /var/log/nginx
$ sudo chown nginx:nginx -R /var/log/nginx
$ sudo mkdir /var/runtime/nginx
$ sudo chown nginx:nginx -R /var/runtime/nginx
4. 下载扩展包,以便 Nginx 支持 Rewrite 和 Gzip 功能
$ cd /usr/local/src/nginx-1.13.6
$ sudo wget http://sourceforge.net/projects/pcre/files/pcre/8.37/pcre-8.37.tar.gz
$ sudo tar zxvf pcre-8.37.tar.gz
$ sudo wget http://www.zlib.net/zlib-1.2.11.tar.gz
$ sudo tar zxvf zlib-1.2.11.tar.gz
5. 安装 NGINX
$ sudo ./configure \
--prefix=/usr/local/nginx \
--user=nginx --group=nginx \
--pid-path=/var/runtime/nginx/nginx.pid \
--lock-path=/var/runtime/nginx/nginx.lock \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log \
--with-debug --with-http_dav_module --with-http_flv_module --with-http_ssl_module \
--with-http_xslt_module --with-http_realip_module --with-http_addition_module --with-http_stub_status_module \
--with-http_sub_module --with-http_random_index_module --with-http_degradation_module --with-http_secure_link_module \
--with-file-aio --with-mail --with-mail_ssl_module --with-http_gzip_static_module --with-http_perl_module \
--with-pcre=pcre-8.37 --with-zlib=zlib-1.2.11 \
--with-ld-opt="-Wl,-E"
--http-scgi-temp-path=/var/runtime/nginx/temp_scgi \
--http-uwsgi-temp-path=/var/runtime/nginx/temp_uwsgi --http-proxy-temp-path=/var/runtime/nginx/temp_proxy \
--http-fastcgi-temp-path=/var/runtime/nginx/temp_fastcgi --http-client-body-temp-path=/var/runtime/nginx/temp_client_body
$ sudo make && make install
7. 配置开机自启动
由于 Ubuntu 16 使用 systemd 替换了 SysV, 所以 Ubuntu 16 之前的 service 和 chkconfig 开机自启动将不在适用 Ubuntu 16。下面将介绍如何配置 Nginx 开机自启动。
新建服务文件:
$ sudo vim /usr/lib/systemd/system/nginx.service
添加服务代码:
[Unit]
Description=nginx
After=network.target
[Service]
# service type
Type=forking
# main service
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
# use private tmp
PrivateTmp=true
[Install]
WantedBy=multi-user.target
启用服务文件:
$ sudo systemctl enable nginx.service
8. 相关命令
$ sudo systemctl stop nginx.service
$ sudo systemctl start nginx.service
$ sudo systemctl status nginx.service
▪ 配置虚拟主机
1. 创建网站目录
$ sudo mkdir /data/httpd/htdocs
$ sudo mkdir /data/httpd/htdocs/xxxx
2. 创建虚拟网站配置目录
$ sudo vim /usr/local/nginx/conf/nginx.conf
启用虚拟主机模式:
移除 server{ ... } 段的全部代码
在移除的代码处增加:
include /usr/local/nginx/conf/vhosts/*.conf;
完整的配置文件代码如下:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
# upload limit
client_max_body_size 100m;
# virtual host
include /usr/local/nginx/conf/vhosts/*.conf;
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
$ sudo mkdir /usr/local/nginx/conf/vhosts
$ cd /usr/local/nginx/conf/vhosts
增加默认 default.conf
配置,内容如下:
# 禁止未绑定的域名和IP访问
server {
listen 80 default;
server_name _;
return 444;
}
增加自定义虚拟主机 xxxx.conf
配置,内容如下:
# 允许 xxxx.com 域名访问
server {
listen 80;
server_name xxxx.com *.xxxx.com;
root /data/httpd/htdocs/xxxx;
access_log /var/log/nginx/access.xxxx.log;
location / {
index index.html index.htm index.php;
# include /data/httpd/htdocs/xxxx/.nginx;
}
# Deny Hidden File
# Like:.nginx .htaccess .svn
location ~ /\. {
deny all;
}
# PHP Script
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}