NGINX安装

2021-05-07  本文已影响0人  若熙笔记

一、NGINX安装

下载:
wget http://nginx.org/download/nginx-1.6.3.tar.gz
解压:
tar -xzvf nginx-1.6.3.tar.gz
安装依赖:
yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
配置:
./configure --prefix=/usr/local/nginx
安装:
make && make install

二、配置开机启动

创建nginx服务文件:
vi /etc/init.d/nginx #写入以下内容

#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f nginx defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add nginx'
### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO
# Author:   licess
# website:  [http://lnmp.org](http://lnmp.org)
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=/usr/local/nginx/$NAME.pid
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)
        $0 stop
        sleep 1
        $0 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: $0 {start|stop|force-quit|restart|reload|status|configtest}"
        exit 1
        ;;
esac
设置执行权限:
chmod a+x /etc/init.d/nginx
启动:
/etc/init.d/nginx start
注册程服务:
chkconfig --add /etc/init.d/nginx
设置开机启动:
chkconfig nginx on

三、配置成功后的命令

service nginx start
service nginx stop
service nginx restart

四、配置解析PHP

修改Nginx.conf文件:

(1)将server块全部注释;
(2)在http块引入公共的host文件目录

include host/*.host;

在host目录创建虚拟主机的域名,格式如下

server {
        listen       80;
        server_name  www.web-test2.com;
        index index.php;
        root /data/web/test2;
        #以下三行为没有index开头的文件可以进行访问
        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;
        location ~ \.php$ {
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_index index.php; 
      fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include fastcgi.conf;
        }
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
                expires 30d;
        }
        location ~ .*\.(js|css)?$
        {
                expires 1h;
        }
        access_log  /usr/local/nginx/logs/www.web-test2.com_access.log;
        error_log   /usr/local/nginx/logs/www.web-test2.com_error.log;
}
上一篇下一篇

猜你喜欢

热点阅读