Nginx+Apache动静分离

 

【centos6.5】

一、搭建LAMP

二、搭建Nginx ,实现动静分离

2.1源码包编译安装Nginx

2.2 修改nginx.conf 主配置文件

 

一、搭建LAMP

实验环境:俩台服务器

192.168.168.135  LAMP服务器    LAMP.amber.com

192.168.168.132  Nginx 服务器    nginx.amber.com

实验时注意观察是在哪台服务器上进行的配置!看清主机名

(此处采用rpm方式搭建)

[root@LAMP ~]# rpm -q httpd mysql mysql-server php php-mysql

package httpd is not installed

package mysql is not installed

package mysql-server is not installed

package php is not installed

package php-mysql is not installed

[root@LAMP ~]# yum -y install httpd mysql mysql-server php php-mysql

[root@LAMP ~]# rpm -q httpd mysql mysql-server php php-mysql

httpd-2.2.15-29.el6.centos.x86_64

mysql-5.1.71-1.el6.x86_64

mysql-server-5.1.71-1.el6.x86_64

php-5.3.3-26.el6.x86_64

php-mysql-5.3.3-26.el6.x86_64

root@LAMP ~]# vim /etc/httpd/conf/httpd.conf

276  ServerName www.amber.com:80  【去掉#号,并修改】

402 DirectoryIndex index.php index.html index.html.var  【添加index.php;apache默认索引页面是index.html修改成其他文件需要修改httpd.conf】

[root@LAMP ~]# httpd -t  【测试配置文件语法是否正确】

Syntax OK

[root@LAMP ~]# /etc/init.d/httpd start

正在启动 httpd:                                           [确定]

[root@LAMP ~]# /etc/init.d/mysqld start

初始化 MySQL 数据库: WARNING: The host 'LAMP' could not be looked up with resolveip.

Installing MySQL system tables...

OK

Filling help tables...

OK

Please report any problems with the /usr/bin/mysqlbug script!

                                                           [确定]

正在启动 mysqld:                                          [确定]

[root@LAMP ~]# mysqladmin -uroot password '123123'   【创建数据库用户名密码】

mysqladmin [ options] command1 command2   ;-u代表 user   -p 代表 password

Options主要分为:连接、认证相关的参数,如ssl认证、连接绑定主机,端口等

Command包括:(1)数据库:create/drop databasename

(2)监控管理:刷新缓冲(flush-XX)、连接管理(processlistkill)、集群方面管理(start-slavestop-slave

其他(status,txtended-status,password,version)】

[root@LAMP ~]# mysql -uroot -p123123   【登录数据库用户密码】

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 4

Server version: 5.1.71 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> quit

Bye

[root@LAMP ~]# vim /var/www/html/test.php

$link=mysql_connect('localhost','root','123123');

if($link) echo "

successful

";

mysql_close();

?>

保存退出

[root@LAMP ~]# service iptables stop

iptables:将链设置为政策 ACCEPT:filter                    [确定]

iptables:清除防火墙规则:                                 [确定]

iptables:正在卸载模块:                                   [确定]

宿主机测试

 

二、搭建Nginx ,实现动静分离

2.1源码包编译安装Nginx

[root@Nginx ~]# rpm -q pcre-devel zlib-devel gcc gcc-c++ make

package pcre-devel is not installed

package zlib-devel is not installed

package gcc is not installed

package gcc-c++ is not installed

make-3.81-20.el6.x86_64

[root@Nginx ~]# yum -y install  pcre-devel zlib-devel gcc gcc-c++ make

[root@Nginx ~]# rpm -q pcre-devel zlib-devel gcc gcc-c++ make

pcre-devel-7.8-6.el6.x86_64

zlib-devel-1.2.3-29.el6.x86_64

gcc-4.4.7-4.el6.x86_64

gcc-c++-4.4.7-4.el6.x86_64

make-3.81-20.el6.x86_64

【创建nginx虚拟用户】

[root@Nginx ~]# useradd -M -s /sbin/nologin nginx【禁止用户用于ssh登录,且不创建家目录】

[root@Nginx ~]# ls  【拉此包进来】

a                ansible.repo.2  c61.repo.2          nginx-1.6.0.tar.gz

[root@Nginx ~]# tar xf nginx-1.6.0.tar.gz -C /usr/src/  

[root@Nginx ~]# cd /usr/src/nginx-1.6.0/

[root@Nginx nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install

【--prefix=PATH  设置安装路径】

【--user=USER 进程用户权限】

【--group=GROUP 进程用户组权限】

【--with-http_stub_status_module 激活状态信息】

【--with-http_ssl_module 激活ssl功能】

[root@Nginx nginx-1.6.0]# cd

[root@Nginx ~]# ln -s /usr/local/nginx/sbin/nginx  /usr/local/sbin/ 【创建软连接】

[root@Nginx ~]# ll /usr/local/sbin/

总用量 0

lrwxrwxrwx. 1 root root 27 4月  21 11:19 nginx -> /usr/local/nginx/sbin/nginx

2.2 修改nginx.conf 主配置文件

[root@Nginx ~]# vim /usr/local/nginx/conf/nginx.conf

user  nginx nginx;

worker_processes  2;

error_log  logs/error.log  info;

pid        logs/nginx.pid;

 

events {

        usr epoll;

    worker_connections  1024;

}

 

http {

    include       mime.types;

    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

                      '$status $body_bytes_sent "$http_referer" '

                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;

    sendfile        on;

    keepalive_timeout  65;

    gzip  on;

    server {

  listen       80;

        server_name  www.amber.com;

        charset utf-8;

        access_log  logs/www.amber.com.access.log  main;

        location / {

            root   /web/www.amber.com;

            index  index.html index.htm;

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

                location ~\.php$ {

                        proxy_pass http://192.168.168.135:80;

                }

                location ~\.(gif|jpg|jpeg|bmp|png|swf) {

                        root /web/www.amber.com;

                }

        }

}

保存退出 ,其余删除

【因配置文件添加了几格路径,但又不存在,所以需要创建】

【ulimit  -a 查看当前系统的所有限制值 ; -S soft  ; -H hard】

[root@Nginx ~]# mkdir -p /web/www.amber.com

[root@Nginx ~]# echo "

www.amber.com

" >/web/www.amber.com/index.html 【创建index.html并把前面的内容写进来】

[root@Nginx ~]# ulimit -n  【查看当前的最大打开文件数,一般默认是1024

1024

[root@Nginx ~]# ulimit -n 65000 【即时修改,重启后就无效了】

[root@Nginx ~]# ulimit -n

65000

[root@Nginx ~]# echo "ulimit -n 65000" >> /etc/profile  【修改值永久生效】

[root@Nginx ~]# 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@Nginx ~]# nginx

[root@Nginx ~]# netstat -anpt |grep nginx

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      5979/nginx

[root@Nginx ~]# service iptables stop

iptables:将链设置为政策 ACCEPT:filter                    [确定]

iptables:清除防火墙规则:                                 [确定]

iptables:正在卸载模块:                                   [确定]

在浏览器里测试

Nginx+Apache动静分离_第1张图片Nginx+Apache动静分离_第2张图片

[root@Nginx ~]# cd /web/www.amber.com/

[root@Nginx www.amber.com]# ls

index.html

[root@Nginx www.amber.com]# ls  【拉一张图片进来】

index.html  tu.jpg

[root@Nginx www.amber.com]# cd

【LAMP】

[root@LAMP ~]# echo "" >>/var/www/html/test.php

[root@LAMP ~]# cat /var/www/html/test.php

$link=mysql_connect('localhost','root','123123');

if($link) echo "

successful

";

mysql_close();

?>

  【呈现这个样子】

Nginx+Apache动静分离_第3张图片Nginx+Apache动静分离_第4张图片

点击图片查看源

Nginx+Apache动静分离_第5张图片

【由此实现了动态PHP语言由LAMP服务器提供解析(192.168.168.135),静态图片由Nginx服务器提供解析(192.168.168.132)】

用fiddler4查看抓包

【192.168.168.132在用http 0.9抓包时失败了,用1.0 ; 1.1; 1.2; 2.0】

【192.168.168.135在用http  0.9 ; 1.0 ; 1.2 ; 2.0 抓包都成功】

Nginx+Apache动静分离_第6张图片

实验项目补充

1、出现以下情况

Nginx+Apache动静分离_第7张图片

原因:

[root@LAMP ~]# cd /var/www/html/

[root@LAMP html]# vim test.php

$link=mysql_connect('localhost','root','123123');

if($link) echo "

successful</h1>"; 【这儿缺反斜杠】

mysql_close();

?>

[root@LAMP ~]# echo "">>/var/www/html/test.php【/>这儿缺空格

配置文件里路径不对,ip不对也会导致图片无法显示

2、出现网页访问成功后不显示打开文件,而是让直接下载,解决方法:重启nginx即可;查看配置文件缺php解析,缺动态解析

 

 

你可能感兴趣的:(web服务篇)