第六章 Nginx服务与LNMP部署

一、Nginx服务基础
1、Nginx安装及运行控制
(1)编译安装
①安装支持软件:yum install -y gcc gcc-c++ pcre-devel zlib-devel
②创建用户、组:useradd -M -s /sbin/nologin nginx
③编译安装
1)./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
2)make && make install
④命令优化
1)ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
(2)Nginx的运行控制
①检查配置文件:nginx -t
启动:nginx
③停止:
1)killall -s QUIT nginx
2)kill -s QUIT [PID]
④重载:
1)killall -s HUP nginx
2)kill -s HUP [PID]
(3)编写服务脚本

#!/bin/bash
#chkconfig:- 99 20
#server file for Nginx
file="/usr/local/nginx/sbin/nginx"
pid="/usr/local/nginx/logs/nginx.pid"

st(){
    netstat -anput | grep nginx >>/dev/null
        if [ $? -eq 0 ]
        then
        echo -e "\033[1;31mnginx:nginx is already running...\033[0m"
        else
        $file
        echo -e "\033[1;32mStarting nginxd success...\033[0m"
        fi
}

sto(){
    netstat -anput | grep nginx >>/dev/null
        if [ $? -eq 0 ]
        then
        kill -s QUIT $(cat $pid)
        echo -e "\033[1;32mStopping nginxd success...\033[0m"
        else
                echo -e "\033[1;31mnginx:nginx not running\033[0m"
        fi
}

rel(){
    netstat -anput | grep nginx >>/dev/null
        if [ $? -eq 0 ]
        then 
        kill -s HUP $(cat $pid)
        echo -e "\033[1;32mReload nginxd success...\033[0m"
        else    
                echo -e "\033[1;31mnginx:nginx not running\033[0m"
        fi
}

sta(){
    $file -v
    netstat -anput | grep nginx >>/dev/null
    if [ $? -eq 0 ]
    then
        echo -e "\033[1;32mnginx:nginx is running\033[0m"
    else
        echo -e "\033[1;31mnginx:nginx not running\033[0m"
    fi
}

case $1 in
    start)
        st
        ;;
    stop)
        sto
        ;;
    reload)
        rel
        ;;
    restart)
        sto
        st
        ;;
    status)
        sta
        ;;
    *)
        echo "Usage:$0 {start|stop|restart|reload|status}"
        exit 1
esac
exit 0

(4)添加系统服务
chmod +x /etc/init.d/nginxd 
chkconfig --add nginx
chkconfig nginx on
2、配置文件nginx.conf
(1)全局配置
user nobody:运行用户
worker_processes 1:工作进程数量(通常根据CPU性能进行设置)
error_log logs/error.log:错误日志位置 
pid logs/nginx.pid:PID文件位置
(2)I/O事件配置
events{}:界定标记,指定nginx的I/O响应模型及连接数等设置
use epoll:使用epoll模型
worker_connections 1024:每个进程处理的连接数
(3)HTTP配置
http{}:界定标记,内部包含访问日志、http端口、网页目录,虚拟主机等
access_log logs/access.log main:访问日志位置
sendfile on:支持文件发送(下载)
keepalive_timeout 65:连接保持超时
server{}:web服务监听配置
1)listen 80:监听地址及端口
2)server_name www.luobin.com:网站名称
3)charset utf-8:默认字符集
4)location /{}:根目录配置
a.root html:网站根目录位置
b.index index.html index.php:默认首页
5)error_page 500 502 503 504/50x.html:内部错误反馈页面
6)location= /50x.html{}:错误页面配置
a.root html:错误页面目录位置
3、访问状态统计
(1)启用模块:--with-http_stub_status_module
(2)配置文件修改:
相关server中添加location/status{}
stub_status on:打开状态统计功能
access_log off:关闭此位置的日志记录
(3)访问:直接访问网站的/status位置
(4)status详解
active connections:活跃的连接数量
server accepts handled requests:总共处理了89个连接 , 成功创建89次握手, 总共处理了87个请求
reading:读取客户端的连接数.
writing:响应数据到客户端的数量
waiting:开启 keep-alive 的情况下,这个值等于 active – (reading+writing), 意思就是 Nginx 已经处理完正在等候下一次请求指令的驻留连接
4、CentOS7中添加nginx服务脚本
(1)vim /lib/systemd/system/nginx.service

[Unit]
Description=nginx  //描述
After=network.target   //描述服务类别
[Service]
Type=forking  //后台运行形式
PIDFile=/usr/local/nginx/logs/nginx.pid  //PID文件位置
ExecStart=/usr/local/nginx/sbin/nginx  //启动服务
ExecReload=/usr/bin/kill -s HUP $MAINPID  //根据PID重载
ExecStop=/usr/bin/kill -s QUIT $MAINPID   //根据PID关闭
[Install]
WantedBy=multi-user.target
(2)chmod 754 /lib/systemd/system/nginx.service
(3)systemctl enable nginx.service

二、Nginx访问控制
1、基于授权的访问控制
(1)使用htpasswd生成用户认证文件(yum install httpd-tools):
htpasswd -c /usr/local/nginx/passwd.db webadmin
(2)修改密码文件权限:
chmod 400 /usr/local/nginx/passwd.db
chown nginx /usr/local/nginx/passwd.db
(3)修改配置文件nginx.conf
location中添加
auth_basic “secret”:添加认证配置
auth_basic_user_file /usr/local/nginx/passwd.db
(4)检测语法并重启服务
①nginx -t
ulimit -n 65530:增加连接限制数量
③service nginx restart
2、基于客户端的访问限制
(1)修改配置文件nginx.conf
location中添加
deny 192.168.85.1:禁止的客户端IP
allow all
(2)重启服务
3、永久修改连接限制数
(1)修改/etc/security/limits.conf 文件(注意带着前面的*号),如下:
①*     soft        nofile    65530
②*    hard        nofile    65530
(2)重新加载库:
①打开文件:vi /etc/pam.d/login在最后加上:
1)session    required    /lib64/security/pam_limits.so

三、Nginx虚拟主机
1、基于域名的虚拟主机

server {
    listen  80;
    server_name www.luobin.com;
    location / {
        root html/www;
        index index.html index.php index.htm;
    }
}
server {
    listen  80;
    server_name test.luobin.com;
    location / {
        root html/test;
        index index.html index.php index.htm;
    }
}

2、基于IP的虚拟主机
listen 192.168.1.101:80;
server_name 192.168.1.101:80;
listen 192.168.1.102:80;
server_name 192.168.1.102:80;

3、基于端口的虚拟主机
listen 80;
server_name www.luobin.com;
listen 8080;
server_name www.luobin.com;

四、LNMP架构部署
1、编译安装MySQL

(1)yum -y install ncurses-devel
 (2)安装gmake
 (3)编译安装mysql:cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DSYSCONFDIR=/etc
 (4)make && make install
 (5)优化
①cp support-files/my-medium.cnf /etc/my.cnf
 ②cp support-files/mysql.server /etc/rc.d/init.d/mysqld
 ③chmod +x /etc/rc.d/init.d/mysqld
 ④chkconfig --add mysqld
 ⑤echo “PATH=$PATH:/usr/local/mysql/bin”>>/etc/profile
 ⑥./etc/profile
 (6)初始化数据库
①groupadd mysql
 ②useradd -M -s /sbin/nologin mysql -g mysql
 ③chown -R mysql:mysql /usr/local/mysql
 ④/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
 (7)启动服务
 ①service mysqld start
 ②mysqladmin -u root password ‘111111’
 2、安装PHP解析环境
 (1)安装PHP依赖包:yum -y install libpng libpng-devel pcre pcre-devel libxml2-devel libjepeg-devel
 (2)编译安装PHP
./configure
 --prefix=/usr/local/php5
 --with-gd
 --with-curl
 --with-zlib
 --with-mysqli
 --with-mysql=mysqlnd
 --with-config-file-path=/usr/local/php5
 --enable-mbstring
 --enable-fpm
 --with-jpeg-dir=/usr/lib
 make &&make install
 (3)复制模板文件件作为PHP主配置文件
cp php.ini-development /usr/local/php5/php.ini
 (4)安装ZendGuardLoader提高PHP效率
 ①cp ZendGuardLoader.so /usr/local/php5/lib/php/
 ②修改php.ini
1)zend_extension=/usr/local/php5/lib/php/ZendGuardLoader.so
 2)zend_loader.enable=1
 3、配置nginx支持PHP环境
 (1)PHP-FPM模块配置
 ①复制模板文件作为配置文件
cp /usr/local/php5/etc/php-fpm.conf.default /usr/local/php5/etc/php-fpm.conf
 ②修改php-fpm.conf文件
pid=run/php-fpm.pid
 user=nginx
 group=nginx
 pm.max_children=50
 pm.start_servers=20
 pm.min_spare_servers=5
 pm.max_spare_servers=20
 ③启动PHP-FPM模块(9000端口)
ln -s /usr/local/php5/bin/* /usr/local/bin
 ln -s /usr/local/php5/sbin/* /usr/local/sbin
 php-fpm
 ④修改nginx启动脚本,添加php-fpm模块
PROG_FPM=”/usr/local/sbin/php-fpm”
 PID_FPM=”/usr/local/php5/var/run/php-fpm.pid”
 $PROG_FPM
 kill -s QUIT $(cat $PID_FPM)
 kill -s HUP $(cat $PID_FPM)


(2)配置nginx.conf支持php解析
①修改server{}

location ~ \.php$ {
root /var/www/phpcom;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}

创建phpinfo页面并测试

<?php
 phpinfo();
 ?>
 ③创建php连接数据库页面并测试
 <?php
 //phpinfo();
 $link=mysql_connect('192.168.11.20','root','111111');
 if($link) echo “数据库连接成功!”;
 mysql_close();
 ?>