标签:mod 软件 make tar 客户 日志 charset fastcgi 首页
实验思路重点内容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
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
配置文件调整(配置文件决定服务的安全性,功能、稳定性等配置文件调整十分重要)
基本优化:
[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)虚拟主机配置:
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 ~]#
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 ‘<h1>恭喜,数据库连接成功了,你牛!‘;
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]#
结果:
标签:mod 软件 make tar 客户 日志 charset fastcgi 首页
原文地址:https://blog.51cto.com/14386333/2406572