1.安装依赖包和开发工具: yum install vim vim-enhanced wget zip unzip telnet ntsysv compat* apr* nasm* gcc gcc* gcc-c++ ntp make imake cmake automake autoconf zlib zlib-devel glibc glibc-devel glib2 libxml glib2-devel libxml2 libxml2-devel bzip2 bzip2-devel libXpm libXpm-devel libidn libidn-devel libtool libtool-ltdl-devel* libmcrypt libmcrypt-devel libevent-devel libmcrypt* curl curl-devel perl perl-Net-SSLeay pcre pcre-devel ncurses ncurses-devel openssl openssl-devel openldap openldap-devel openldap-clients openldap-servers krb5 krb5-devel e2fsprogs e2fsprogs-devel libjpeg libpng libjpeg-devel libjpeg-6b libjpeg-devel-6b libpng-devel libtiff-devel freetype freetype-devel fontconfig-devel gd gd-devel kernel screen sysstat flex bison nss_ldap pam-devel compat-libstdc++-33 2.清除系统中的httpd痕迹: yum remove httpd rm -rvf /etc/httpd 3.创建www用户和用户组: groupadd www useradd -s /sbin/nologin -g www www 4.安装pcre软件包: tar zxvf pcre-8.12.tar.gz -C /usr/src/ cd /usr/src/pcre-8.12/ ./configure && make && make install ldconfig 5.解压、配置、编译、安装nginx: tar zxvf nginx-1.9.0.tar.gz -C /usr/src/ cd /usr/src/nginx-1.9.0/ ./configure --prefix=/usr/local/nginx \ --user=www \ --group=www \ --with-mail \ --with-mail_ssl_module \ --with-http_ssl_module \ --with-http_flv_module \ --with-http_dav_module \ --with-http_sub_module \ --with-http_spdy_module \ --with-http_realip_module \ --with-http_addition_module \ --with-http_gzip_static_module \ --with-http_stub_status_module \ --with-pcre make && make install 6.编辑nginx.conf配置文件: vim /usr/local/nginx/conf/nginx.conf user www www; worker_processes auto; pid /home/www/pid/nginx.pid; worker_rlimit_nofile 51200; events http 7.创建目录并修改权限: mkdir -p /home/www/log mkdir -p /home/www/pid mkdir -p /home/www/html chown -R www:www /home/www/log chown -R www:www /home/www/pid chown -R www:www /home/www/html chown -R www:www /usr/local/nginx 8.测试启动nginx服务: ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx nginx -t nginx -c /usr/local/nginx/conf/nginx.conf ps -aux | grep nginx netstat -anptu | grep 80 9.编写nginx启动脚本: vim /etc/init.d/nginx #!/bin/sh # chkconfig: # Description: # Provides: # Default-Start: # Default-Stop: PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin NAME=nginx NGINX_BIN=/usr/local/nginx/sbin/$NAME CONFIGFILE=/usr/local/nginx/conf/$NAME.conf PIDFILE=/home/www/pid/$NAME.pid SCRIPTNAME=/etc/init.d/$NAME case "$1" in start) echo -n "Starting $NAME... " if netstat -tnpl | grep -q nginx;then echo "$NAME (pid `pidof $NAME`) already running." exit 1 fi $NGINX_BIN -c $CONFIGFILE if [ "$?" != 0 ] ; then echo " failed" exit 1 else echo " done" fi ;; stop) echo -n "Stoping $NAME... " if ! netstat -tnpl | grep -q nginx; then echo "$NAME is not running." exit 1 fi $NGINX_BIN -s stop if [ "$?" != 0 ] ; then echo " failed. Use force-quit" exit 1 else echo " done" fi ;; status) if netstat -tnpl | grep -q nginx; then PID=`pidof nginx` echo "$NAME (pid $PID) is running..." else echo "$NAME is stopped" exit 0 fi ;; force-quit) echo -n "Terminating $NAME... " if ! netstat -tnpl | grep -q nginx; then echo "$NAME is not running." exit 1 fi kill `pidof $NAME` if [ "$?" != 0 ] ; then echo " failed" exit 1 else echo " done" fi ;; restart) $SCRIPTNAME stop sleep 1 $SCRIPTNAME start ;; reload) echo -n "Reload service $NAME... " if netstat -tnpl | grep -q nginx; then $NGINX_BIN -s reload echo " done" else echo "$NAME is not running, can't reload." exit 1 fi ;; configtest) echo -n "Test $NAME configure files... " $NGINX_BIN -t ;; *) echo "Usage: $SCRIPTNAME {start|stop|force-quit|restart|reload|status|configtest}" exit 1 ;; esac 10.添加nginx系统服务: chmod a+x /etc/init.d/nginx chkconfig --add nginx chkconfig --level 2345 nginx on chkconfig --list | grep nginx 11.创建测试页面和错误页面,并赋予权限: touch /home/www/html/index.html cat>/home/www/html/index.html<<EOF This is the test Page for Nginx !!! EOF touch /home/www/html/404.html cat>/home/www/html/404.html<<EOF Error Page for Nginx !!! EOF chown -R www:www /home/www/html 12.重启nginx服务: service nginx restart ps -aux | grep nginx netstat -anptu | grep 80 13.测试: http://localhost/ http://localhost/cuowu 14.防火墙开启80端口: iptables -A INPUT -p tcp --dport 80 -j ACCEPT (责任编辑:IT) |