安装依赖环境
yum -y install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel perl-devel perl-ExtUtils-Embed autoconf libxslt-devel libxml2 gd-devel perl-ExtUtils-Embed GeoIP GeoIP-devel GeoIP-data gd-devel
1、下载安装包:
cd /usr/local/src
wget http://nginx.org/download/nginx-1.16.1.tar.gz
2、解压:
tar -zxvf nginx-1.16.1.tar.gz
3、配置环境:
cd nginx-1.16.1
./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-stream_ssl_preread_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-http_auth_request_module --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-debug --with-stream --without-http_rewrite_module
nginx大部分常用模块,编译时./configure --help以--without开头的都默认安装。
--prefix=PATH : 指定nginx的安装目录。默认 /usr/local/nginx
--conf-path=PATH : 设置nginx.conf配置文件的路径。nginx允许使用不同的配置文件启动,通过命令行中的-c选项。默认为prefix/conf/nginx.conf
--user=name: 设置nginx工作进程的用户。安装完成后,可以随时在nginx.conf配置文件更改user指令。默认的用户名是nobody。--group=name类似
--with-pcre : 设置PCRE库的源码路径,如果已通过yum方式安装,使用--with-pcre自动找到库文件。使用--with-pcre=PATH时,需要从PCRE网站下载pcre库的源码(版本4.4 – 8.30)并解压,剩下的就交给Nginx的./configure和make来完成。perl正则表达式使用在location指令和 ngx_http_rewrite_module模块中。
--with-zlib=PATH : 指定 zlib(版本1.1.3 – 1.2.5)的源码解压目录。在默认就启用的网络传输压缩模块ngx_http_gzip_module时需要使用zlib 。
--with-http_ssl_module : 使用https协议模块。默认情况下,该模块没有被构建。前提是openssl与openssl-devel已安装
--with-http_stub_status_module : 用来监控 Nginx 的当前状态
--with-http_realip_module : 通过这个模块允许我们改变客户端请求头中客户端IP地址值(例如X-Real-IP 或 X-Forwarded-For),意义在于能够使得后台服务器记录原始客户端的IP地址
--add-module=PATH : 添加第三方外部模块,如nginx-sticky-module-ng或缓存模块。每次添加新的模块都要重新编译(Tengine可以在新加入module时无需重新编译
4、编译安装:
make && make install
这里对解压完成后的部分目录和文件做个简单的介绍:
src 该目录存放了Nginx的所有源码;
man 该目录存放了Nginx的帮助文档;
html 该目录存放了两个html文件。这两个文件与Nginx服务器的运行相关,这两个文件的作用会在下文
给出,这里不做赘述;
conf 该目录存放的是Nginx服务器的配置文件,包含Nginx服务器的基本配置文件;
auto 该目录存放了大量脚本文件,和configure脚本程序有关;
configure 该文件是Nginx软件的自动脚本程序。运行configure脚本一般会完成两项工作:
一是检查环境,根据环境检查结果生成C代码;二是生成编译代码需要的Makefile文件。
5、配置开机自启动:
在目录/usr/lib/systemd/system下创建文件nginx.service,文件内容如下:
[Unit]
Description=A high performance web server and a reverse proxy server Documentation=man:nginx(8)
After=network.target
[Service]
Type=forking
Restart=on-failure
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid TimeoutStopSec=5
KillMode=mixed
RemainAfterExit=no
[Install]
WantedBy=multi-user.target
6、命令
设置开机启动:
systemctl enable nginx
启动Nginx:
systemctl start nginx
停止Nginx:
systemctl stop nginx
重启Nginx:
systemctl reload nginx
不带服务的操作命令
检查配置文件是否正确
/usr/local/nginx/sbin/nginx -t
./sbin/nginx -v # 查看版本
./sbin/nginx -V # 可以看到编译选项
启动、关闭
./sbin/nginx # 默认配置文件 conf/nginx.conf,-c 指定
./sbin/nginx -s stop
或 pkill nginx
重启,不会改变启动时指定的配置文件
./sbin/nginx -s reload
或 kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
---------------------------------------------------------------------------------------
安装问题汇总
1、nginx安装error: perl module ExtUtils::Embed is required
编译安装nginx的时候–with-http_perl_module 这个参数,编译时出现如下错误:
error: perl module ExtUtils::Embed is required
解决办法安装依赖:
yum -y install perl-devel perl-ExtUtils-Embed
2、可能出现错误
在配置信息./configure --prefix=/usr/local/nginx 的时,出现错误:
/configure: error: the HTTP rewrite module requires the PCRE library.
解决方法:安装pcre
yum -y install pcre pcre-devel
3、缺少ssl错误,错误信息如下:
./configure: error: the HTTP cache module requires md5 functions
from OpenSSL library. You can either disable the module by using
--without-http-cache option, or install the OpenSSL library into the system,
or build the OpenSSL library statically from the source with nginx by using
--with-http_ssl_module --with-openssl= options.
解决方法:安装openssl
yum -y install openssl openssl-devel
4、缺少编译器,错误信息如下:
./configure: error: C compiler cc is not found
解决方法:安装gcc-c++
yum -y install gcc-c++ autoconf automake
5、autoconf是自动配置,automake是自动编译
缺少zlib包,错误信息如下:
./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= option.
解决方法:安装zlib
yum install -y zlib-devel
6、确实libxml2,错误信息如下:
./configure: error: the HTTP XSLT module requires the libxml2/libxslt
libraries. You can either do not enable the module or install the libraries.
解决方法:
yum -y install libxml2 libxslt-devel
http_image_filter_module是nginx提供的集成图片处理模块,需要gd-devel的支持,错误信息如下:
./configure: error: the HTTP image filter module requires the GD library.
You can either do not enable the module or install the libraries.
7、解决方法:
yum -y install gd-devel
缺少ExtUtils,错误信息如下:
./configure: error: perl module ExtUtils::Embed is required
解决方法:
yum -y install perl-devel perl-ExtUtils-Embed
8、缺少GeoIP,错误信息如下:
./configure: error: the GeoIP module requires the GeoIP library.
You can either do not enable the module or install the library.
解决方法:
yum -y install GeoIP GeoIP-devel GeoIP-data
9、当时是下面错误用,了解决方法
http_image_filter_module是nginx提供的集成图片处理模块,需要gd-devel的支持,错误信息如下:
./configure: error: the HTTP image filter module requires the GD library.
You can either do not enable the module or install the libraries.
解决方法:
yum -y install gd-devel