本文共 13662 字,大约阅读时间需要 45 分钟。
实验思路
第一步 安装及运行控制第二步 配置文件调整第三步 状态统计及虚拟主机第四步 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.conf1.基本格式:vi /usr/local/nginx/conf/nginx.conf:%g/^$/d ##删除空行:%g/#/d ##删除包含#号的行即注释配置项格式:关键字 值;配置包含:全局配置,I/O事件配置(events {配置项}),HTTP配置(http { server { location {} } })http配置可以有多个server配置,server配置可以有多个location配置。:wq2.重要配置项: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支持)安装后调整添加zendguardloader3.配置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 = nginxpm = dynamic pm.max_children = 50 ##启动时开启的进程数pm.start_servers= 20 ##最少空闲进程数pm.min_spare_servers = 5 ##最小空闲进程数pm.max_spare_servers = 35 ##最多进程数:wq2)配置nginx支持PHP解析:两种方式二选一方式一:代理的方式(转发php请求到其他能解析php的主机)(集群中使用)Server { Location ~ .php$ { proxy_pass }}方式二:调用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]# cd2)运行控制:192.168.200.202[root@localhost ~]# nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: 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 HUP nginx ##重新加载配置文件,相当于reload[root@localhost ~]# killall -s QUIT nginx ##退出,正常结束[root@localhost ~]# killall -s KILL nginx ##强制杀死[root@localhost ~]# vi /etc/init.d/nginx#!/bin/bashNP="/usr/local/nginx/sbin/nginx"
NPF="/usr/local/nginx/logs/nginx.pid"case "$1" in ##$1表示第一位置变量,$0表示脚本本身start)$NP;if [ $? -eq 0 ] thenecho "nginx is starting!! "fi;;stop)kill -s QUIT $(cat $NPF)if [ $? -eq 0 ]thenecho "nginx is stopping!! "fi;;restart)$0 stop$0 start;;reload)kill -s HUP $(cat $NPF)if [ $? -eq 0 ]thenecho "nginx config file is reload! "fi;;)echo "Usage: $0 {start|stop|restart|reload}"exit 1esacexit 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.202linux客户端访问验证:192.168.200.254
[root@localhost ~]# yum -y install elinks[root@localhost ~]# elinks --dump ##访问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
配置文件调整(配置文件决定服务的安全性,功能、稳定性等配置文件调整十分重要)
基本优化:[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf:%g/^$/d:%g/#/d:set nu调整并发链接数:并发连接数=worker_process(工作进程数)X worker_connections=2x4096=81921 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 oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@localhost ~]#[root@localhost ~]# /etc/init.d/nginx restartnginx is stopping!! nginx is starting!! [root@localhost ~]#[root@localhost ~]# ps aux |grep nginx |grep worker |wc -l ##验证工作进程数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.confnameserver 192.168.200.254nameserver 10.0.0.2:wq[root@ns named]# nslookup www.linuxfan.cnServer: 192.168.200.254Address: 192.168.200.254#53Name: www.linuxfan.cn
Address: 192.168.200.202[root@ns named]# nslookup www.linuxren.cn
Server: 192.168.200.254Address: 192.168.200.254#53Name: 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 oknginx: 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-scriptnameserver 192.168.200.254 ##修改原有的nameserver:wq[root@localhost ~]# elinks --dump ##访问测试www.linuxfan.cn[root@localhost ~]# elinks --dump www.linuxren.cn[root@localhost ~]#NGF=/usr/local/nginx/sbin/nginx
NGP=/usr/local/nginx/logs/nginx.pidFPMF=/usr/local/php5/sbin/php-fpmFPMP=/usr/local/php5/var/run/php-fpm.pidcase $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 ]thenecho "nginx is running! "elseecho "nginx is not running! "finetstat -upltn |grep php-fpm &>/dev/null if [ $? -eq 0 ]thenecho "php-fpm is runing! "elseecho "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 statusnginx 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 '<h1>恭喜,数据库连接成功了,你牛!';mysql_close();?>:wq[root@localhost ~]#访问验证:192.168.200.11(真机)注意将DNS设置192.168.200.2545.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 ##登录mysqlcreate database skyucdb; ##创建数据库grant all on skyucdb.* to runskyuc@'localhost' identified by '123123'; ##授权本地访问quit;[root@localhost ~]# 浏览器访问:web页面的操作比较简单大家自己完成。[root@localhost ~]# cd /usr/local/nginx/html/skyuc ##192.168.200.202上删除安装文件
[root@localhost skyuc]# rm -rf install/[root@localhost skyuc]#结果:转载于:https://blog.51cto.com/14386333/2406572