Nginx+Apache实现动静分离案例
搭建LAMP架构部分
1.安装httpd服务
[[email protected] ~]# yum -y install httpd httpd-devel
2.在防火墙中准许的服务中添加http和https服务
[[email protected] ~]# firewall-cmd --permanent --zone=public --add-service=http
success
[[email protected] ~]# firewall-cmd --permanent --zone=public --add-service=https
success
[[email protected] ~]# firewall-cmd --reload
success
[[email protected] ~]# systemctl start httpd
3.安装数据库相关软件包
[[email protected] ~]# yum -y install mariadb mariadb-server mariadb-libs mariadb-devel
[[email protected] ~]# systemctl start mariadb
4.配置数据库相关信息
[[email protected] ~]# 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
New password:
Re-enter new password:
Password updated successfully!
Remove anonymous users? [Y/n] n
… skipping.
Disallow root login remotely? [Y/n] n
… skipping.
Remove test database and access to it? [Y/n] n
… skipping.
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!
5.安装php,建立php和mysql关联
[[email protected] ~]# yum -y install php
[[email protected] ~]# yum -y install php-mysql
6.安装php插件
[[email protected] ~]# 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首页文件
[[email protected] ~]# cd /var/www/html/
[[email protected] html]# ls
[[email protected] html]# vim index.php
[[email protected] 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服务
[[email protected] nginx-1.12.2]# yum -y install elinks
[[email protected] nginx-1.12.2]# setenforce 0
[[email protected] nginx-1.12.2]# systemctl stop firewalld
[[email protected] ~]# service nginx start
9.使用elinks远程访问nginx首页进行测试
[[email protected] ~]# elinks http://14.0.0.30/
10.本来无法访问动态页面http://14.0.0.30/index.php
11.进入主配置文件将.php结尾的动态网页交给LAMP架构14.0.0.30服务器处理
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
…省略内容
location ~ .php$ {
proxy_pass http://14.0.0.40;
}
12.再次访问动态网页http://14.0.0.30/index.php