0%

CentOS7源码编译安装、配置nginx

安装依赖

安装gcc-c++,安装pcre, pcre-devel(会依赖pcre),安装zlib, zlib-devel和openssl,openssl-devel

1
yum install -y gcc-c++, pcre, pcre-devel, zlib, zlib-devel, openssl, openssl-devel

下载nginx源码包

在官网上下载Stable version,稳定版本。http://nginx.org/en/download.html

目前最新的稳定版本是: nginx-1.14.0 http://nginx.org/download/nginx-1.14.0.tar.gz

1
2
3
4
cd /usr/local
wget http://nginx.org/download/nginx-1.14.0.tar.gz

tar -zxvf nginx-1.14.0.tar.gz

编译安装

1
2
3
4
cd nginx-1.14.0
./configure

make && make install

启动nginx

1
2
3
4
5
6
7
8
whereis nginx
nginx: /usr/local/nginx
cd /usr/local/nginx
sbin/nginx

ps -ef | grep nginx
root 20240 1 0 11:09 ? 00:00:00 nginx: master process sbin/nginx
nobody 20241 20240 0 11:09 ? 00:00:00 nginx: worker process

访问http://192.168.1.30,如果在本机访问,则访问http://localhost,出现如下图片,表明已经安装成功。

配置nginx

1
2
cd conf
vi nginx.conf

添加一条记录,增加虚拟主机的配置

1
include vhost/*.conf;

并新建一个vhost目录,用于放置*.conf文件

1
mkdir vhost

添加一个虚拟主机

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
upstream web {
server localhost:8080;
}
server {
listen 80;
server_name domain.com www.domain.com;

location / {
proxy_pass http://web;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

}

重新加载配置文件

1
sbin/nginx -s reload

这样,通过访问域名domain.com或www.domain.com的请求,就可以转发localhost:8080上了