首页 > 代码库 > 源码安装Nginx

源码安装Nginx

1.下载源码,解压

[root@localhost ~]# tar -xzvf nginx-1.8.0.tar.gz

[root@localhost ~]# cd nginx-1.8.0

[root@localhost nginx-1.8.0]# ls
auto CHANGES.ru configure html man src
CHANGES conf contrib LICENSE README

一般源码安装前,应先查看一下README的内容

2.准备编译配置文件

解压文件中有configure的执行文件,指定目录

[root@localhost nginx-1.8.0]# ./configure --prefix=/usr/local/nginx

出现错误1

[root@localhost nginx-1.8.0]# ./configure --prefix=/usr/local/nginx
checking for OS
+ Linux 3.10.0-514.el7.x86_64 x86_64
checking for C compiler ... not found

./configure: error: C compiler cc is not found

错误原因:未找到C编译器

[root@localhost nginx-1.8.0]# yum install gcc gcc-c++ -y

出现错误2

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

错误原因:未安装pcre库

[root@localhost nginx-1.8.0]# yum install pcre pcre-devel -y

出现错误3

./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.

错误原因:gzip模块要求zlib库

[root@localhost nginx-1.8.0]# yum install zlib zlib-devel -y

成功

3.编译并安装

[root@localhost nginx-1.8.0]# make&&make install

测试

[root@localhost nginx-1.8.0]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.8.0

源码安装成功

4.为Nginx提供SysV init 脚本

[root@localhost nginx-1.8.0]# vim /usr/lib/systemd/system/nginx.service

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

保存退出

[root@localhost nginx-1.8.0]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@localhost nginx-1.8.0]# systemctl start nginx
[root@localhost nginx-1.8.0]# systemctl status nginx
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since 二 2017-07-11 10:00:43 CST; 7s ago
Docs: http://nginx.org/en/docs/
Process: 7504 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
Main PID: 7505 (nginx)
CGroup: /system.slice/nginx.service
├─7505 nginx: master process /usr/local/nginx/sbin/nginx
└─7506 nginx: worker process

7月 11 10:00:43 localhost.localdomain systemd[1]: Starting nginx - high per...
7月 11 10:00:43 localhost.localdomain systemd[1]: PID file /usr/local/nginx...
7月 11 10:00:43 localhost.localdomain systemd[1]: Started nginx - high perf...
Hint: Some lines were ellipsized, use -l to show in full.

Nginx源码安装完成

源码安装Nginx