> CentOS > CentOS教程 >

CentOS_6.5安装Nginx_1.9.0

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
    {
        use epoll;
        worker_connections 51200;
        multi_accept on;
    }
http
    {
        include mime.types;
        default_type application/octet-stream;
        server_names_hash_bucket_size 128;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 50m;
        sendfile on;
        tcp_nopush on;
        keepalive_timeout 60;
        tcp_nodelay on;

        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 256k;
       
        gzip on;
        gzip_min_length 1k;
        gzip_buffers 4 16k;
        gzip_http_version 1.0;
        gzip_comp_level 2;
        gzip_types text/plain application/x-javascript text/css application/xml;
        gzip_vary on;
        gzip_proxied expired no-cache no-store private auth;
        gzip_disable "MSIE [1-6]\.";

        server_tokens off;
        log_format access '$remote_addr - $remote_user [$time_local] "$request" '
        '$status $body_bytes_sent "$http_referer" '
        '"$http_user_agent" $http_x_forwarded_for';
    server
        {
            listen 80 default;
            server_name 127.0.0.1;
            access_log /home/www/log/access.log access;
            error_log /home/www/log/error.log error;
            index index.html index.htm index.php;
            root /home/www/html/;
            error_page 404 /404.html;

            location ~ [^/]\.php(/|$)
            {
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                include fastcgi.conf;
            }
            location /nginx_status
            {
                stub_status on;
                access_log off;
            }
            location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
            {
                expires 30d;
            }
            location ~ .*\.(js|css)?$
            {
                expires 12h;
            }
        }
    include vhost/*.conf;
    }
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:        2345 80 20
# Description:        Start and Stop Nginx
# Provides:        nginx
# Default-Start:    2 3 4 5
# Default-Stop:        0 1 6
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)