Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。在高连接并发的情况下,Nginx是Apache服务器不错的替代品:Nginx在美国是做虚拟主机生意的老板们经常选择的软件平台之一。能够支持高达50,000个并发连接数的响应,而且内存开销极小。这也是Nginx广受欢迎的重要原因。本文演示了基于Linux 6下编译安装Nginx,供大家参考。
一、安装环境
<code><span># cat /etc/issue</span> Red Hat Enterprise Linux Server release <span>6.3</span> (Santiago) Kernel \r <span><span>on</span><span>an</span> \<span>m</span></span><span># nginx -v</span> nginx <span>version</span>: nginx/<span>1.8</span><span>.0</span></code>
二、配置安装环境
<code><span>###为简化安装及配置,此处关闭了防火墙,生产环境建议开启</span><span># service iptables stop</span><span># chkconfig iptables off</span><span># vi /etc/selinux/config </span> SELINUX=disabled <span>###创建用户及组</span><span>#groupadd -r nginx</span><span>#useradd -s /sbin/nologin -g nginx -r nginx</span><span>###安装环境依赖包 http://nginx.org/en/linux_packages.html</span><span># yum install pcre-devel zlib-devel openssl openssl-devel gcc gcc-c++</span></code>
三、编译及安装Nginx
<code># cd /tmp/ # tar -xvf nginx-1.8.0.tar.gz # cd /nginx-1.8.0 # ./configure \ -<span>-prefix=<span>/etc/nginx</span> \ </span>-<span>-sbin-path=<span>/usr/sbin</span><span>/nginx \ </span></span>-<span>-conf-path=<span>/etc/nginx</span><span>/nginx.conf \ </span></span>-<span>-error-log-path=<span>/var/log</span><span>/nginx/error</span>.log \ </span>-<span>-http-log-path=<span>/var/log</span><span>/nginx/access</span>.log \ </span>-<span>-pid-path=<span>/var/run</span><span>/nginx.pid \ </span></span>-<span>-lock-path=<span>/var/run</span><span>/nginx.lock \ </span></span>-<span>-http-client-body-temp-path=<span>/var/cache</span><span>/nginx/client</span>_temp \ </span>-<span>-http-proxy-temp-path=<span>/var/cache</span><span>/nginx/proxy</span>_temp \ </span>-<span>-http-fastcgi-temp-path=<span>/var/cache</span><span>/nginx/fastcgi</span>_temp \ </span>-<span>-http-uwsgi-temp-path=<span>/var/cache</span><span>/nginx/uwsgi</span>_temp \ </span>-<span>-http-scgi-temp-path=<span>/var/cache</span><span>/nginx/scgi</span>_temp \ </span>-<span>-user=nginx \ </span>-<span>-group=nginx \ </span>-<span>-with-http_ssl_module \ </span>-<span>-with-http_realip_module \ </span>-<span>-with-http_addition_module \ </span>-<span>-with-http_sub_module \ </span>-<span>-with-http_dav_module \ </span>-<span>-with-http_flv_module \ </span>-<span>-with-http_mp4_module \ </span>-<span>-with-http_gunzip_module \ </span>-<span>-with-http_gzip_static_module \ </span>-<span>-with-http_random_index_module \ </span>-<span>-with-http_secure_link_module \ </span>-<span>-with-http_stub_status_module \ </span>-<span>-with-http_auth_request_module \ </span>-<span>-with-mail \ </span>-<span>-with-mail_ssl_module \ </span>-<span>-with-file-aio \ </span>-<span>-with-http_spdy_module \ </span>-<span>-with-ipv6 </span> 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: "/etc/nginx" nginx binary file: "/usr/sbin/nginx" 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" ###如果apache httpd服务启动,建议先停止或更改端口号 # service httpd stop # mkdir -p /var/cache/nginx/{client_temp,proxy_temp,fastcgi_temp,uwsgi_temp,scgi_temp} # make && make install ###启动nginx # /usr/sbin/nginx -c /etc/nginx/nginx.conf # ps -ef|grep nginx|grep -v grep root 33412 1 0 10:18 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf nginx 33413 33412 0 10:18 ? 00:00:00 nginx: worker process [root@orasrv1 cache]# netstat -nltp|grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 33412/nginx [root@orasrv1 cache]# </code>
四、配置nginx为系统服务
<code>vi /etc/init.d/nginx <span>#!/bin/bash</span><span># nginx Startup script for the Nginx HTTP Server</span><span># chkconfig: - 85 15</span><span># description: Nginx is an HTTP(S) server, HTTP(S) reverse \</span><span># proxy and IMAP/POP3 proxy server</span><span># Author : Leshami</span><span># Blog : http://blog.csdn.net/leshami </span><span># processname: nginx</span><span># pidfile: /var/run/nginx.pid</span><span># config: /etc/nginx/nginx.conf</span><span>#path for nginx binary</span> nginxd=/usr/sbin/nginx <span>#path for nginx configuration</span> nginx_c/nginx/nginx.conf <span>#path for nginx pid</span> nginx_pid=/var/run/nginx.pid RETVAL=<span>0</span> prog=<span>"nginx"</span><span># Source function library.</span> . /etc/rc.d/init.d/functions <span># Source networking configuration.</span> . /etc/sysconfig/network <span># Check that networking is up.</span> [ <span>${NETWORKING}</span> = <span>"no"</span> ] && <span>exit</span><span>0</span> [ -x <span>$nginxd</span> ] || <span>exit</span><span>0</span><span># Start nginx daemons functions.</span><span><span>start</span></span>() { <span>if</span> [ <span>-e</span><span>$nginx_pid</span> ];<span>then</span><span>echo</span><span>"nginx already running...."</span><span>exit</span><span>1</span><span>fi</span><span>echo</span> -n $<span>"Starting <span>$prog</span>: "</span> daemon <span>$nginxd</span> -c <span>${nginx_config}</span> RETVAL=$? <span>echo</span> [ <span>$RETVAL</span> = <span>0</span> ] && touch /var/lock/subsys/nginx <span>return</span><span>$RETVAL</span> } <span># Stop nginx daemons functions.</span><span><span>stop</span></span>() { <span>echo</span> -n $<span>"Stopping <span>$prog</span>: "</span> killproc <span>$nginxd</span> RETVAL=$? <span>echo</span> [ <span>$RETVAL</span> = <span>0</span> ] && rm <span>-f</span> /var/lock/subsys/nginx /var/run/nginx.pid } <span># reload nginx service functions.</span><span><span>reload</span></span>() { <span>echo</span> -n $<span>"Reloading <span>$prog</span>: "</span><span>#kill -HUP `cat ${nginx_pid}`</span> killproc <span>$nginxd</span> -HUP RETVAL=$? <span>echo</span> } <span># See how we were called.</span><span>case</span><span>"<span>$1</span>"</span><span>in</span> start) start ;; stop) stop ;; reload) reload ;; restart) stop start ;; status) status <span>$prog</span> RETVAL=$? ;; *) <span>echo</span> $<span>"Usage: <span>$prog</span> {start|stop|restart|reload|status|help}"</span><span>exit</span><span>1</span><span>esac</span><span>exit</span><span>$RETVAL</span><span># chmod u+x /etc/init.d/nginx </span><span># service nginx start</span> Starting nginx: [ OK ] <span># ps -ef|grep nginx |grep -v grep</span> root <span>33534</span><span>1</span><span>0</span><span>10</span>:<span>33</span> ? <span>00</span>:<span>00</span>:<span>00</span> nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf nginx <span>33535</span><span>33534</span><span>0</span><span>10</span>:<span>33</span> ? <span>00</span>:<span>00</span>:<span>00</span> nginx: worker process <span># service nginx stop</span> Stopping nginx: [ OK ] <span># chkconfig --add nginx</span><span># chkconfig nginx on</span></code>
五、安装过程中的常见故障
<code>./configure: error: <span>the</span> HTTP rewrite module requires <span>the</span> PCRE library. You can either disable <span>the</span> module <span>by</span><span>using</span><span>--without-http_rewrite_module</span> option, <span>or</span> install <span>the</span> PCRE library <span>into</span><span>the</span><span>system</span>, <span>or</span> build <span>the</span> PCRE library statically <span>from</span><span>the</span> source <span>with</span> nginx <span>by</span><span>using</span><span>--with-pcre=<path> option.</span>./configure: error: <span>the</span> HTTP gzip module requires <span>the</span> zlib library. You can either disable <span>the</span> module <span>by</span><span>using</span><span>--without-http_gzip_module</span> option, <span>or</span> install <span>the</span> zlib library <span>into</span><span>the</span><span>system</span>, <span>or</span> build <span>the</span> zlib library statically <span>from</span><span>the</span> source <span>with</span> nginx <span>by</span><span>using</span><span>--with-zlib=<path> option.</span><span>### 以上2个错误,请安装相应的依赖包,见本文第二部分:配置安装环境</span><span># /usr/sbin/nginx </span> nginx: [emerg] getpwnam(<span>"nginx"</span>) failed <span>### 需要创建nginx用户组及用户</span><span># /usr/sbin/nginx</span> nginx: [emerg] mkdir() <span>"/var/cache/nginx/client_temp"</span> failed (<span>2</span>: No such <span>file</span><span>or</span><span>directory</span>) <span>### 需要创建对应的目录</span></code>
以上就介绍了Linux 6下安装编译安装Nginx,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。