nginx 编译安装实践 ==================
系统环境:CentOS 6.8 最小化安装
nginx 源码包下载:PCRE 下载: (rewrite 模块依赖)
zlib 库下载:(gzip 模块依赖)
系统上已经安装有 pcre 和 zlib,zlib-devel,但要编译 nginx 可能还需要 pcre-devel, 需要先 yum install pcre-devel.有这两个开发包,就不用按照前一篇《nginx 编译安装》所说的提供源码路径。
[root@lamp1 nginx-1.10.1]# rpm -qa | grep pcrepcre-7.8-7.el6.x86_64[root@lamp1 nginx-1.10.1]# rpm -qa | grep zlibzlib-1.2.3-29.el6.x86_64zlib-devel-1.2.3-29.el6.x86_64查看 ./configure --help: --without-MODULE_NAME:默认要编译的模块 --with-MODULE_NAME:默认不编译的模块,可使用 ./configure --with-MODULE_NAME 编译该模块
一会将会参考 nginx yum 安装的编译参数
[root@lamp1 nginx-1.10.1]# ./configure --help --help print this message --prefix=PATH set installation prefix --sbin-path=PATH set nginx binary pathname --modules-path=PATH set modules path --conf-path=PATH set nginx.conf pathname --error-log-path=PATH set error log pathname --pid-path=PATH set nginx.pid pathname --lock-path=PATH set nginx.lock pathname --user=USER set non-privileged user for worker processes --group=GROUP set non-privileged group for worker processes --build=NAME set build name --builddir=DIR set build directory --with-select_module enable select module --without-select_module disable select module --with-poll_module enable poll module --without-poll_module disable poll module --with-threads enable thread pool support --with-file-aio enable file AIO support --with-ipv6 enable IPv6 support --with-http_ssl_module enable ngx_http_ssl_module --with-http_v2_module enable ngx_http_v2_module --with-http_realip_module enable ngx_http_realip_module --with-http_addition_module enable ngx_http_addition_module --with-http_xslt_module enable ngx_http_xslt_module --with-http_xslt_module=dynamic enable dynamic ngx_http_xslt_module --with-http_image_filter_module enable ngx_http_image_filter_module --with-http_image_filter_module=dynamic enable dynamic ngx_http_image_filter_module --with-http_geoip_module enable ngx_http_geoip_module --with-http_geoip_module=dynamic enable dynamic ngx_http_geoip_module --with-http_sub_module enable ngx_http_sub_module --with-http_dav_module enable ngx_http_dav_module --with-http_flv_module enable ngx_http_flv_module --with-http_mp4_module enable ngx_http_mp4_module --with-http_gunzip_module enable ngx_http_gunzip_module --with-http_gzip_static_module enable ngx_http_gzip_static_module --with-http_auth_request_module enable ngx_http_auth_request_module --with-http_random_index_module enable ngx_http_random_index_module --with-http_secure_link_module enable ngx_http_secure_link_module --with-http_degradation_module enable ngx_http_degradation_module --with-http_slice_module enable ngx_http_slice_module --with-http_stub_status_module enable ngx_http_stub_status_module --without-http_charset_module disable ngx_http_charset_module --without-http_gzip_module disable ngx_http_gzip_module --without-http_ssi_module disable ngx_http_ssi_module --without-http_userid_module disable ngx_http_userid_module --without-http_access_module disable ngx_http_access_module --without-http_auth_basic_module disable ngx_http_auth_basic_module --without-http_autoindex_module disable ngx_http_autoindex_module --without-http_geo_module disable ngx_http_geo_module --without-http_map_module disable ngx_http_map_module --without-http_split_clients_module disable ngx_http_split_clients_module --without-http_referer_module disable ngx_http_referer_module --without-http_rewrite_module disable ngx_http_rewrite_module --without-http_proxy_module disable ngx_http_proxy_module --without-http_fastcgi_module disable ngx_http_fastcgi_module --without-http_uwsgi_module disable ngx_http_uwsgi_module --without-http_scgi_module disable ngx_http_scgi_module --without-http_memcached_module disable ngx_http_memcached_module --without-http_limit_conn_module disable ngx_http_limit_conn_module --without-http_limit_req_module disable ngx_http_limit_req_module --without-http_empty_gif_module disable ngx_http_empty_gif_module --without-http_browser_module disable ngx_http_browser_module --without-http_upstream_hash_module disable ngx_http_upstream_hash_module --without-http_upstream_ip_hash_module disable ngx_http_upstream_ip_hash_module --without-http_upstream_least_conn_module disable ngx_http_upstream_least_conn_module --without-http_upstream_keepalive_module disable ngx_http_upstream_keepalive_module --without-http_upstream_zone_module disable ngx_http_upstream_zone_module --with-http_perl_module enable ngx_http_perl_module --with-http_perl_module=dynamic enable dynamic ngx_http_perl_module --with-perl_modules_path=PATH set Perl modules path --with-perl=PATH set perl binary pathname --http-log-path=PATH set http access log pathname --http-client-body-temp-path=PATH set path to store http client request body temporary files --http-proxy-temp-path=PATH set path to store http proxy temporary files --http-fastcgi-temp-path=PATH set path to store http fastcgi temporary files --http-uwsgi-temp-path=PATH set path to store http uwsgi temporary files --http-scgi-temp-path=PATH set path to store http scgi temporary files --without-http disable HTTP server --without-http-cache disable HTTP cache --with-mail enable POP3/IMAP4/SMTP proxy module --with-mail=dynamic enable dynamic POP3/IMAP4/SMTP proxy module --with-mail_ssl_module enable ngx_mail_ssl_module --without-mail_pop3_module disable ngx_mail_pop3_module --without-mail_imap_module disable ngx_mail_imap_module --without-mail_smtp_module disable ngx_mail_smtp_module --with-stream enable TCP/UDP proxy module --with-stream=dynamic enable dynamic TCP/UDP proxy module --with-stream_ssl_module enable ngx_stream_ssl_module --without-stream_limit_conn_module disable ngx_stream_limit_conn_module --without-stream_access_module disable ngx_stream_access_module --without-stream_upstream_hash_module disable ngx_stream_upstream_hash_module --without-stream_upstream_least_conn_module disable ngx_stream_upstream_least_conn_module --without-stream_upstream_zone_module disable ngx_stream_upstream_zone_module --with-google_perftools_module enable ngx_google_perftools_module --with-cpp_test_module enable ngx_cpp_test_module --add-module=PATH enable external module --add-dynamic-module=PATH enable dynamic external module --with-cc=PATH set C compiler pathname --with-cpp=PATH set C preprocessor pathname --with-cc-opt=OPTIONS set additional C compiler options --with-ld-opt=OPTIONS set additional linker options --with-cpu-opt=CPU build for the specified CPU, valid values: pentium, pentiumpro, pentium3, pentium4, athlon, opteron, sparc32, sparc64, ppc64 --without-pcre disable PCRE library usage --with-pcre force PCRE library usage --with-pcre=DIR set path to PCRE library sources --with-pcre-opt=OPTIONS set additional build options for PCRE --with-pcre-jit build PCRE with JIT compilation support --with-md5=DIR set path to md5 library sources --with-md5-opt=OPTIONS set additional build options for md5 --with-md5-asm use md5 assembler sources --with-sha1=DIR set path to sha1 library sources --with-sha1-opt=OPTIONS set additional build options for sha1 --with-sha1-asm use sha1 assembler sources --with-zlib=DIR set path to zlib library sources --with-zlib-opt=OPTIONS set additional build options for zlib --with-zlib-asm=CPU use zlib assembler sources optimized for the specified CPU, valid values: pentium, pentiumpro --with-libatomic force libatomic_ops library usage --with-libatomic=DIR set path to libatomic_ops library sources --with-openssl=DIR set path to OpenSSL library sources --with-openssl-opt=OPTIONS set additional build options for OpenSSL --with-debug enable debug logging参考 nginx 软件包的编译参数,其中除了默认编译的模块,它也加入了如下模块:
--with-http_ssl_module--with-http_realip_module--with-http_addition_module--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_stub_status_module--with-http_auth_request_module--with-threads--with-stream--with-stream_ssl_module--with-http_slice_module--with-mail--with-mail_ssl_module--with-file-aio--with-http_v2_module--with-ipv6也可以参考它的其他编译参数:
--prefix=/etc/nginx--sbin-path=/usr/sbin/nginx--conf-path=/etc/nginx/nginx.conf--error-log-path=/var/log/nginx/error.log--http-log-path=/var/log/nginx/access.log--pid-path=/var/run/nginx.pid--lock-path=/var/run/nginx.lock--http-client-body-temp-path=/var/cache/nginx/client_temp--http-proxy-temp-path=/var/cache/nginx/proxy_temp--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp--http-scgi-temp-path=/var/cache/nginx/scgi_temp--user=nginx--group=nginx配置命令:
创建 nginx 普通用户以及 /etc/nginx 目录:
[root@lamp1 nginx-1.10.1]# mkdir /etc/nginx[root@lamp1 nginx-1.10.1]# groupadd nginx[root@lamp1 nginx-1.10.1]# useradd -g nginx nginx[root@lamp1 nginx-1.10.1]# tail -1 /etc/passwdnginx:x:500:500::/home/nginx:/bin/bash[root@lamp1 nginx-1.10.1]# tail -1 /etc/groupnginx:x:500:创建 /var/log/nginx, /var/cache/nginx 目录
[root@lamp1 nginx-1.10.1]# mkdir /var/log/nginx[root@lamp1 nginx-1.10.1]# mkdir /var/cache/nginx注:这些目录如果没有自己创建,在 make install 时也会自动创建。
./configure --prefix=/etc/nginx \--sbin-path=/usr/sbin/nginx \--conf-path=/etc/nginx/nginx.conf \--error-log-path=/var/log/nginx/error.log \--http-log-path=/var/log/nginx/access.log \--pid-path=/var/run/nginx.pid \--lock-path=/var/run/nginx.lock \--http-client-body-temp-path=/var/cache/nginx/client_temp \--http-proxy-temp-path=/var/cache/nginx/proxy_temp \--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \--http-scgi-temp-path=/var/cache/nginx/scgi_temp \--user=nginx \--group=nginx \--with-http_ssl_module \--with-http_realip_module \--with-http_addition_module \--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_stub_status_module \--with-http_auth_request_module \--with-threads \--with-stream \--with-stream_ssl_module \--with-http_slice_module \--with-mail \--with-mail_ssl_module \--with-file-aio \--with-http_v2_module \--with-ipv6配置完成:
Configuration summary + using threads + using system PCRE library + using system OpenSSL library + md5: using OpenSSL library + sha1: using OpenSSL library + using system zlib library nginx path prefix: "/etc/nginx" nginx binary file: "/usr/sbin/nginx" nginx modules path: "/etc/nginx/modules" nginx configuration prefix: "/etc/nginx" nginx configuration file: "/etc/nginx/nginx.conf" nginx pid file: "/var/run/nginx.pid" nginx error log file: "/var/log/nginx/error.log" nginx http access log file: "/var/log/nginx/access.log" nginx http client request body temporary files: "/var/cache/nginx/client_temp" nginx http proxy temporary files: "/var/cache/nginx/proxy_temp" nginx http fastcgi temporary files: "/var/cache/nginx/fastcgi_temp" nginx http uwsgi temporary files: "/var/cache/nginx/uwsgi_temp" nginx http scgi temporary files: "/var/cache/nginx/scgi_temp"执行编译及安装:
$ make && make install
启动 nginx:
$ nginx
编译安装没有 init 脚本,可从 yum 安装的 nginx 中拷贝一份至 /etc/init.d/ 目录下:
[root@vm1 nginx]# cat /etc/init.d/nginx#!/bin/sh## nginx Startup script for nginx## chkconfig: - 85 15# processname: nginx# config: /etc/nginx/nginx.conf# config: /etc/sysconfig/nginx# pidfile: /var/run/nginx.pid# description: nginx is an HTTP and reverse proxy server#### BEGIN INIT INFO# Provides: nginx# Required-Start: $local_fs $remote_fs $network# Required-Stop: $local_fs $remote_fs $network# Default-Start: 2 3 4 5# Default-Stop: 0 1 6# Short-Description: start and stop nginx### END INIT INFO# Source function library.. /etc/rc.d/init.d/functionsif [ -L $0 ]; then initscript=`/bin/readlink -f $0`else initscript=$0fisysconfig=`/bin/basename $initscript`if [ -f /etc/sysconfig/$sysconfig ]; then . /etc/sysconfig/$sysconfigfinginx=${NGINX-/usr/sbin/nginx}prog=`/bin/basename $nginx`conffile=${CONFFILE-/etc/nginx/nginx.conf}lockfile=${LOCKFILE-/var/lock/subsys/nginx}pidfile=${PIDFILE-/var/run/nginx.pid}SLEEPMSEC=${SLEEPMSEC-200000}UPGRADEWAITLOOPS=${UPGRADEWAITLOOPS-5}RETVAL=0start() { echo -n $"Starting $prog: " daemon --pidfile=${pidfile} ${nginx} -c ${conffile} RETVAL=$? echo [ $RETVAL = 0 ] && touch ${lockfile} return $RETVAL}stop() { echo -n $"Stopping $prog: " killproc -p ${pidfile} ${prog} RETVAL=$? echo [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}}reload() { echo -n $"Reloading $prog: " killproc -p ${pidfile} ${prog} -HUP RETVAL=$? echo}upgrade() { oldbinpidfile=${pidfile}.oldbin configtest -q || return echo -n $"Starting new master $prog: " killproc -p ${pidfile} ${prog} -USR2 echo for i in `/usr/bin/seq $UPGRADEWAITLOOPS`; do /bin/usleep $SLEEPMSEC if [ -f ${oldbinpidfile} -a -f ${pidfile} ]; then echo -n $"Graceful shutdown of old $prog: " killproc -p ${oldbinpidfile} ${prog} -QUIT RETVAL=$? echo return fi done echo $"Upgrade failed!" RETVAL=1}configtest() { if [ "$#" -ne 0 ] ; then case "$1" in -q) FLAG=$1 ;; *) ;; esac shift fi ${nginx} -t -c ${conffile} $FLAG RETVAL=$? return $RETVAL}rh_status() { status -p ${pidfile} -b ${nginx} ${nginx}}# See how we were called.case "$1" in start) rh_status >/dev/null 2>&1 && exit 0 start ;; stop) stop ;; status) rh_status RETVAL=$? ;; restart) configtest -q || exit $RETVAL stop start ;; upgrade) rh_status >/dev/null 2>&1 || exit 0 upgrade ;; condrestart|try-restart) if rh_status >/dev/null 2>&1; then stop start fi ;; force-reload|reload) reload ;; configtest) configtest ;; *) echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|upgrade|reload|status|help|configtest}" RETVAL=2esacexit $RETVAL从其他客户端测试 nginx 是否可用:
[root@vm1 nginx]# curl http://192.168.0.171编译安装完成。