微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

nginx网站服务

实验思路
第一步 安装及运行控制
第二步 配置文件调整
第三步 状态统计及虚拟主机
第四步 LNMP构建
第五步 LNMP平台部署(上线)web应用(网站项目)
2.实验环境:
主机 OS IP地址 软件 说明概述
Ctos6-1 Centos6.5 192.168.200.254 ftp及yum源 提供基础环境
Ctos6-2 Centos6.5 192.168.200.202 NginxmysqlPHP Nginx网站
3.重点内容
重点内容1: 什么是Nginx
轻量级HTTP服务软件,具有稳定性、高效性、低消耗、高并发连接处理能力的一款专门用于处理静态页面的web服务软件。

重点内容2: 安装及运行控制:
1.下载软件—>安装依赖包(支持软件)创建用户和组—>安装
2.运行控制:
Nginx –t ##检查配置
Nginx ##启动
killall –s 信号 Nginx ##信号有HUP重载配置,QUIT退出、KILL杀死
重点内容3: 配置文件:/usr/local/Nginx/conf/Nginx.conf
1.基本格式:
vi /usr/local/Nginx/conf/Nginx.conf
:%g/^$/d ##删除空行
:%g/#/d ##删除包含#号的行即注释
配置项格式:关键字 值;
配置包含:全局配置,I/O事件配置(events {配置项}),HTTP配置(http { server { location {} } })http配置可以有多个server配置,server配置可以有多个location配置。
:wq
2.重要配置项:
Server_name 网站域名;
Location / {
Root html; ##网站的根目录
Index index.html; ##指定首页
}
重点内容4: 访问状态统计及虚拟主机:
1.访问状态:Nginx内置HTTP_STUB_STATUS模块
启用这个功能需要两个步骤:一是编译安装是指定--with-http_stub_status_module,二是修改配置文件
2.虚拟主机:Nginx支持虚拟主机(端口、ip、域名)
1)Nginx实现虚拟主机:一个虚拟主机一个server{不同监听地址、域名、端口、日志位置、网页根目录}
2)常用虚拟主机为基于域名虚拟主机,配置多个虚拟机时只需要添加多个域名解析同时设置多个server{}配置即可。
重点内容5: LNMP架构及应用部署:Nginx处理静态页面PHP-fpm处理动态页面
1.安装MysqL数据库:编译安装优化调整初始化数据库启动MysqL服务
2.安装PHP:编译安装(注意--PHP-fpm启用PHP支持)安装后调整添加zendguardloader

3.配置PHP-fpm(Nginx支持PHP环境):一创建PHP-fpm监听TCP/9000端口,二添加Nginx转发PHP请求到9000端口。
1)创建PHP-fpm配置文件
vi /usr/local/PHP5/etc/PHP-fpm.conf ##fpm配置文件
[global] ##全局配置
pid = run/PHP-fpm.pid
[www] ##网站配置
listen = 127.0.0.1:9000 ##监听的ip:端口
user = Nginx ##用户必须是Nginx进程的用户
group = Nginx
pm = dynamic
pm.max_children = 50 ##启动时开启的进程数
pm.start_servers= 20 ##最少空闲进程数
pm.min_spare_servers = 5 ##最小空闲进程数
pm.max_spare_servers = 35 ##最多进程数
:wq
2)配置Nginx支持PHP解析:两种方式二选一
方式一:代理的方式(转发PHP请求到其他能解析PHP的主机)(集群中使用)
Server {
Location ~ .PHP$ { proxy_pass http://PHP解析主机ip:端口 }
}
方式二:调用PHP-fpm进程(单机使用)
Server {
Location ~ .PHP$ { ##匹配URL中PHP的请求
root html; ##网页根目录
fastcgi_pass 127.0.0.1:9000; ##指定fpm监听地址及端口
fastcgi_index index.PHP; ##指定PHP首页文件
include fastcgi.conf; ##引入fastcgi.conf配置
}
}
4.发布PHP应用(代码上线):下载程序代码(网页项目)并解压复制到网页根目录创建数据库并授权网页初始化设置及访问。

三.项目实验步骤(操作截图或者操作命令)
1.安装及运行控制:
1)安装Nginx:192.168.200.202
[root@localhost ~]# lftp 192.168.200.254 ##下载
lftp 192.168.200.254:~> cd tools/
lftp 192.168.200.254:/tools> get Nginx-1.6.0.tar.gz
802956 bytes transferred
lftp 192.168.200.254:/tools> bye
[root@localhost ~]# yum -y install pcre-devel zlib-devel &>/dev/null ##安装依赖包
[root@localhost ~]# useradd -M -s /sbin/nologin Nginx ##创建用户
[root@localhost ~]# tar zxf Nginx-1.6.0.tar.gz -C /usr/src/ &>/dev/null ##解压
[root@localhost ~]# cd /usr/src/Nginx-1.6.0/
[root@localhost Nginx-1.6.0]# ./configure --prefix=/usr/local/Nginx --user=Nginx --group=Nginx --with-http_stub_status_module &&make &&make install ##安装
[root@localhost Nginx-1.6.0]# ln -s /usr/local/Nginx/sbin/Nginx /usr/local/sbin/ ###创建软链接,优化命令搜索路径
[root@localhost Nginx-1.6.0]# ls -l /usr/local/sbin/Nginx
lrwxrwxrwx 1 root root 27 8月 31 17:02 /usr/local/sbin/Nginx -> /usr/local/Nginx/sbin/Nginx
[root@localhost Nginx-1.6.0]# cd
2)运行控制:192.168.200.202
[root@localhost ~]# Nginx -t
Nginx: the configuration file /usr/local/Nginx/conf/Nginx.conf Syntax is ok
Nginx: configuration file /usr/local/Nginx/conf/Nginx.conf test is successful
[root@localhost ~]#
[root@localhost ~]# Nginx ##启动服务
[root@localhost ~]# netstat -utpln |grep 80 ##验证
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3250/Nginx
[root@localhost ~]#
3)使用Nginx服务脚本:
[root@localhost ~]# killall -s H
UP Nginx ##重新加载配置文件,相当于reload
[root@localhost ~]# killall -s QUIT Nginx ##退出,正常结束
[root@localhost ~]# killall -s KILL Nginx ##强制杀死
[root@localhost ~]# vi /etc/init.d/Nginx
#!/bin/bash

chkconfig: 35 99 20

description: Nginx Server Control Script

NP="/usr/local/Nginx/sbin/Nginx"
NPF="/usr/local/Nginx/logs/Nginx.pid"
case "$1" in ##$1表示第一位置变量,$0表示脚本本身
start)
$NP;
if [ $? -eq 0 ]
then
echo "Nginx is starting!! "
fi
;;
stop)
kill -s QUIT $(cat $NPF)
if [ $? -eq 0 ]
then
echo "Nginx is stopping!! "
fi
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $NPF)
if [ $? -eq 0 ]
then
echo "Nginx config file is reload! "
fi
;;
)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0
:wq
[root@localhost ~]# chkconfig --add Nginx ##添加系统服务
[root@localhost ~]# chmod +x /etc/init.d/Nginx ##授权
[root@localhost ~]# /etc/init.d/Nginx restart ##重启验证
Nginx is stopping!!
Nginx is starting!!
[root@localhost ~]# netstat -utpln |grep Nginx ##查看Nginx监听端口
tcp 0 0 0.0.0.0:80 0.0.0.0:
LISTEN 3277/Nginx
[root@localhost ~]#

4)访问验证:
真机访问验证:192.168.200.202

linux客户端访问验证:192.168.200.254
[root@localhost ~]# yum -y install elinks
[root@localhost ~]# elinks --dump http://192.168.200.202 ##访问
Welcome to Nginx!

If you see this page, the Nginx web server is successfully installed and
working. Further configuration is required.

For online documentation and support please refer to [1]Nginx.org.
Commercial support is available at [2]Nginx.com.

Thank you for using Nginx.

References

Visible links

  1. http://Nginx.org/
  2. http://Nginx.com/
    [root@localhost ~]#
  1. 配置文件调整(配置文件决定服务的安全性,功能、稳定性等配置文件调整十分重要)
    基本优化:
    [root@localhost ~]# vi /usr/local/Nginx/conf/Nginx.conf
    :%g/^$/d
    :%g/#/d
    :set nu
    调整并发链接数:
    并发连接数=worker_process(工作进程数)X worker_connections=2x4096=8192
    1 worker_processes 2;
    2 events {
    3 worker_connections 4096;
    4 }
    10 charset utf-8; ##支持中文字符集,utf-8万国码
    16 index index.html index.htm index.PHP; ##支持PHP首页
    :wq
    [root@localhost ~]# Nginx –t ##测试
    Nginx: the configuration file /usr/local/Nginx/conf/Nginx.conf Syntax is ok
    Nginx: configuration file /usr/local/Nginx/conf/Nginx.conf test is successful
    [root@localhost ~]#
    [root@localhost ~]# /etc/init.d/Nginx restart
    Nginx is stopping!!
    Nginx is starting!!
    [root@localhost ~]#
    [root@localhost ~]# ps aux |grep Nginx |grep worker |wc -l ##验证工作进程数

  2. 状态统计及虚拟主机
    1)状态统计:192.168.200.202
    [root@localhost ~]# Nginx –V ##验证安装是是否加载统计功能
    Nginx version: Nginx/1.6.0
    built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)
    configure arguments: --prefix=/usr/local/Nginx --user=Nginx --group=Nginx --with-http_stub_status_module
    [root@localhost ~]# vi /usr/local/Nginx/conf/Nginx.conf ##修改配置
    18 location /status {
    19 stub_status on; ##启用状态统计
    20 access_log off; #关闭日志记录,仅仅是http://ip/status的日志被关闭
    21 }
    :wq
    [root@localhost ~]# /etc/init.d/Nginx restart
    Nginx is stopping!!
    Nginx is starting!!
    [root@localhost ~]# elinks --dump http://192.168.200.202/status ##192.168.200.254访问验证
    Active connections: 1(活动连接)
    server accepts handled requests(已经处理的连接信息) 1(连接数) 1(成功TCP握手) 1(已处理的请求数)
    Reading: 0 Writing: 1 Waiting: 0
    [root@localhost ~]#

2)虚拟主机配置:
A.设置dns解析:192.168.200.254
[root@ns ~]# cd /var/named/chroot/var/named
[root@ns named]# vi ../../etc/named.conf ##最后添加
zone "linuxren.cn." IN {
type master;
file "linuxren.cn.zone";
};
:wq
[root@ns named]# cp linuxfan.cn.zone linuxren.cn.zone
[root@ns named]# sed -i 's/fan/ren/g' linuxren.cn.zone
[root@ns named]# /etc/init.d/named restart
停止 named:. [确定]
启动 named: [确定]
[root@localhost ~]#
[root@ns named]# vi /etc/resolv.conf
nameserver 192.168.200.254
nameserver 10.0.0.2
:wq
[root@ns named]# nslookup www.linuxfan.cn
Server: 192.168.200.254
Address: 192.168.200.254#53

Name: www.linuxfan.cn
Address: 192.168.200.202

[root@ns named]# nslookup www.linuxren.cn
Server: 192.168.200.254
Address: 192.168.200.254#53

Name: www.linuxren.cn
Address: 192.168.200.202

[root@ns named]#
B.修改配置文件及准备测试目录:192.168.200.202
[root@localhost ~]# mkdir /var/www/ ##创建测试目录
[root@localhost ~]# echo www.linuxfan.cn >/usr/local/Nginx/html/index.html ##创建测试首页
[root@localhost ~]# echo www.linuxren.cn >/var/www/index.html
[root@localhost ~]# vi /usr/local/Nginx/conf/Nginx.conf ##添加如下内容,注意行号
13 server_name www.linuxfan.cn; ##修改网站域名
27 server {
28 listen 80;
29 server_name www.linuxren.cn; ##修改网站域名
30 location / {
31 root /var/www/; ##指定网页根目录
32 index index.html index.htm index.PHP;
33 }
34 error_page 500 502 503 504 /50x.html;
35 location = /50x.html {
36 root html;
37 }
38 }
:wq
[root@localhost ~]# Nginx –t ##检查语法
Nginx: the configuration file /usr/local/Nginx/conf/Nginx.conf Syntax is ok
Nginx: configuration file /usr/local/Nginx/conf/Nginx.conf test is successful
[root@localhost ~]#
[root@localhost ~]# /etc/init.d/Nginx restart ##重启服务
Nginx is stopping!!
Nginx is starting!!
[root@localhost ~]#
C:访问测试:192.168.200.254
[root@localhost ~]# vi /etc/resolv.conf
; generated by /sbin/dhclient-script
nameserver 192.168.200.254 ##修改原有的nameserver
:wq
[root@localhost ~]# elinks --dump http://www.linuxfan.cn ##访问测试
www.linuxfan.cn
[root@localhost ~]# elinks --dump http://www.linuxren.cn
www.linuxren.cn
[root@localhost ~]#

  1. LNMP构建
    1)安装MysqL:192.168.200.202
    [root@localhost ~]# lftp 192.168.200.254
    lftp 192.168.200.254:~> cd tools/
    lftp 192.168.200.254:/tools> get lamp_install_publis-app-2015-07-16.tar.xz
    95811756 bytes transferred in 3 seconds (26.83M/s)
    lftp 192.168.200.254:/tools> bye
    [root@localhost ~]# tar Jxf lamp_install_publis-app-2015-07-16.tar.xz
    [root@localhost ~]# vi bin/MysqL_install.sh
    :q
    [root@localhost ~]# vi bin/MysqL_config.sh
    :q
    [root@localhost ~]# MysqL_install.sh &&MysqL_config.sh
    。。。此出省略很多提示。。。
    Starting MysqL.. SUCCESS!
    MysqL root password is 123123
    [root@localhost ~]# source /etc/profile ##执行配置文件
    [root@localhost ~]# MysqL -uroot -p123123 ##登录MysqL验证
    MysqL> quit
    Bye
    [root@localhost ~]#
    2)安装PHP:192.168.200.202
    [root@localhost ~]# yum -y install gd libxml2-devel libjpeg-devel libpng-devel &>/dev/null ##安装依赖包
    [root@localhost ~]# ls PHP-5.3.28.tar.gz
    PHP-5.3.28.tar.gz
    [root@localhost ~]# tar zxf PHP-5.3.28.tar.gz -C /usr/src/
    [root@localhost ~]#
    [root@localhost ~]# cd /usr/src/PHP-5.3.28/
    [root@localhost PHP-5.3.28]# ./configure --prefix=/usr/local/PHP5 --with-gd --with-zlib --with-MysqL=/usr/local/MysqL/ --with-config-file-path=/usr/local/PHP5 --enable-mbstring --enable-fpm --with-jpeg-dir=/usr/lib &&make &&make install ##安装使用--enable-fpm选项启用fastCGI进程管理,以便解析PHP页面
    。。。省略大量提示信息。。。
    You may want to add: /usr/local/PHP5/lib/PHP to your PHP.ini include_path
    [PEAR] Structures_Graph- installed: 1.0.4
    [PEAR] XML_Util - installed: 1.2.1
    /usr/src/PHP-5.3.28/build/shtool install -c ext/phar/phar.phar /usr/local/PHP5/bin
    ln -s -f /usr/local/PHP5/bin/phar.phar /usr/local/PHP5/bin/phar
    Installing PDO headers: /usr/local/PHP5/include/PHP/ext/pdo/
    [root@localhost PHP-5.3.28]# cp PHP.ini-development /usr/local/PHP5/PHP.ini ##复制配置文件
    [root@localhost PHP-5.3.28]# ln -s /usr/local/PHP5/bin/ /usr/local/bin/ ##优化路径
    [root@localhost PHP-5.3.28]# ln -s /usr/local/PHP5/sbin/
    /usr/local/sbin/
    [root@localhost PHP-5.3.28]# cd
    3)安装zendguardloader:192.168.200.202
    [root@localhost ~]# tar zxf ZendGuardLoader-PHP-5.3-linux-glibc23-x86_64.tar.gz
    [root@localhost ~]#
    [root@localhost ~]# cp /root/ZendGuardLoader-PHP-5.3-linux-glibc23-x86_64/PHP-5.3.x/ZendGuardLoader.so /usr/local/PHP5/lib/PHP/ ##注意是一行,复制模块文件
    [root@localhost ~]# vi /usr/local/PHP5/PHP.ini ##在最后添加
    zend_extension=/usr/local/PHP5/lib/PHP/ZendGuardLoader.so ##指定模块文件位置
    zend_loader.enable=1 ##启用zend模块
    :wq
    4)配置PHP环境:启用PHP-fpm:192.168.200.202
    [root@localhost ~]# vi /usr/local/PHP5/etc/PHP-fpm.conf
    [global]
    pid = run/PHP-fpm.pid
    [www]
    user = Nginx
    group = Nginx
    listen = 127.0.0.1:9000
    pm = dynamic
    pm.max_children = 5
    pm.start_servers = 2
    pm.min_spare_servers = 1
    pm.max_spare_servers = 3
    :wq
    [root@localhost ~]#
    [root@localhost ~]# /usr/local/sbin/PHP-fpm ##启动服务
    [root@localhost ~]# netstat -utpln |grep PHP
    tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 118437/PHP-fpm
    [root@localhost ~]#
    5)Nginx支持PHP-fpm:
    [root@localhost ~]# vi /usr/local/Nginx/conf/Nginx.conf
    22 location ~.PHP$ {
    23 root html;
    24 fastcgi_pass 127.0.0.1:9000;
    25 fastcgi_index index.PHP;
    26 include fastcgi.conf;
    27 }
    :wq
    6)编写启动LNMP脚本:
    [root@localhost ~]# vi /etc/init.d/lnmp
    #!/bin/bash

    chkconfig: 35 95 30

    description: This script is for LNMP Management!

    NGF=/usr/local/Nginx/sbin/Nginx
    NGP=/usr/local/Nginx/logs/Nginx.pid
    FPMF=/usr/local/PHP5/sbin/PHP-fpm
    FPMP=/usr/local/PHP5/var/run/PHP-fpm.pid
    case $1 in
    start)
    $NGF &&echo "Nginx is starting! "
    $FPMF && echo "PHP-fpm is starting! "
    ;;
    stop)
    kill -QUIT $(cat $NGP) &&echo "Nginx is stoped! "
    kill -QUIT $(cat $FPMP) &&echo "PHP-fpm is stoped! "
    ;;
    restart)
    $0 stop
    $0 start
    ;;
    reload)
    kill -HUP $(cat $NGP)
    kill -HUP $(cat $FPMP)
    ;;
    status)
    netstat -utpln |grep Nginx &>/dev/null
    if [ $? -eq 0 ]
    then
    echo "Nginx is running! "
    else
    echo "Nginx is not running! "
    fi
    netstat -upltn |grep PHP-fpm &>/dev/null
    if [ $? -eq 0 ]
    then
    echo "PHP-fpm is runing! "
    else
    echo "PHP-fpm is not running! "
    fi
    ;;
    *)
    echo "Usage $0 {start|stop|status|restart}"
    exit 1
    ;;
    esac
    :wq
    [root@localhost ~]# chmod +x /etc/init.d/lnmp
    [root@localhost ~]# chkconfig --add lnmp
    [root@localhost ~]# /etc/init.d/lnmp status
    Nginx is running!
    PHP-fpm is runing!
    [root@localhost ~]#
    [root@localhost ~]# vi /usr/local/Nginx/html/index.PHP
    <?PHP
    $link=MysqL_connect('localhost','root','123123');
    if ($link) echo '<h2>恭喜,数据库连接成功了,你牛!';
    MysqL_close();
    ?>
    :wq
    [root@localhost ~]#
    访问验证:192.168.200.11(真机)注意将DNS设置192.168.200.254

5.LNMP平台部署(上线)web应用(网站项目):192.168.200.202
[root@localhost ~]# yum -y install unzip ##安装解压软件
[root@localhost ~]# lftp ftp.linuxfan.cn
lftp ftp.linuxfan.cn:~> cd tools/
lftp ftp.linuxfan.cn:/tools> get SKYUC.v3.4.2.soURCE.zip ##下载网站项目
8249271 bytes transferred
lftp ftp.linuxfan.cn:/tools> bye
[root@localhost ~]#
[root@localhost ~]# unzip SKYUC.v3.4.2.soURCE.zip ##解压
[root@localhost ~]# cd SKYUC.v3.4.2.soURCE/
[root@localhost ~]# cp -rf wwwroot /usr/local/Nginx/html/skyuc ##复制项目,也可以用ln命令来链接
[root@localhost ~]# cd /usr/local/Nginx/html/skyuc ##进入目录
[root@localhost ~]# chown -R Nginx:Nginx admincp/ data/ templates/ upload/ ##授权
[root@localhost ~]# MysqL -uroot -p123123 -s ##登录MysqL
create database skyucdb; ##创建数据库
grant all on skyucdb.* to runskyuc@'localhost' identified by '123123'; ##授权本地访问
quit;
[root@localhost ~]#
浏览器访问:http://192.168.200.202/skyuc
web页面的操作比较简单大家自己完成。

[root@localhost ~]# cd /usr/local/Nginx/html/skyuc ##192.168.200.202上删除安装文件
[root@localhost skyuc]# rm -rf install/
[root@localhost skyuc]#
结果:

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐