LANMP实现动静态分离以及负载均衡

环境:VWmare上配置的 ubuntu19.04 mysql5.7.25 + php7.2 nginx1.15.9 +apache2.4.38

目的:1、动静态分离:nginx处理css、js、jpg、png、html等静态资源,apache处理php动态文件

          2、负载均衡:将客户端的请求分发到不同的服务器上处理请求

一、安装环境:

root@ubuntu:~# apt-get install nginx
root@ubuntu:~# apt-get install php
root@ubuntu:~# apt-get install mysql
root@ubuntu:~# apt-get install apache

 二、安装php扩展(按需安装,以下仅为部分)

root@ubuntu:~# apt-get install php7.2-gd
root@ubuntu:~# apt-get install php7.2-mysql
root@ubuntu:~# apt-get install php7.2-xml
root@ubuntu:~# apt-get install php7.2-redis
root@ubuntu:~# apt-get install php7.2-fpm

默认的nginx配置文件路径 /etc/nginx/nginx.conf

默认的nginx网站配置文件路径 /etc/nginx/sites-available/default

默认的nginx访问日志目录 /var/log/nginx/access.log

默认的 apache配置文件路径 /etc/apache/apache.conf

默认的apache网站配置文件路径 /etc/apache2/sites-available/000-default.conf

默认的apache访问日志目录 /var/log/apache/access.log

默认的mysql配置路径 /etc/mysql/mysql.conf.d/mysqld.cnf

默认的网站根目录 /var/www/html

三、部署网站。

(1)在网站之家上随便下载了一个企业站,解压后扔到网站目录 

root@ubuntu:~# cd /var/www/html
root@ubuntu:/var/www/html# mkdir test.locals.com
root@ubuntu:/var/www/html/test.locals.com# cd test.locals.com
root@ubuntu:/var/www/html/test.locals.com# wget http://tj.mycodes.net/201904/PHPOK5.2.060.zip
root@ubuntu:/var/www/html/test.locals.com# unzip PHPOK5.2.060.zip
root@ubuntu:/var/www/html/test.locals.com# cd ../
root@ubuntu:/var/www/html# chown -R www-data:www-data test.locals.com
root@ubuntu:/var/www/html# chmod -R 755 test.locals.com/

 (2)创建并编辑nginx网站配置文件:nginx监听80端口

root@ubuntu:~# cd /etc/nginx/sites-available/
root@ubuntu:/etc/nginx/sites-available#cp default test.locals.com
root@ubuntu:/etc/nginx/sites-available#vim test.locals.com

#负载均衡start 

upstream test_lanmp{
        server test.locals.com:8080 weight=1 max_fails=2 fail_timeout=30s;

        #server ........  #这里可设置多个目标地址
}

#负载均衡end

#设置日志格式,最好放在/etc/nginx.conf/nginx.conf,本人为了方便修改,暂时放在这个配置文件。  log_format具体参数参考 https://nginx.org/en/docs/http/ngx_http_core_module.html

log_format  main  '$remote_addr $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '$http_user_agent $http_x_forwarded_for $request_time $upstream_response_time $upstream_addr $upstream_status';
access_log /var/log/nginx/access.log main;


server {
        listen 80;
        listen [::]:80;

        root /var/www/html/test.locals.com;
        index index.html index.php;

        server_name test.locals.com;

        location / {
                try_files $uri $uri/ =404;
        }
        location ~ \.php$ {

                ip_hash;#没有做负载均衡的状态下注释此行

                #动静态分离start
                proxy_set_header Host $host:$server_port; #设置请求的头部中主机名为请求的主机名,而不是代理的nginx的主机名

                proxy_set_header X-Real-IP      $remote_addr;

                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://test_lanmp;                

                #proxy_pass http://test.locals.com:8080;#没有做负载均衡的状态下直接指定代理的url,如果没有做负载均衡,注释掉上一行,放行此行

                #动静态分离end
        }
}

 (3)创建并编辑nginx网站配置文件:apache监听8080端口 

root@ubuntu:~# cd /etc/apache/sites-available/
root@ubuntu:~# cd /etc/apache/sites-available/
root@ubuntu:/etc/apache/sites-available# cp 000-default.conf test.local.com.conf
root@ubuntu:/etc/apache/sites-available# vim test.local.com.conf

 <VirtualHost *:8080>
    ServerName test.locals.com

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/test.locals.com

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

(4)编辑hosts文件

root@ubuntu:~# vim /etc/hosts

 127.0.0.1    localhost
127.0.1.1    ubuntu
127.0.0.1    test.locals.com
# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

四、检查nginx配置文件无误后,重启服务。

root@ubuntu:~# nginx -t
root@ubuntu:~# service nginx restart
root@ubuntu:~# service apache restart

五、查看结果。

在浏览器输入http://test.locals.com/和http://test.locals.com:8080,需要同时都可以访问才可以,否则请检查自己的网站配置。

查看apache访问日志

root@ubuntu:~# cat /var/log/apache/access.log

 

查看nginx访问日志 

root@ubuntu:~# cat /var/log/nginx/access.log

 

由上可以看出来,nginx请求的是静态文件,而apache只处理后端。

只做负载均衡的请看另一篇文章 ubuntu16.04 配置 nginx 简单负载均衡

至此,动静态分离和负载均衡配置完成。 

猜你喜欢

转载自blog.csdn.net/qq_38157006/article/details/89530909
今日推荐