更新系统安装包列表

1
apt update

必须软件包

下面都是编译nginx必须的,提前安装好。

1
2
3
4
apt install gcc make
apt install libpcre3 libpcre3-dev //【正则表达式库】官网http://www.pcre.org/
apt install openssl libssl-dev //【openssl库】官网https://www.openssl.org/
apt install zlib1g-dev

创建需要使用的目录

创建目录sources和web,分别用来放源码和编译后的文件。

1
2
mkdir /sources/
mkdir /web/

安装nginx

命令流程:

1
2
3
4
5
6
cd /sources/
wget http://nginx.org/download/nginx-1.13.5.tar.gz
tar -zxf nginx-1.13.5.tar.gz
cd nginx-1.13.5
./configure --prefix=/web/nginx --with-http_ssl_module
make && make install
--with-http_ssl_module   //打开https

打开nginx.conf

1
vim nginx.conf

支持php。在server里加入

复制代码

1
2
3
4
5
6
7
8
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
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;
}

支持asp.net。在server里加入

复制代码

1
2
3
4
5
6
7
8
9
10
location / {
proxy_pass http://127.0.0.1:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_bypass $http_upgrade;
}

防火墙添加80、443端口(可选,需要外网访问的)

1
2
firewall-cmd --zone=public --add-port=80/tcp --permanent    //--permanent永久生效,没有此参数重启后失效
firewall-cmd --reload //重新载入

附、常用命令

nginx常用命令

1
2
3
4
ln -s /web/nginx/sbin/nginx /usr/local/bin        //快捷方式
/web/nginx/sbin/nginx //启动nginx
/web/nginx/sbin/nginx -s reload //重启nginx
/web/nginx/sbin/nginx -s stop //关闭nginx