以前都是靠各种一键脚本来安装 nginx,今天我尝试一下从源码手动编译安装 nginx,这里记录一下过程,有不足之处欢迎指正。废话不多说,直接开始。

系统环境

[root@hwc ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)

环境需要

编译安装 nginx 需要 gccpcre-develzlib-developensslopenssl-devel

yum -y install gcc pcre-devel zlib-devel openssl openssl-devel

安装

首先下载安装包,这里下载的是nginx-1.9.9,最新版请去官方下载站获取。这里我下载到了/usr/local文件夹内

cd /usr/local
wget https://nginx.org/download/nginx-1.9.9.tar.gz

解压刚刚下载的文件。然后进入解压出来的文件夹内

tar -zxvf nginx-1.9.9.tar.gz
cd /usr/local/nginx-1.9.9

配置安装参数,使用https协议模块

./configure --with-http_ssl_module

之后会开始执行配置文件进行安装参数配置,配置结束会出现配置总结,如下

Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + md5: using OpenSSL library
  + sha1: using OpenSSL library
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

细心的同学会发现,如果配置参数不加--with-http_ssl_module,则配置总结中会出现一句话+ OpenSSL library is not used,这说明https协议模块使用了OpenSSL库。 具体其他的可选配置参数及作用如下

--prefix=path 定义一个目录,存放服务器上的文件 ,也就是nginx的安装目录。默认使用 /usr/local/nginx。
--sbin-path=path 设置nginx的可执行文件的路径,默认为 prefix/sbin/nginx.
--conf-path=path 设置在nginx.conf配置文件的路径。nginx允许使用不同的配置文件启动,通过命令行中的-c选项。默认为prefix/conf/nginx.conf.
--pid-path=path 设置nginx.pid文件,将存储的主进程的进程号。安装完成后,可以随时改变的文件名 , 在nginx.conf配置文件中使用 PID指令。默认情况下,文件名 为prefix/logs/nginx.pid.
--error-log-path=path 设置主错误,警告,和诊断文件的名称。安装完成后,可以随时改变的文件名 ,在nginx.conf配置文件中 使用 的error_log指令。默认情况下,文件名 为prefix/logs/error.log.
--http-log-path=path 设置主请求的HTTP服务器的日志文件的名称。安装完成后,可以随时改变的文件名 ,在nginx.conf配置文件中 使用 的access_log指令。默认情况下,文件名 为prefix/logs/access.log.
--user=name 设置nginx工作进程的用户。安装完成后,可以随时更改的名称在nginx.conf配置文件中 使用的 user指令。默认的用户名是nobody。
--group=name 设置nginx工作进程的用户组。安装完成后,可以随时更改的名称在nginx.conf配置文件中 使用的 user指令。默认的为非特权用户。
--with-select_module --without-select_module 启用或禁用构建一个模块来允许服务器使用select()方法。该模块将自动建立,如果平台不支持的kqueue,epoll,rtsig或/dev/poll。
--with-poll_module --without-poll_module 启用或禁用构建一个模块来允许服务器使用poll()方法。该模块将自动建立,如果平台不支持的kqueue,epoll,rtsig或/dev/poll。
--without-http_gzip_module — 不编译压缩的HTTP服务器的响应模块。编译并运行此模块需要zlib库。
--without-http_rewrite_module 不编译重写模块。编译并运行此模块需要PCRE库支持。
--without-http_proxy_module — 不编译http_proxy模块。
--with-http_ssl_module — 使用https协议模块。默认情况下,该模块没有被构建。建立并运行此模块的OpenSSL库是必需的。
--with-pcre=path — 设置PCRE库的源码路径。PCRE库的源码(版本4.4 - 8.30)需要从PCRE网站下载并解压。其余的工作是Nginx的./ configure和make来完成。正则表达式使用在location指令和 ngx_http_rewrite_module 模块中。
--with-pcre-jit —编译PCRE包含“just-in-time compilation”(1.1.12中, pcre_jit指令)。
--with-zlib=path —设置的zlib库的源码路径。要下载从 zlib(版本1.1.3 - 1.2.5)的并解压。其余的工作是Nginx的./ configure和make完成。ngx_http_gzip_module模块需要使用zlib 。
--with-cc-opt=parameters — 设置额外的参数将被添加到CFLAGS变量。例如,当你在FreeBSD上使用PCRE库时需要使用:–with-cc-opt="-I /usr/local/include。.如需要需要增加 select()支持的文件数量:–with-cc-opt="-D FD_SETSIZE=2048".
--with-ld-opt=parameters —设置附加的参数,将用于在链接期间。例如,当在FreeBSD下使用该系统的PCRE库,应指定:–with-ld-opt="-L /usr/local/lib".

配置完成后,目录里会有一个Makefile文件,这个文件是后面编译安装会用到的。查看Makefile文件内容如下

default:	build

clean:
	rm -rf Makefile objs

build:
	$(MAKE) -f objs/Makefile
	$(MAKE) -f objs/Makefile manpage

install:
	$(MAKE) -f objs/Makefile install

upgrade:
	/usr/local/nginx/sbin/nginx -t

	kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
	sleep 1
	test -f /usr/local/nginx/logs/nginx.pid.oldbin

	kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`

可以看到Makefile文件内写好了四个功能,分别是cleanbuildinstallupgrade。开头第一句表示默认执行build功能。 编译安装

make
make install

然后查看是否安装成功,需要找到nginx的可执行文件,默认路径为/usr/local/nginx/sbin/nginx(可通过编译前更改配置参数来更改,具体看上文)。正常情况下输出如下

[root@hwc sbin]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

启动nginx

/usr/local/nginx/sbin/nginx

查看是否启动成功

[root@hwc sbin]# ps -ef | grep nginx
root     23072     1  0 15:16 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody   23073 23072  0 15:16 ?        00:00:00 nginx: worker process
root     23075  8736  0 15:16 pts/0    00:00:00 grep --color=auto nginx

启动成功,在浏览器输入本机ip,则会出现nginx的欢迎页。如果没有出现,记得检查端口开放情况(以及安全组配置情况)。

配置开机自启动

CentOS 7以上使用Systemd进行系统初始化的,服务文件以 .service 结尾 创建nginx.service

vim /lib/systemd/system/nginx.service

编辑内容如下,请把nginx的可执行文件路径替换成你自己的

[Unit]
Description=nginx
After=network.target
  
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
  
[Install]
WantedBy=multi-user.target

说明:

Unit:服务说明
Description:服务描述
After:服务类别
Service:服务
Type:类型,forking代表后台运行
ExecStart:启动命令
ExecReload:重启命令
ExecStop:停止命令
PrivateTmp:true表示给服务分配独立的空间
Install:设置成多用户

设置开机自启动

systemctl enable nginx.service

其他选项

# 设置开机自动启动
systemctl enable nginx.service

# 设置禁止开机自动启动
systemctl disable nginx.service

# 启动nginx服务
systemctl start nginx.service

# 重新加载nginx配置
systemctl reload nginx.service

# 停止nginx服务
systemctl stop nginx.service

# 查看nginx服务状态
systemctl status nginx.service

错误处理

  • 端口已被占用 报错内容
    nginx[23812]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
    

    查看进程你会发现,其实 nginx 已经启动,所以执行下面的命令,并再次启动就可以了

    killall nginx
    

REFERENCE