Linux下Nginx源码编译安装
什么是Nginx?
Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器,在高连接并发的情况下Nginx 是 Apache 服务器不错的替代品.其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好.目前中国大陆使用nginx网站用户有:新浪、网易、 腾讯,另外知名的微网志Plurk也使用nginx。
Nginx 作为 负载均衡 服务器:
Nginx 既可以在内部直接支持 Rails 和 PHP 程序对外进行服务,也可以支持作为 HTTP 代理服务器对外进行服务。 Nginx 采用 C 进行编写, 不论是系统资源开销还是 CPU 使用效率都比 Perlbal 要好很多。作为邮件代理服务器:
Nginx 同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器), Last.fm 描述了成功并且美妙的使用经验。Nginx 是一个安装非常简单,配置文件非常简洁(还能够支持perl语法), Bugs非常少的服务器: Nginx 启动特别容易,并且几乎可以做到 7*24 不间断运行,即使运行数个月也不需要重新启动。 你还能够不间断服务的情况下进行软件版本的升级。
官网下载
[root@nginx ~]# ls
nginx-1.15.2.tar.gz
安装依赖软件
##安装编译工具 gcc
[root@nginx ~]# yum install -y gcc
##安装PCRE库,pcre提供编译版本的库,pcre-devel提供开发阶段的头文件和编译项目的源代码。
[root@nginx ~]# yum install -y pcre pcre-devel
##安装OpenSSL加密库,nginx中如果服务器需要提供安全网页则会用到OpenSSL库
[root@nginx ~]# yum install -y openssl openssl-devel
##安装zlib库,zlib库提供了压缩算法,在nginx的各种模块中需要使用gzip压缩
[root@nginx ~]# yum install -y zlib zlib-devel
解压
[root@nginx ~]# tar zxf nginx-1.15.2.tar.gz
[root@nginx ~]# ls
nginx-1.15.2 nginx-1.15.2.tar.gz
查看模块
[root@nginx ~]# cd nginx-1.15.2
[root@nginx nginx-1.15.2]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@nginx nginx-1.15.2]# ./configure --help ##查看参数
--help print this message
--prefix=PATH nginx安装路径
--sbin-path=PATH nginx二进制执行文件路径
--modules-path=PATH set modules path
--conf-path=PATH nginx配置文件存放位置
--error-log-path=PATH 错误日志文件路径
--pid-path=PATH nginx.pid文件路径(pid文件包含进程的标识符)
--lock-path=PATH 锁文件存放路径(锁文件(nginx.log)用于控制nginx进程没有被启动两次))
--user=USER 指定启动nginx进程的默认用户;set non-privileged user for worker processes
--group=GROUP 指定启动nginx进程的默认用户组;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-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_mirror_module disable ngx_http_mirror_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_grpc_module disable ngx_http_grpc_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_random_module
disable ngx_http_upstream_random_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
--with-stream_realip_module enable ngx_stream_realip_module
--with-stream_geoip_module enable ngx_stream_geoip_module
--with-stream_geoip_module=dynamic enable dynamic ngx_stream_geoip_module
--with-stream_ssl_preread_module enable ngx_stream_ssl_preread_module
--without-stream_limit_conn_module disable ngx_stream_limit_conn_module
--without-stream_access_module disable ngx_stream_access_module
--without-stream_geo_module disable ngx_stream_geo_module
--without-stream_map_module disable ngx_stream_map_module
--without-stream_split_clients_module
disable ngx_stream_split_clients_module
--without-stream_return_module disable ngx_stream_return_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_random_module
disable ngx_stream_upstream_random_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-compat dynamic modules compatibility
--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-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
添加模块,编译
[root@nginx nginx-1.15.2]# ./configure --prefix=/usr/local/lnmp/nginx --with-http_ssl_module --with-http_stub_status_module --with-file-aio --with-threads --user=nginx --group=nginx
编译成功后执行安装命令
[root@nginx nginx-1.15.2]# make && make install
添加用户
[root@server1 nginx-1.10.1]# useradd -s /sbin/nologin -M nginx
创建软链接,便于启动
[root@nginx nginx-1.15.2]# ln -s /usr/local/lnmp/nginx/sbin/nginx /usr/local/sbin/
开启Nginx
[root@nginx nginx-1.15.2]# nginx
[root@nginx nginx-1.15.2]# ps ax| grep nginx
7066 ? Ss 0:00 nginx: master process nginx
7067 ? S 0:00 nginx: worker process
7069 pts/0 S+ 0:00 grep --color=auto nginx
至此,已经成功安装Nginx,访问 http:ip
平滑加载
[root@nginx nginx-1.15.2]# nginx -s reload
关闭Nginx
[root@nginx nginx-1.15.2]# nginx -s stop
编译时选择需要的模块不同,或者需要改变安装路径、用户等,则要重新编译或者重新安装Nginx
重新编译
[root@nginx ~]# ls
nginx-1.15.2 nginx-1.15.2.tar.gz
[root@nginx ~]# cd nginx-1.15.2
[root@nginx nginx-1.15.2]# make clean
rm -rf Makefile objs
[root@nginx nginx-1.15.2]# ./configure --prefix=/usr/local/web/nginx --with-http_ssl_module --with-http_stub_status_module --with-file-aio --with-threads --user=nginx --group=nginx
##注意重新编译仅仅 make 不用 make install
[root@nginx nginx-1.15.2]# make
[root@nginx nginx-1.15.2]# nginx
[root@nginx nginx-1.15.2]# ps xa| grep nginx
9728 ? Ss 0:00 nginx: master process nginx
9729 ? S 0:00 nginx: worker process
9732 pts/0 S+ 0:00 grep --color=auto nginx
[root@nginx nginx-1.15.2]# nginx -s stop
重新安装
[root@nginx ~]# ls
nginx-1.15.2 nginx-1.15.2.tar.gz
[root@nginx ~]# cd nginx-1.15.2
[root@nginx nginx-1.15.2]# make clean
rm -rf Makefile objs
[root@nginx nginx-1.15.2]# ./configure --prefix=/usr/local/lnmp/nginx --with-http_ssl_module --with-http_stub_status_module --with-file-aio --with-threads --user=nginx --group=nginx
##注意重新安装nginx时,需要 make && make install
[root@nginx nginx-1.15.2]# make && make install
[root@nginx nginx-1.15.2]# nginx
[root@nginx nginx-1.15.2]# ps xa| grep nginx
12419 ? Ss 0:00 nginx: master process nginx
12420 ? S 0:00 nginx: worker process
12422 pts/0 S+ 0:00 grep --color=auto nginx
[root@nginx nginx-1.15.2]# nginx -s stop