Nginx+Apache实现动静分离案例
搭建LAMP架构部分
1.安装httpd服务
[root@localhost ~]# yum -y install httpd httpd-devel
2.在防火墙中准许的服务中添加http和https服务
[root@localhost ~]# firewall-cmd --permanent --zone=public --add-service=http
success
[root@localhost ~]# firewall-cmd --permanent --zone=public --add-service=https
success
[root@localhost ~]# firewall-cmd --reload
success
[root@localhost ~]# systemctl start httpd
3.安装mariadb数据库(快捷轻量化的数据库)
[root@lamp ~]# yum -y install mariadb mariadb-server mariadb-libs mariadb-devel
[root@lamp ~]# systemctl start mariadb
4.配置数据库相关信息
[root@lamp ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Set root password? [Y/n] y ##是否设置root密码,yes
New password:
Re-enter new password:
Password updated successfully!
Remove anonymous users? [Y/n] n ##是否删除匿名用户,no
... skipping.
Disallow root login remotely? [Y/n] n ##是否拒绝root用户远程登录,no
... skipping.
Remove test database and access to it? [Y/n] n ##是否删除测试数据库,no
... skipping.
Reload privilege tables now? [Y/n] y ##是否加载权限列表,yes
... 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!
5.安装php,建立php和mysql关联
[root@lamp ~]# yum -y install php
[root@lamp ~]# yum -y install php-mysql
6.安装php插件
[root@lamp ~]# yum install -y php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap curl curl-devel php-bcmath
7.至此lamp架构搭建完成,在网页站点中加入php首页文件
[root@lamp ~]# cd /var/www/html/
[root@lamp html]# ls
[root@lamp html]# vim index.php
phpinfo();
?>
[root@lamp html]# systemctl restart httpd
8.访问http://14.0.0.40/index.php
搭建Nginx部分
1.安装环境依赖包
yum -y install gcc gcc-c++ make pcre-devel zlib-devel
2.创建运行用户、组
useradd -M -s /sbin/nologin nginx
3.编译安装
tar zxf nginx-1.12.2.tar.gz
cd nginx-1.12.2
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
make && make install
4.以便管理员直接执行“nginx”命令就可以调用Nginx的主程序
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
5.测试语法是否有错
nginx -t
6.制作nginx的service启动脚本
vim /etc/init.d/nginx
#!/bin/bash
#chkconfig: - 99 20
#description:Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo "Usage:$0 {start|stop|restart|reload}"
exit 1
esac
exit 0
7.赋予启动脚本执行权限
chmod +x /etc/init.d/nginx
chkconfig --add nginx
8.安装elinks远程访问工具,启动nginx服务
[root@nginx nginx-1.12.2]# yum -y install elinks
[root@nginx nginx-1.12.2]# setenforce 0
[root@nginx nginx-1.12.2]# systemctl stop firewalld
[root@nginx ~]# service nginx start
9.使用elinks远程访问nginx首页进行测试
[root@nginx ~]# elinks http://14.0.0.30/
10.本来无法访问动态页面http://14.0.0.30/index.php
在nginx的主配置文件中设置动态转发的location
1.进入主配置文件将.php结尾的动态网页交给LAMP架构14.0.0.30服务器处理
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
...省略内容
location ~ \.php$ {
proxy_pass http://14.0.0.40;
}
2.再次访问动态网页http://14.0.0.30/index.php