nginx+apache动静分离+discuz论坛搭建

nginx+apache动静分离+discuz论坛搭建

其实是利用nginx的反向代理功能,将静态请求交给nginx本地处理,将动态请求交给后端的apche处理

实验拓朴图如下

在这里插入图片描述

实验环境如下

关闭所有主机的防火墙
在这里插入图片描述
所有用的的软件包,都在这了
链接:https://pan.baidu.com/s/1L7j55GvLEuYLFQ1HTWLD8w
提取码:rgi1

一.首先配置mysql

安装就不说了

创建一个授权账号,给一会的discuz用
mysql> grant all on *.* to discuz@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql没有其他操作了

二.中间apache服务器

注意看主机名,在哪个主机操作,不在阐述在哪个主机操作了

apache1上

源码安装apache和php与discuz
apache

在这里插入图片描述
[root@apache1 ~]# mount /dev/cdrom /mnt/
[root@apache1 ~]# yum -y install gcc gcc-c++ apr apr-devel cyrus-sasl-devel expat-devel libdb-devel openldap-devel apr-util-devel apr-util pcre-devel pcre openssl*
[root@apache1 ~]# tar -zxvf httpd-2.4.38.tar.gz
[root@apache1 ~]# cd httpd-2.4.38/
[root@apache1 httpd-2.4.38]# ./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi --enable-ssl --enable-mpms-shared=all
[root@apache1 httpd-2.4.38]# make && make install
[root@apache1 ~]# cp /usr/local/httpd/bin/apachectl /etc/init.d/apache
[root@apache1 ~]# vim /usr/local/httpd/conf/httpd.conf
在这里插入图片描述
[root@apache1 ~]# /etc/init.d/apache start
[root@apache1 ~]# echo "<h1>This http 1</h1>"> /usr/local/httpd/htdocs/index.html

在这里插入图片描述

PHP

[root@apache1 ~]# yum -y install php-mcrypt libmcrypt libmcrypt-devel autoconf freetype gd libmcrypt libpng libpng-devel libjpeg libxml2 libxml2-devel zlib curl curl-devel re2c libmcrypt-devel freetype-devel libjpeg-devel bzip2-devel
会报一些包找不到,不用管
[root@apache1 ~]# tar zxvf libmcrypt-2.5.7.tar.gz
[root@apache1 ~]# cd libmcrypt-2.5.7/
[root@apache1 libmcrypt-2.5.7]# ./configure --prefix=/usr/local/libmcrypt && make && make install

[root@apache1 ~]# cd
[root@apache1 ~]# tar zxvf php-5.6.36.tar.gz
[root@apache1 php-5.6.36]# ./configure --prefix=/usr/local/php5.6 --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-fpm --enable-sockets --enable-sysvshm --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --with-mhash --with-mcrypt=/usr/local/libmcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/usr/local/php5.6/etc/ --with-bz2 --enable-maintainer-zts --with-apxs2=/usr/local/httpd/bin/apxs
[root@apache1 php-5.6.36]# make && make install
[root@apache1 php-5.6.36]# cd
[root@apache1 php-5.6.36]# cp php.ini-production /usr/local/php5.6/etc/php.ini
[root@apache1 php-5.6.36]# vim /usr/local/httpd/conf/httpd.conf
在这里插入图片描述
在这里插入图片描述

AddType application/x-httpd-php .php .phtml

[root@apache1 php-5.6.36]# cd
[root@apache1 ~]# vim /usr/local/httpd/htdocs/index.php

<?php
phpinfo();
?>

[root@apache1 ~]# vim /usr/local/httpd/htdocs/test.php

<?php
$link=mysql_connect('192.168.1.11','discuz','123456');
if ($link)echo "connection success......";
mysql_close();
?>

[root@apache1 ~]# /etc/init.d/apache restart

访问测试
在这里插入图片描述
在这里插入图片描述

discuz

[root@apache1 ~]# cd
[root@apache1 ~]# unzip Discuz_7.2_FULL_SC_UTF8.zip
[root@apache1 ~]# cp -r upload/ /usr/local/httpd/htdocs/discuz
[root@apache1 ~]# chown -R daemon:daemon /usr/local/httpd/htdocs/discuz/
[root@apache1 ~]# vim /usr/local/php5.6/etc/php.ini
在这里插入图片描述
[root@apache1 ~]# /etc/init.d/apache restart

安装,同意,下一步我就不多说了
http://192.168.1.13/discuz/install/index.php
在这里插入图片描述
有个什么《康盛改善计划》的说明,这边直接跳过就可以
在这里插入图片描述

然后把这些安装好的东西,给apache2传过去

[root@apache1 ~]# scp -r /usr/local/httpd/ root@192.168.1.14:/usr/local
[root@apache1 ~]# scp -r /usr/local/php5.6 root@192.168.1.14:/usr/local
[root@apache1 ~]# scp -r /usr/local/libmcrypt root@192.168.1.14:/usr/local
[root@apache1 ~]# scp -r /usr/local/httpd/htdocs/discuz/ root@192.168.1.14:
/usr/local/httpd/htdocs/

论坛也要给nginx上传一个,因为论坛中也有静态资源,让nginx去处理
[root@apache1 ~]# scp -r /usr/local/httpd/htdocs/discuz/ root@192.168.1.12:/usr/local/nginx/

apache2

[root@apache2 ~]# yum -y install gcc gcc-c++ apr apr-devel cyrus-sasl-devel expat-devel libdb-devel openldap-devel apr-util-devel apr-util pcre-devel pcre openssl*
[root@apache2 ~]# yum -y install php-mcrypt libmcrypt libmcrypt-devel autoconf freetype gd libmcrypt libpng libpng-devel libjpeg libxml2 libxml2-devel zlib curl curl-devel re2c bzip2-devel libmcrypt-devel freetype-devel libjpeg-devel
[root@apache2 ~]# cp /usr/local/httpd/bin/apachectl /etc/init.d/apache
[root@apache2 ~]# /etc/init.d/apache start
[root@apache2 ~]# chown -R daemon:daemon /usr/local/httpd/htdocs/discuz/

访问测试
这是因为两个discuz后端的数据库都是一个,数据都是在数据库中的,所以其中数据都是一样的
在这里插入图片描述

三.配置nginx反向代理实现动静分离

1)首先安装nginx

[root@nginx ~]# mount /dev/cdrom /mnt/
[root@nginx ~]# yum install -y gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre pcre-devel在这里插入图片描述
[root@nginx ~]# tar -zxvf nginx-1.10.3.tar.gz
[root@nginx nginx-1.10.3]# cd
[root@nginx nginx-1.10.3]# ./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre
[root@nginx nginx-1.10.3]# make && make install
[root@nginx nginx-1.10.3]# cd
[root@nginx ~]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
[root@nginx ~]# nginx
在这里插入图片描述

2)配置动静分离

[root@nginx nginx]# cd /usr/local/nginx/
[root@nginx nginx]# mkdir nginx
[root@nginx nginx]# echo "This nginx" > nginx/index.html
[root@nginx nginx]# mv discuz/ nginx
[root@nginx nginx]# chmod -R nobody:nobody nginx/discuz/

[root@nginx ~]# cd /usr/local/nginx/conf/
[root@nginx conf]# rm -rf nginx.conf
[root@nginx conf]# vim nginx.com
直接粘贴我下面的


#user  nobody;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    upstream apache {
        server 192.168.1.13:80 weight=1;
        server 192.168.1.14:80 weight=1;}

    upstream nginx {
        server www.nginx.com:808 weight=1;}


    #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;
    server {
        listen       808;
        location / {
            root   nginx;
            index  index.html index.htm;
        }
        #error_page   500 502 503 504  /50x.html;
        #location = /50x.html {
         #   root   html;
       # }
    }
#动静分离
    server {
          listen       80;
          server_name  www.nginx.com;
          root   html;

    location ~ .*\.php$ {
          proxy_pass http://apache;
          proxy_redirect default;} #这句的作用在转发给后端apache的主机头中写上客户端真实IP

    location / {
          proxy_pass http://nginx;}
}
}
要实现动静分离实际上location非常简单,所有.php结尾的文件去到动态组即可,其他全部(静态)本地nginx处理 但是这样nginx本身上也要安装discuz论坛,如果有静态请求,就去nginx本地discuz目录去找

添加hosts
[root@nginx conf]# vim /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.12 www.nginx.com

windwos中也添加
进到C:\Windows\System32\drivers\etc 把那个hosts文件,复制粘贴到桌面
使用记事本打开,添加如下
192.168.1.12 www.nginx.com
在这里插入图片描述
保存后,再拖回原来的位置,覆盖原来的hosts文件

四.测试轮询,和动静分离,discuz轮询

首先测试轮询,到apahce1和apache2默认页面中修改成不同的

[root@apache1 ~]# echo "This is apache1" > /usr/local/httpd/htdocs/index.php

[root@apache2 ~]# echo "This is apache2" > /usr/local/httpd/htdocs/index.php

测试轮询和动分离

访问测试,刷新应该是轮询的 .php页面的去了后台的apache上
在这里插入图片描述
在这里插入图片描述

测试静分离

可以看到后面是html结尾的,是静态资源,去到了nginx上找,并返回
http://www.nginx.com/index.html
在这里插入图片描述

测试discuz轮询

这个需要通过日志来看
先去到apahce1,和apache2中都运行这个命令
[root@apache1 ~]# tail -f /usr/local/httpd/logs/access_log
[root@apache2 ~]# tail -f /usr/local/httpd/logs/access_log

去到mysql那个终端,方便一会看效果

浏览器访问 http://www.nginx.com/discuz/index.php,并刷新多次
在这里插入图片描述

这两个apache终端应该,都有这提示就对了,表示这个终端有变动,有日志产生
在这里插入图片描述

实验完毕 !!!

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值