Apache与Nginx实现Web页面动静分离

7 篇文章 0 订阅

一、实验环境及软件版本

  • 实验环境:VMware Workstation 15.5、Centos 7.6、X Shell 6
  • 实验所用到的软件版本及下载地址:nginx-1.12.2.tar:https://wwa.lanzous.com/i1720fk557c

二、实验步骤

  • 本实验需要创建两台虚拟机,一台作为Apache Web服务,另一台作为Nginx Web服务
  • 首先进行Apache Web服务器的配置

1、关闭防火墙及系统核心防护

setenforce 0                ## 关闭系统核心防护
systemctl stop firewalld    ## 关闭防火墙

2、安装http服务及相关开发包

yum -y install httpd httpd-devel

3、开启Apache服务

systemctl start httpd

4、打开浏览器,输入服务器IP进行访问测试
ax4Zy4.md.png

5、安装数据库

yum -y install mariadb mariadb-server mariadb-libs mariadb-devel

6、开启mariadb服务并查看服务端口是否开启

systemctl start mariadb       ## 开启mariadb服务
netstat -natp | grep 3306     ## 查看端口是否开启

7、配置数据库

[root@localhost ~]# mysql_secure_installation      ## 配置数据库

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):    ## 输入当前root用户密码,因为第一次设置所以直接回车
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y         ## 是否设置root用户密码
New password:                      ## 输入密码
Re-enter new password:             ## 确认密码
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] n       ## 是否删除匿名用户
 ... skipping.

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n       ## 是否拒绝root用户远程登录
 ... skipping.

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] n       ## 是否删除测试数据库
 ... skipping.

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y        ## 是否重新加载权限列表
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

## 到此MariaDB数据库配置完成!

8、安装PHP服务及php与mysql连接依赖包

yum -y install php
yum -y install php-mysql

9、安装PHP插件

yum -y install php-gd php-ldap php-odbc php-pear php-xml  php-xmlrpc php-mbstring php-snmp php-soap curl curl-devel php-bcmath

10、创建php首页

cd /var/www/html    ## 进入站点目录
vim index.php       ## 创建php首页

写入以下内容:

<?php
   phpinfo();
?>

在浏览器中输入http://192.168.50.134/index.php
ax4ESU.md.png

现在将首页内容进行替换:

<?php
echo "apache web"
?>

刷新浏览器再次进行访问:
ax4FYV.md.png

  • Apache服务器配置完毕,下面进行配置Nginx服务器

11、将nginx安装包上传大服务器/opt目录下,解压缩

tar zxvf nginx-1.12.2.tar.gz

12、安装编译及依赖环境

yum -y install gcc gcc-c++ zlib-devel pcre pcre-devel

13、进入目录进行安装前配置

cd nginx-1.12.2/

./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module

14、创建nginx程序用户

useradd -M -s /sbin/nologin nginx

15、编译&安装

make && make install

16、创建命令软连接

ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/

17、关闭防火墙并关闭系统核心防护功能

systemctl stop firewalld
setenforce 0

18、开启nginx服务

nginx

19、安装elinks命令行浏览器

elinks http://192.168.50.136

PS:测试完成可以输入q退出

ax4kWT.md.png

ax4iF0.png

20、开始配置动静分离,编辑nginx配置文件

vim /usr/local/nginx/conf/nginx.conf

将下面三行行首的注释符号#去掉,并修改http://后面的动态处理服务的IP地址

location ~ \.php$ {
   proxy_pass   http://192.168.50.134;
}

21、重启nginx服务

nginx -s quit      ## 关闭nginx服务
nginx              ## 开启nginx服务

22、打开浏览器,清除缓存并重启,输入:http://192.168.50.136/index.php
ax4VlF.md.png

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值