189 8069 5689

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 Nginx、MySQL、php 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
    $link=mysql_connect('localhost','root','123123');
    if ($link) echo '

    恭喜,数据库连接成功了,你牛!';
    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]#
结果:


分享标题:nginx网站服务
网页路径:http://cdxtjz.cn/article/ggopch.html

其他资讯