在linux中搭建nginx环境

本文记录一下如何在linux中搭建nginx环境。


1.依赖包安装

1
2
3
4
yum install gcc-c++
yum install pcre pcre-devel
yum install zlib zlib-devel
yum install openssl openssl--devel

2.nginx安装

安装之前,最好检查一下是否已经安装有nginx

1
find -name nginx

如果系统已经安装了nginx,那么就先卸载

1
yum remove nginx

正式开始安装

1
2
3
4
5
6
wget http://nginx.org/download/nginx-1.7.4.tar.gz(推荐最新版)
tar -zxvf nginx-1.7.4.tar.gz
cd nginx-1.7.4
./configure $默认安装在/usr/local/nginx
make
make install

3.配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
server {
listen 81;
server_name localhost;
location /{
root /webroot/www/accountbook_web/;
index index.html;
try_files $uri $uri/ /index.html =404;
}
location /accountbook{
proxy_pass http://localhost;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

4.启动

1
2
cd /usr/local/nginx/sbin
./nginx

5.修改配置文件后重新加载命令

1
./nginx -s reload

附:

nginx -s signal其中,信号可以是下列之一:

1
2
3
4
stop — fast shutdown
quit — graceful shutdown
reload — reloading the configuration file
reopen — reopening the log files