部署高可用动态网站(四)

服务器硬件、部署LNMP动态网站、
网站架构、LNP+Mariadb数据库分离、Web服务器集群
Keepalived高可用、部署Ceph分布式存储
部署Git版本控制系统、优化Web服务器

一:部署Git版本控制系统

部署Git版本控制系统,管理网站代码,实现如下效果:

基于SSH协议的服务器
基于Git协议的服务器
基于HTTP协议的服务器
上传代码到版本仓库

方案

生产环境应该有一台独立的Git服务器,这里为了节约主机资源,我们使用数据库主机同时做完Git服务器,如图-1所示。

在这里插入图片描述
图-1

主机配置如表-1所示。
在这里插入图片描述
表-1

步骤

实现此案例需要按照如下步骤进行。

步骤一:部署SSH协议的版本控制服务器

1)安装软件包,创建空仓库。

[root@database ~]# yum -y install git
[root@database ~]# mkdir /var/git/
[root@database ~]# git init --bare /var/git/wordpress.git #创建空仓库
2)登陆web1服务器克隆git仓库,上传网站代码到git服务器。

[root@web1 var]# git config --global push.default simple
[root@web1 var]# git config --global user.email you@example.com
[root@web1 var]# git config --global user.name “Your Name”

[root@web1 var]# cd /var/
[root@web1 var]# git clone root@192.168.2.21:/var/git/wordpress.git
[root@web1 var]# cd /var/wordpress
[root@web1 wordpress]# cp -a /usr/local/nginx/html/* ./

[root@web1 wordpress]# git add .
[root@web1 wordpress]# git commit -m “wordpress code”
[root@web1 wordpress]# git push
root@192.168.2.21’s password:<输入192.168.2.21主机root的密码>

步骤二:部署Git协议的版本控制服务器

1)安装软件包(192.168.2.21操作)

[root@database ~]# yum -y install git-daemon
2)修改配置文件,启动Git服务

[root@database ~]# vim /usr/lib/systemd/system/git@.service
修改前内容如下:
ExecStart=-/usr/libexec/git-core/git-daemon --base-path=/var/lib/git --export-all --user-path=public_git --syslog --inetd –verbose
修改后内容如下:
ExecStart=-/usr/libexec/git-core/git-daemon --base-path=/var/git --export-all --user-path=public_git --syslog --inetd –verbose

[root@database ~]# systemctl start git.socket
[root@database ~]# systemctl status git.socket
3)客户端测试(使用web2做完客户端主机,192.168.2.12)

在web2执行clone等同于是把代码又备份了一份。

[root@web2 ~]# cd /var/
[root@web2 var]# git clone git://192.168.2.21/wordpress.git

步骤三:部署HTTP协议的版本控制服务器

1)安装软件包(192.168.2.21操作)

[root@database ~]# yum -y install httpd gitweb
2)修改配置文件

[root@database ~]# vim /etc/gitweb.conf
$projectroot = “/var/git”; #添加一行
3)启动服务

[root@database ~]# systemctl start httpd
4)客户端验证

[root@room9pc01 ~]# firefox http://192.168.2.21/git
访问网页可以查看到wordpress仓库,点击tree菜单后可以看到如图-2所示的代码。

在这里插入图片描述
图-2

二:优化Web服务器

优化Web服务器,实现如下效果:

自定义网站404错误页面
升级nginx至1.15.8版本,开启status模块
编写日志切割脚本,实现每周五备份日志
开启gzip压缩功能,提高数据传输效率
开启文件缓存功能

步骤

实现此案例需要按照如下步骤进行。

步骤一:自定义404错误页面

1)优化前测试(客户端访问一个不存在的页面)。

[root@room9pc01 ~]# firefox http://www.lab.com/git
2) 修改Nginx配置文件,自定义错误页面

[root@web1 ~]# vim /usr/local/nginx/conf/nginx.conf
error_page 404 /404.html; //自定义错误页面
[root@web2 ~]# vim /usr/local/nginx/conf/nginx.conf
error_page 404 /404.html; //自定义错误页面
[root@web3 ~]# vim /usr/local/nginx/conf/nginx.conf
error_page 404 /404.html; //自定义错误页面
3) 重启nginx

[root@web1 ~]# /usr/local/nginx/sbin/nginx -s reload
[root@web2 ~]# /usr/local/nginx/sbin/nginx -s reload
[root@web3 ~]# /usr/local/nginx/sbin/nginx -s reload

步骤二:升级nginx版本,开启status模块

1)配置、编译新的nginx(web1、web2、web3做相同操作,下面以web1为例)

[root@web1 ~]# tar -xf nginx-1.15.8.tar.gz
[root@web1 ~]# cd nginx-1.15.8
[root@web1 nginx-1.15.8]# ./configure
–with-http_ssl_module
–with-http_stub_status_module
[root@web1 nginx-1.15.8]# make
2) 备份老版本nginx,更新新版本nginx

[root@web1 nginx-1.15.8]# mv /usr/local/nginx/sbin/nginx{,.old}
[root@web1 nginx-1.15.8]# cp objs/nginx /usr/local/nginx/sbin/
3)修改配置文件

[root@web1 ~]# vim /usr/local/nginx/conf/nginx.conf
… …
location /status {
stub_status on;
allow 192.168.2.0/24;
deny all;
}
… …
4) 升级或重启服务

注意:必须在nginx-1.15.8源码包目录下执行make upgrade命令。

[root@web1 nginx-1.15.8]# make upgrade
或者手动执行killall命令杀死进程后重新启动

[root@web1 ~]# killall nginx
[root@web1 ~]# /usr/local/nginx/sbin/nginx

步骤三:编写日志切割脚本

1)编写脚本(以web1为例)

[root@web1 ~]# vim /usr/local/nginx/logbak.sh
#!/bin/bash
date=date +%Y%m%d
logpath=/usr/local/nginx/logs
mv $logpath/access.log l o g p a t h / a c c e s s − logpath/access- logpath/accessdate.log
mv $logpath/error.log l o g p a t h / e r r o r − logpath/error- logpath/errordate.log
kill -USR1 $(cat $logpath/nginx.pid)
2)创建计划任务

[root@web1 ~]# crontab -e
03 03 * * 5 /usr/local/nginx/logbak.sh

步骤四:对页面进行压缩处理

1)修改Nginx配置文件

[root@proxy ~]# cat /usr/local/nginx/conf/nginx.conf
http {
… …
gzip on; //开启压缩
gzip_min_length 1000; //小文件不压缩
gzip_comp_level 4; //压缩比率
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
//对特定文件压缩,类型参考mime.types
… …
}

步骤五:服务器内存缓存

1)如果需要处理大量静态文件,可以将文件缓存在内存,下次访问会更快。

http {
open_file_cache max=2000 inactive=20s;
open_file_cache_valid 60s;
open_file_cache_min_uses 5;
open_file_cache_errors off;
//设置服务器最大缓存2000个文件句柄,关闭20秒内无请求的文件句柄
//文件句柄的有效时间是60秒,60秒后过期
//只有访问次数超过5次会被缓存
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值