本次实验利用Nginx实现简单的动静分离
首先,先了解下Nginx这个工具,有那些优缺点
优点:高并发量,并且能保持较低的内存资源消耗,
高度化模块化设计,安装配置编写模块相对比较简单
稳定性高,出现bug相对容易排查,通过日志就可以解决大部分问题
强大的正则支持,
可以作为web服务器使用,还可以作为负载均衡器,支持php和http代理服务器
工作在网络7层之上,可针对http应用做一些分流的策略
缺点:仅支持http、https、Email协议
健康检查只能通过端口检测,不支持url检测,比如客户端正在上传文件中出现故障,nginx则会直接更换一台服务器重新处理,用户体验感不佳
实验括朴图
部署LNMP动态网站
第一步:部署LNMP环境,
[root@centos7 ~]# yum -y install gcc openssl-devel pcre-devel #安装依赖包
[root@centos7 ~]# tar -xf nginx-1.12.2.tar.gz #将下载的nginx源码包解压
[root@centos7 ~]# cd nginx-1.12.2
[root@centos7 nginx-1.12.2]# ./configure --with-http_ssl_module --with-http_stub_status_module #源码编译安装和相关模块
[root@centos7 nginx-1.12.2]# make && make install #进行编译
[root@centos7 ~]# yum -y install mariadb mariadb-server mariadb-devel #安装mariadb数据库
[root@centos7 ~]# yum -y install php php-mysql php-fpm #安装动态网站及相关支持
启动服务,(nginx,mariadb,php-fpm)
[root@centos7 ~]# /usr/local/nginx/sbin/nginx #启动Nginx服务
[root@centos7 ~]# echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local
[root@centos7 ~]# chmod +x /etc/rc.local
[root@centos7 ~]# ss -utnlp | grep :80 #查看端口信息
[root@centos7 ~]# systemctl start mariadb #启动mariadb服务器
[root@centos7 ~]# systemctl enable mariadb #设置开机自启
[root@centos7 ~]# systemctl start php-fpm #启动php-fpm服务
[root@centos7 ~]# systemctl enable php-fpm #设置开机自启
设置防火墙与SELinux.
[root@centos7 ~]# firewall-cmd --set-default-zone=trusted
[root@centos7 ~]# setenforce 0
[root@centos7 ~]# sed -i '/SELINUX/s/enforcing/permissive/' /etc/selinux/config
因为是源码安装,所以nginx本身不支持systemd管理,要使用的话需要手动编写服务的service文件,存储路径为/usr/lib/systemd/system/下面,后缀需为.service结尾才能识别
[root@centos7 ~]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=The Nginx HTTP Server #描述信息
After=network.target remote-fs.target nss-lookup.target #指定启动nginx之前需要其他的其他服务,如network.target等
[Service]
Type=forking #Type为服务的类型,仅启动一个主进程的服务为simple,需要启动若干子进程的服务为forking
ExecStart=/usr/local/nginx/sbin/nginx #设置执行systemctl start nginx后需要启动的具体命令.
ExecReload=/usr/local/nginx/sbin/nginx -s reload #设置执行systemctl reload nginx后需要执行的具体命令.
ExecStop=/bin/kill -s QUIT ${MAINPID} #设置执行systemctl stop nginx后需要执行的具体命令.
[Install]
WantedBy=multi-user.target
保存,退出,最后测试下文件是否有错误,可以用systemctl restart nginx重启下服务,如果无报错,则正确,
第二步
环境基本配置完毕,接下来修改Nginx主配置文件,实现动静分离
修改配置文件,通过两个location实现动静分离,一个location匹配动态页面,一个loation匹配其他所有页面。注意修改默认首页为index.php
[root@centos7 ~]# vim /usr/local/nginx/conf/nginx.conf
...省略部分配置文件内容...
location / {
root html;
index index.php index.html index.htm; #大概45行这里,添加个index,php动态选项
}
...省略部分配置文件内容...
location ~ \.php$ { #大概65行这里,
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
...省略部分配置文件内容...
[root@centos7 ~]# /usr/local/nginx/sbin/nginx -s reload #重新加载配置
第三步:
为网站提前创建一个数据库、添加账户并设置该账户有数据库访问权限
[root@centos7 ~]# mysql
MariaDB [(none)]> create database wordpress character set utf8mb4;
#创建数据库,数据库名称为wordpress,该数据库支持中文(character set utf8mb4)
MariaDB [(none)]> grant all on wordpress.* to wordpress@'localhost' identified by 'wordpress';
#语法格式:grant 权限 on 数据库名.表名 to 用户名@客户端主机 identified by 密码
#创建用户并授权,用户名为wordpress,该用户对wordpress数据库下的所有表有所有权限
#wordpress用户的密码是wordpress,授权该用户可以从localhost主机登录数据库服务器
#all代表所有权限(wordpress用户可以对wordpress数据库中所有表有所有权限)
#wordpress.*代表wordpress数据库中的所有表
MariaDB [(none)]> grant all on wordpress.* to wordpress@'192.168.2.11' identified by 'wordpress';
MariaDB [(none)]> flush privileges; #刷新权限
MariaDB [(none)]> exit #退出数据库
第四步:
上线php动态网站代码,这里以wordpress为例
[root@centos7 ~]# cd wordpress
[root@centos7 wordpress]# tar -xf wordpress-5.0.3-zh_CN.tar.gz
[root@centos7 wordpress]# cp -r wordpress/* /usr/local/nginx/html/ 复制的时候记得同时把权限复制
[root@centos7 wordpress]# chown -R apache:apache /usr/local/nginx/html/ #动态网站运行过程中,php脚本需要对网站目录有读写权限,而php-fpm默认启动用户为apache
最后,使用客户端访问web服务器的ip,这里是192.168.2.11
首次进入会自动进入config配置页面,初始化网站,设置密码,用户名,数据库主机为本机ip 192.168.2.11,最后登陆访问就行了,