Nginx服务器安装和配置


1、安装pcre软件包

[root@www ~]# rpm -qa | grep pcre
[root@www ~]# yum -y install pcre

2、创建nginx用户

[root@www ~]# useradd -s /sbin/nologin nginx
[root@www ~]# passwd nginx
Changing password for user nginx.
New password:
BAD PASSWORD: it is too short
BAD PASSWORD: is too simple
Retype new password:
passwd: all authentication tokens updated successfully.


3、nginx编译参数

--user    指定启动程序所属用户

--group    指定组

--prefix    指定安装路径

--sbin-path    设置nginx二进制文件的路径名

--conf-path    指定配置文件路径

--error-log-path    错误日志文件路径

--http-log-path    指定访问日志文件路径

--http-client-body-temp-path    设置存储HTTP客户端请求主体的临时文件路径

--http-proxy-temp-path    设置存储HTTP代理临时文件的路径

--http-fastcgi-temp-path    设置存储HTTP fastcgi的临时文件的路径

--pid-path    设置nginx.pid文件路径

--lock-path    设置nginx.lock文件路径

--with-openssl    启用SSL

--with-pcre    启用正则表达式

--with-http_stub_status_module    安装可以监控nginx状态的模块

--with-http_ssl_module    启用SSL支持

--with-http_gzip_static_module    启用gzip压缩


4、安装nginx

[root@www nginx-1.9.6]# ./configure \
> --user=nginx \
> --group=nginx \
> --prefix=/usr/local/nginx \
> --sbin-path=/usr/sbin/nginx \
> --conf-path=/etc/nginx/nginx.conf \
> --error-log-path=/var/log/nginx/error.log \
> --http-log-path=/var/log/nginx/access.log \
> --http-client-body-temp-path=/tmp/nginx/client_body \
> --http-proxy-temp-path=/tmp/nginx/proxy \
> --http-fastcgi-temp-path=/tmp/nginx/fastcgi \
> --pid-path=/var/run/nginx.pid \
> --lock-path=/var/lock/subsys/nginx \
> --with-http_stub_status_module \
> --with-http_ssl_module \
> --with-http_gzip_static_module \
> --with-pcre \
> --with-http_realip_module \
> --with-http_sub_module

[root@www nginx-1.9.6]# make && make install

[root@www nginx-1.9.6]# ls /usr/local/nginx/
html

检查配置

[root@www nginx-1.9.6]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: [emerg] mkdir() "/tmp/nginx/client_body" failed (2: No such file or directory)
nginx: configuration file /etc/nginx/nginx.conf test failed
[root@www nginx-1.9.6]# mkdir /tmp/nginx/client_body -p
[root@www nginx-1.9.6]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

启动nginx

[root@www nginx-1.9.6]# nginx
[root@www nginx-1.9.6]# netstat -tlnp |grep nginx
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      4554/nginx       

5、查看nginx版本及编译参数

[root@www nginx-1.9.6]# nginx -V
nginx version: nginx/1.9.6
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/subsys/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-http_sub_module

6、控制Nginx服务器

启动:nginx

停止:nginx -s stop

退出:nginx -s quit

重新打开:nginx -s reopen

重新加载配置:nginx -s reload


7、配置nginx启动脚本

[root@www ~]# vim /etc/init.d/nginx

#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings

NGINX_SBIN="/usr/sbin/nginx"
NGINX_CONF="/etc/nginx/nginx.conf"
NGINX_PID="/var/run/nginx.pid"
RETVAL=0
prog="Nginx"

#Source networking configuration
. /etc/sysconfig/network
# Check networking is up
[ ${NETWORKING} = "no" ] && exit 0
[ -x $NGINX_SBIN ] || exit 0

start() {
        echo -n $"Starting $prog: "
        touch /var/lock/subsys/nginx
        daemon $NGINX_SBIN -c $NGINX_CONF
        RETVAL=$?
        echo
        return $RETVAL
}

stop() {
        echo -n $"Stopping $prog: "
        killproc -p $NGINX_PID $NGINX_SBIN -TERM
        rm -rf /var/lock/subsys/nginx /var/run/nginx.pid
        RETVAL=$?
        echo
        return $RETVAL
}

reload(){
        echo -n $"Reloading $prog: "
        killproc -p $NGINX_PID $NGINX_SBIN -HUP
        RETVAL=$?
        echo
        return $RETVAL
}


restart(){
        stop
        start
}

configtest(){
    $NGINX_SBIN -c $NGINX_CONF -t
    return 0
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  reload)
        reload
        ;;
  restart)
        restart
        ;;
  configtest)
        configtest
        ;;
  *)
        echo $"Usage: $0 {start|stop|reload|restart|configtest}"
        RETVAL=1
esac

exit $RETVAL

设置可执行权限

[root@www ~]# chmod 755 /etc/init.d/nginx


添加开机启动

[root@www ~]# chkconfig --add nginx
[root@www ~]# chkconfig nginx on

开启

[root@www ~]# service nginx start
Starting Nginx:                                            [  OK  ]

停止

[root@www ~]# service nginx stop
Stopping Nginx:                                            [  OK  ]

重启

[root@www ~]# service nginx restart
Stopping Nginx:                                            [  OK  ]
Starting Nginx:                                            [  OK  ]


重新加载配置

[root@www ~]# service nginx reload
Reloading Nginx:                                           [  OK  ]



8、配置nginx服务器php解析

[root@www nginx-1.9.6]# vim /etc/nginx/nginx.conf
打开php配置:

 65         location ~ \.php$ {
 66             root           html;
 67             fastcgi_pass   127.0.0.1:9000;
 68             fastcgi_index  index.php;
 69             fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
 70             include        fastcgi_params;
 71         }

user  nginx;
worker_processes  8;
error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
    worker_connections  204800;
}


    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    tcp_nopush     on;
    charset utf-8;


wKiom1Z5TM-iS_xQAAB0MHM1BR4922.jpg

php-fpm启动报错

Starting php-fpm [18-Dec-2015 13:33:03] ERROR: unable to bind listening socket for address '127.0.0.1:9000': Address already in use (98)
[18-Dec-2015 13:33:03] ERROR: FPM initialization failed
 failed

[root@www nginx-1.9.6]# /usr/local/php/sbin/php-fpm -t
[18-Dec-2015 13:35:14] NOTICE: configuration file /usr/local/php/etc/php-fpm.conf test is successful

[root@www nginx-1.9.6]# killall php-fpm
[root@www nginx-1.9.6]# service php-fpm start
Starting php-fpm  done


重新加载配置

[root@www nginx-1.9.6]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@www nginx-1.9.6]# nginx -s reload

测试php解析

[root@www nginx-1.9.6]# vim /usr/local/nginx/html/index.php

<?php
phpinfo();
?>

wKioL1Z5Us-w4n_RAAFOnfSzyw4757.jpg