跳到主要内容

源码安装

linux安装

在Centos下,yum源不提供nginx的安装,可以通过切换yum源的方法获取安装。也可以通过直接下载安装包的方法,以下命令均需root权限执行:

首先安装必要的库(nginx 中gzip模块需要 zlib 库,rewrite模块需要 pcre 库,ssl 功能需要openssl库)。选定/usr/local为安装目录,以下具体版本号根据实际改变。


安装PCRE库

$ cd /usr/local/

$ wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.36.tar.gz

$ tar -zxvf pcre-8.36.tar.gz

$ cd pcre-8.36

$ ./configure

$ make

$ make install

./configure报错

configure: error: You need a C++ compiler for C++ support.

解决办法

yum install -y gcc gcc-c++

安装zlib库

$ cd /usr/local/

$ wget http://zlib.net/zlib-1.2.8.tar.gz

$ tar -zxvf zlib-1.2.8.tar.gz

$ cd zlib-1.2.8

$ ./configure

$ make

$ make install

安装ssl

$ cd /usr/local/

$ wget http://www.openssl.org/source/openssl-1.0.1j.tar.gz

$ tar -zxvf openssl-1.0.1j.tar.gz

$ ./config

$ make

$ make install

安装nginx

$ cd /usr/local/

$ wget http://nginx.org/download/nginx-1.8.0.tar.gz

$ tar -zxvf nginx-1.8.0.tar.gz

$ cd nginx-1.8.0


#配置nginx
#--prefix 指定安装的目录
#/usr/local/nginx 是安装目录,不能和自己下载的文件目录重了
#./configure --prefix=/usr/local/nginx

#带ssl stub_status模块 添加strem模块 –with-stream,这样就能传输tcp协议了
#http_stub_status_module 状态监控
#http_ssl_module 配置https
#stream 配置tcp得转发
#http_gzip_static_module 压缩
#http_sub_module 替换请求

$ ./configure --prefix=/usr/local/nginx

## ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gunzip_module --with-http_gzip_static_module

$ make

$ make install

添加用户

useradd nginx


安装常见错误:

Nginx启动提示找不到libpcre.so.1解决方法

如果是32位系统

[root@lee ~]\# ln -s /usr/local/lib/libpcre.so.1 /lib

如果是64位系统

[root@lee ~]\# ln -s /usr/local/lib/libpcre.so.1 /lib64

然后在启动nginx就OK了

[root@lee ~]\# /usr/local/webserver/nginx/sbin/nginx


vim /usr/lib/systemd/system/nginx.service


[Unit]

Description=The nginx HTTP and reverse proxy server

After=network-online.target remote-fs.target nss-lookup.target

Wants=network-online.target

[Service]

Type=forking

PIDFile=/run/nginx.pid

# Nginx will fail to start if /run/nginx.pid already exists but has the wrong

# SELinux context. This might happen when running `nginx -t` from the cmdline.

# https://bugzilla.redhat.com/show_bug.cgi?id=1268621

ExecStartPre=/usr/bin/rm -f /run/nginx.pid

ExecStartPre=/usr/sbin/nginx -t

ExecStart=/usr/sbin/nginx

ExecReload=/usr/sbin/nginx -s reload

KillSignal=SIGQUIT

TimeoutStopSec=5

KillMode=process

PrivateTmp=true

[Install]

WantedBy=multi-user.target