第六天 LNMP环境快速部署-Nginx服务配置
一.LNMP环境快速部署
1.准备工作
Linux 恢复快照 设置IP 关闭防火墙 关闭SELINUX
Vmnet1 网卡连接
配置光盘yum源
2.安装步骤
传输软件包到linux系统
1)解压缩
tar -zxvf lnmp1.2-full.tar.gz
2)进入解压目录
cd lnmp1.2-full
3)安装
./install.sh lnmp
二.LNMP相关软件安装目录
Nginx 目录: /usr/local/nginx/
MySQL 目录 : /usr/local/mysql/
MySQL数据库所在目录:/usr/local/mysql/var/
PHP目录 : /usr/local/php/
PHPMyAdmin目录 : /home/wwwroot/default/phpmyadmin/
默认网站目录 : /home/wwwroot/default/
Nginx日志目录:/home/wwwlogs/
三.LNMP相关配置文件位置
Nginx主配置文件:/usr/local/nginx/conf/nginx.conf
MySQL配置文件:/etc/my.cnf
PHP配置文件:/usr/local/php/etc/php.ini
四.LNMP状态管理命令
LNMP 状态管理: lnmp {start|stop|reload|restart|kill|status}
LNMP 各个程序状态管理: lnmp {nginx|mysql|mariadb|php-fpm|pureftpd} {start|stop|reload|restart|kill|status}
五.配置文件
检查nginx配置文件语句错误
/usr/local/nginx/sbin/nginx -t
平滑重启nginx进程
pkill -HUP nginx
实验1 虚拟主机
1)域名解析
C:\Windows\System32\drivers\etc\hosts
192.168.5.5 www.sina.com
192.168.5.5 www.sohu.com
2)规划网站目录
mkdir /home/wwwroot/sina/
mkdir /home/wwwroot/sohu/
vim /home/wwwroot/sina/index.html
hello Sina!
vim /home/wwwroot/sohu/index.html
hi Sohu!
3)修改配置文件
vim /usr/local/nginx/conf/nginx.conf
66 listen 80;
4)建立虚拟主机文件
vim /usr/local/nginx/conf/vhost/v.conf
1 server {
2 listen 80;
3 server_name www.sina.com;
4 index index.html index.htm index.php;
5 root /home/wwwroot/sina;
6
7 include enable-php.conf;
8 }
9 server {
10 listen 80;
11 server_name www.sohu.com;
12 index index.html index.htm index.php;
13 root /home/wwwroot/sohu;
14
15 include enable-php.conf;
16 }
5)重启服务 测试
pkill -HUP nginx
www.sina.com www.sohu.com
实验2 Rewrite 重写/重定向
www.sina.com -> www.sohu.com
1)修改虚拟主机文件
vim /usr/local/nginx/conf/vhost/v.conf
1 server {
2 listen 80;
3 server_name www.sina.com;
4 index index.html index.htm index.php;
5 root /home/wwwroot/sina;
6
7 include enable-php.conf;
8 location /nginx_status
9 {
10 stub_status on;
11 access_log off;
12 }
13 if ($http_host = www.sina.com) {
14 rewrite (.*) http://www.sohu.com permanent;
15 }
16 }
2)重启服务 测试
pkill -HUP nginx
www.sina.com -> www.sohu.com
网页文件跳转
1)修改虚拟主机文件
vim /usr/local/nginx/conf/vhost/v.conf
1 server {
2 listen 80;
3 server_name www.sina.com;
4 index index.html index.htm index.php;
5 root /home/wwwroot/sina;
6
7 include enable-php.conf;
8 location /nginx_status
9 {
10 stub_status on;
11 access_log off;
12 }
13 rewrite index(\d+).html /index.php?id=$1 last;
14 }
2)建立index.php文件
vim /home/wwwroot/sina/index.php
1 <?php
2 echo "hello Ningx Rewrite!";
3 ?>
3)重启服务 测试
pkill -HUP nginx
www.sina.com/index5.html
实验3 代理负载均衡(反向代理)
1)准备工作
域名解析
192.168.31.168 www.sohu.com
S0 192.168.31.168 Nginx 代理
S1 192.168.31.178 Apache 解析网站代码
S2 192.168.31.188 Apache 解析网站代码
C windows 测试
2)修改S0 192.168.31.168 配置文件
vim /usr/local/nginx/conf/nginx.conf
65 upstream myweb1 {
66 server 192.168.31.178:80;
67 server 192.168.31.188:80;
68 }
69 server {
70 listen 80;
71 server_name www.sohu.com;
72 location / {
73 proxy_pass http://myweb1;
74 proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;
75 proxy_set_header Host $host;
76 proxy_set_header X-Forwarded-For $remote_addr;
77 }
78 }
79
80 #include vhost/*.conf;
81 }
3)修改S1 192.168.31.178 正常访问
cd /usr/local/apache2/htdocs/
vim index.html
S111111111
测试 192.168.31.178 S1111111111
4)修改S1 192.168.31.188 正常访问
cd /usr/local/apache2/htdocs/
vim index.html
S2222222222
测试 192.168.31.188 S222222222222
5)重启S0 测试
pkill -HUP nginx
测试 www.sohu.com S1111111111 S22222222
一.LNMP环境快速部署
1.准备工作
Linux 恢复快照 设置IP 关闭防火墙 关闭SELINUX
Vmnet1 网卡连接
配置光盘yum源
2.安装步骤
传输软件包到linux系统
1)解压缩
tar -zxvf lnmp1.2-full.tar.gz
2)进入解压目录
cd lnmp1.2-full
3)安装
./install.sh lnmp
二.LNMP相关软件安装目录
Nginx 目录: /usr/local/nginx/
MySQL 目录 : /usr/local/mysql/
MySQL数据库所在目录:/usr/local/mysql/var/
PHP目录 : /usr/local/php/
PHPMyAdmin目录 : /home/wwwroot/default/phpmyadmin/
默认网站目录 : /home/wwwroot/default/
Nginx日志目录:/home/wwwlogs/
三.LNMP相关配置文件位置
Nginx主配置文件:/usr/local/nginx/conf/nginx.conf
MySQL配置文件:/etc/my.cnf
PHP配置文件:/usr/local/php/etc/php.ini
四.LNMP状态管理命令
LNMP 状态管理: lnmp {start|stop|reload|restart|kill|status}
LNMP 各个程序状态管理: lnmp {nginx|mysql|mariadb|php-fpm|pureftpd} {start|stop|reload|restart|kill|status}
五.配置文件
检查nginx配置文件语句错误
/usr/local/nginx/sbin/nginx -t
平滑重启nginx进程
pkill -HUP nginx
实验1 虚拟主机
1)域名解析
C:\Windows\System32\drivers\etc\hosts
192.168.5.5 www.sina.com
192.168.5.5 www.sohu.com
2)规划网站目录
mkdir /home/wwwroot/sina/
mkdir /home/wwwroot/sohu/
vim /home/wwwroot/sina/index.html
hello Sina!
vim /home/wwwroot/sohu/index.html
hi Sohu!
3)修改配置文件
vim /usr/local/nginx/conf/nginx.conf
66 listen 80;
4)建立虚拟主机文件
vim /usr/local/nginx/conf/vhost/v.conf
1 server {
2 listen 80;
3 server_name www.sina.com;
4 index index.html index.htm index.php;
5 root /home/wwwroot/sina;
6
7 include enable-php.conf;
8 }
9 server {
10 listen 80;
11 server_name www.sohu.com;
12 index index.html index.htm index.php;
13 root /home/wwwroot/sohu;
14
15 include enable-php.conf;
16 }
5)重启服务 测试
pkill -HUP nginx
www.sina.com www.sohu.com
实验2 Rewrite 重写/重定向
www.sina.com -> www.sohu.com
1)修改虚拟主机文件
vim /usr/local/nginx/conf/vhost/v.conf
1 server {
2 listen 80;
3 server_name www.sina.com;
4 index index.html index.htm index.php;
5 root /home/wwwroot/sina;
6
7 include enable-php.conf;
8 location /nginx_status
9 {
10 stub_status on;
11 access_log off;
12 }
13 if ($http_host = www.sina.com) {
14 rewrite (.*) http://www.sohu.com permanent;
15 }
16 }
2)重启服务 测试
pkill -HUP nginx
www.sina.com -> www.sohu.com
网页文件跳转
1)修改虚拟主机文件
vim /usr/local/nginx/conf/vhost/v.conf
1 server {
2 listen 80;
3 server_name www.sina.com;
4 index index.html index.htm index.php;
5 root /home/wwwroot/sina;
6
7 include enable-php.conf;
8 location /nginx_status
9 {
10 stub_status on;
11 access_log off;
12 }
13 rewrite index(\d+).html /index.php?id=$1 last;
14 }
2)建立index.php文件
vim /home/wwwroot/sina/index.php
1 <?php
2 echo "hello Ningx Rewrite!";
3 ?>
3)重启服务 测试
pkill -HUP nginx
www.sina.com/index5.html
实验3 代理负载均衡(反向代理)
1)准备工作
域名解析
192.168.31.168 www.sohu.com
S0 192.168.31.168 Nginx 代理
S1 192.168.31.178 Apache 解析网站代码
S2 192.168.31.188 Apache 解析网站代码
C windows 测试
2)修改S0 192.168.31.168 配置文件
vim /usr/local/nginx/conf/nginx.conf
65 upstream myweb1 {
66 server 192.168.31.178:80;
67 server 192.168.31.188:80;
68 }
69 server {
70 listen 80;
71 server_name www.sohu.com;
72 location / {
73 proxy_pass http://myweb1;
74 proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;
75 proxy_set_header Host $host;
76 proxy_set_header X-Forwarded-For $remote_addr;
77 }
78 }
79
80 #include vhost/*.conf;
81 }
3)修改S1 192.168.31.178 正常访问
cd /usr/local/apache2/htdocs/
vim index.html
S111111111
测试 192.168.31.178 S1111111111
4)修改S1 192.168.31.188 正常访问
cd /usr/local/apache2/htdocs/
vim index.html
S2222222222
测试 192.168.31.188 S222222222222
5)重启S0 测试
pkill -HUP nginx
测试 www.sohu.com S1111111111 S22222222