一、软件下载
预先知道需要哪些软件库去下载:
1、nginx源码
下载源码位置 http://nginx.org/en/download.html
注意要下稳定版,即带有stable的版本为稳定版。
org后缀的域名是社区版。.com后缀的域名是nginx的商业版。选择社区版。
笔者下载的是nginx-1.12.2.tar.gz。
2、下载openssl
这个软件做https的时候需要,建议最好编译安装的时候,就安装上去。
编译nginx时候,使用参数--with-openssl=源码位置。
下载地址: https://www.openssl.org/source/
3、下载pcre库。
下载地址:http://www.pcre.org/ ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/
pcre是一个正则库,nginx使用正则进行重写要用到,必须安装。很多软件都会使用这个库来做正则匹配,比如php引擎也会用到(pcre扩展,只是php源码中自带了这个库)
编译nginx时,使用参数--with-pcre=来指定pcre源码的位置,是源码位置,所以不需要编译安装pcre库的。
注:pcre库在官网,存在两个版本,pcre和pcre2。要下载pcre,pcre2是新版的库,编译是通不过的。
编译pcre就必须用到c++编译器。
使用pcre2就使用gcc编译器。
4、下载zlib库
下载地址为:http://www.zlib.net/
5、基础软件需求
需要c语言编译器gcc、 c++编译器gcc-c++
安装方式编译器方式,centos操作系统下,直接使用yum命令安装
yum install gcc gcc-c++
ubantu使用apt-get命令安装:
apt-get install gcc g++
注:centos系统的软件仓库和ubantu系统的软件仓库,c++编译器使用的名称不一样。
二、安装过程
1、解压各类软件源码
tar -xzvf 源码包.tar.gz
如果是下载的zip压缩包,则使用unzip命令解压: unzip 压缩包
解压后,会在当前目录创建一个文件夹。
要预先解压pcre、openssl、zlib的源码。--with-pcre、--with-http_ssl_module --with-openssl、--with-zlib指定是这些库的源码目录。
2、编译源码
切换到nginx源码目录
cd 源码目录
执行如下命令:
./configure --prefix=/disk2/soft/nginx/
--with-pcre=/disk2/soft_download/pcre-8.38
--with-http_ssl_module 、
--with-openssl=/disk2/soft_download/openssl-1.0.2m
--with-debug
--with-http_stub_status_module
--with-http_gzip_static_module
--with-zlib=/disk2/soft_download/zlib-1.2.11
执行成功后,执行如下命令:
make && make install
解释:
prefix是指定nginx安装到哪个目录。建议指定,指定的好处,清晰容易找。如果不指定目录,nginx的二进制文件和配置文件就会按照默认目录,分散到多个文件夹:/usr/bin/nginx、/etc/nginx.conf。
注意pcre等参数,指定的是源码目录,不是安装好的目录。nginx不同于php是引入库的安装目录,nginx它会把这些组件直接编到nginx里面去,好处是性能高,依赖性少。php引入的是已经安装好的库目录,如果库文件夹被删除了,php引擎使用时就会出错。
--conf-path=配置文件位置
、--pid-path=pid文件的位置
、--user=
name、
--group=
name没必要指定。这些参数,有的可以在启动nginx时指定,有的可以在nginx.conf中去配置。
追求编译的参数越简单明了,障碍越少。
--pid-path、
--user=
可以在编译完成后,去nginx.conf中配置。name、
--group=
name
--conf-path,因为
默认就是"安装目录/conf/nginx.conf"。若想载入指定的配置文件,启动的时候也可以用-c参数指定。所以不需要指定,
3、启动和开机启动
则启动命令为:安装目录/sbin/nginx
假设编译安装时指定的--prefix参数配置的安装目录为:/disk2/soft/nginx。
启动命令就是:/disk2/soft/nginx/sbin/nginx
这是nginx的二进制文件。直接在命令行中输入这个就可以启动。使用-c参数载入指定的nginx.conf配置文件。
/disk2/soft/nginx/sbin/nginx -c /disk2/soft/nginx/conf/nginx.conf
若没有指定-c参数,载入的是配置文件 "安装目录/conf/nginx.conf",用源码编译安装,一般不用指定-c就可以。
注:如果在源码编译时候使用了--conf-path
=指定了nginx.conf配置文件的位置,那么nginx默认会去这个位置载入nginx配置文件。没有指定这个参数,
使用命令/disk2/soft/nginx/sbin/nginx 启动nginx时,默认是去"prefix/conf/nginx.conf" 载入配置文件。除非启动时,使用-c参数明确告诉nginx配置文件位置,就可以覆盖掉默认的配置。
/disk2/soft/nginx/sbin/nginx -t 测验配置文件语法是否正确
/disk2/soft/nginx/sbin/nginx -s reload 重新载入配置文件
启动命令是"/disk2/soft/nginx/sbin/nginx",把这个加到 /etc/rc.local文件中就可以了。
4、做成服务
源码编译,是没有加到service服务的,也就是没法:service nginx start这样使用。若想这样,需要自己设置。
笔者习惯使用/disk2/soft/nginx/sbin/nginx 来启动。这样很清晰知道是哪个nginx。如果服务器安装了多个nginx,输入路径来启动、reload,这样很清晰。
如果需要停止掉nginx服务。直接使用" kill -9进程编号"。
官网有个做成服务的启动脚本:https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/ 可惜这个是在centos5下测验的。我放centos6.9下则有点瑕疵,它使用killproc来杀掉进程名称。这样没效。
找到一个脚本,经过测验,内容如下:
#! /bin/bash # Description: Startup script for webserver on CentOS. cp it in /etc/init.d and # chkconfig --add nginx && chkconfig nginx on # then you can use server command control nginx # # chkconfig: 2345 08 99 # description: Starts, stops nginx set -e
#将nginx二进制文件所在目录加到环境变量,在命令行直接使用nginx就可以定位到二进制文件。这个配置不是必须的步骤,可以注释掉。 PATH=$PATH:/disk2/soft/nginx/sbin/ DESC="nginx daemon" NAME=nginx
#修改这里成自己的nginx二进制文件位置 DAEMON=/disk2/soft/nginx/sbin/nginx
#配置文件位置 CONFIGFILE=/disk2/soft/nginx/conf/nginx.conf
#pid文件位置。在nginx.conf中指定的,这里指定是为了方便stop时去杀掉这个进程编号 PIDFILE=/disk2/soft/nginx/logs/nginx.pid SCRIPTNAME=/etc/init.d/$NAME # Gracefully exit if the package has been removed. test -x $DAEMON || exit 0 d_start() { $DAEMON -c $CONFIGFILE || echo -n " already running" } d_stop() { kill -QUIT `cat $PIDFILE` || echo -n " not running" } d_reload() { kill -HUP `cat $PIDFILE` || echo -n " can't reload" } case "$1" in start) echo -n "Starting $DESC: $NAME" d_start echo "." ;; stop) echo -n "Stopping $DESC: $NAME" d_stop echo "." ;; reload) echo -n "Reloading $DESC configuration..." d_reload echo "reloaded." ;; restart) echo -n "Restarting $DESC: $NAME" d_stop sleep 1 d_start echo "." ;; *) echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2 exit 3 ;; esac exit 0
重命名一个自己喜欢的名称。把脚本放到/etc/init.d/目录下。比如nginx,命令service nginx start中的nginx就是对应这个文件名。
脚本的内容要稍微修改。因为二进制文件和配置文件的目录是不一样。
使用service来启动nginx,不是必备的。其实建议直接用完整路径来启动。很清晰,排查问题的时候,指定nginx的路径。