如何安全的使用centos7来搭建一台nginx+php+mariadb+svn的web服务器,并使用svn的hooks自动发布网站!

==============
如何安全的使用centos7来搭建一台nginx+php+mariadb+svn的web服务器?
使用svn通道借助hooks安全发布生产环境!
==============
今日在服务器安全上做了一些研究。
虽然入门都未达到,但是为了方便自己。
还是要记录下来,以备不时之需!
安装结束首次登入:
首先更新系统
yum update
检察selinux是否开启。
getenforce
如果显示Enforcing说明已经开启。未开启的话
vi /etc/selinux/config
找到配置SELINUX修改为enforcing
SELINUX=enforcing
查看主机名
echo $HOSTNAME
发现不合心意,
vi /etc/hostname
修改为自己想看到的名字,这样做的好处是在终端中你可以很方便的识别是哪一台主机。
这这里我们可以重启一下,让更新和主机名可以正式应用。
reboot

重新登陆后首先添加一个普通用户用于登陆。
adduser a
passwd a

修改ssh,更改端口以及禁止root直接登入。
vi /etc/ssh/sshd_config
修改其中登陆端口为1984,禁止root直接登入
Port 1984
PermitRootLogin no
重启ssh服务(千万不要关闭终端!)
systemctl restart sshd.service
新建一个终端窗口尝试连接,以确认修改是否生效!
如果你运气好,发现根本连不上。
你一定觉得这是防火墙在作怪,来试一试:
firewall-cmd –state
是不是见了鬼了,嗯防火墙压根就没工作。

其实这是selinux在搞事!没有经过selinux授权,你私自更改端口号是没法用的。
幸好,你仍然保持一个终端在连接。我们现在对selinux进行一些配置工作。在此之前,我们先按装selinux的配置工具
yum -y install policycoreutils-python
然后把修改的端口注册授权给ssh
semanage port -a -t ssh_port_t -p tcp 1984
稍等几秒种,注册成功后,新建一个终端连接,端口为你设置的端口。O shit!连上了。
我们这里查看一下selinux授权的ssh端口
semanage port -l | grep ssh
发现默认22和1984,对于我这样的强迫症一定想给那个22给删除掉。
semanage port -d -t ssh_port_t -p tcp 22
居然发现删除不了。最终确认,这种关键项,没法删除原始数据!!!
好了,先抛开selinux。
我们把系统防火墙开开,请随机启动。
systemctl enable firewalld.service
systemctl restart firewalld.service
重启号之后,在新建一个窗口来尝试连接。O shit!又连不上了。
来查一下防火墙开放的端口
firewall-cmd –list-all-zones
这里我们只需看public区域就行了。
你会发现默认开启了dhcp 和 ssh,不过刚刚我们已经把ssh的端口修改为新端口了。所以这里需要把新端口【永久】放行
firewall-cmd –zone=public –add-port=1984/tcp –permanent
重新查询已经看到放行了,重载或者重启一下防火墙。
firewall-cmd –reload
然后试试,果然连上了。在查询一下开放服务。那个ssh又让我不太爽了。好,取消它!
firewall-cmd –zone=public –remove-service=ssh

至此,防火墙,selinux,还有ssh登陆告一段路。我们可以reboot一下。
reboot

===========================

yum install epel-release
yum update //更新yum源

nginx官方安装源!
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

现在也都是php8的时代了。我们首先设置默认php5 成为php8

yum -y install https://mirrors.aliyun.com/remi/enterprise/remi-release-7.rpm
yum -y install yum-utils
yum repolist all |grep php
yum-config-manager –enable remi-php

现在我们来安装一些软件。
yum install nginx mariadb* lighttpd-fastcgi php-cli php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-mssql php-snmp php-soap php-tidy php-common php-devel php-fpm php php-mysqli –skip-broken

centos7中mysql已经被替换成为mariadb。但是在使用上没什么区别。如果没法安装nginx。那么首先添加
安装结束之后,我们在安装一个可能会用到的php组件ImageMagick
yum install ImageMagick ImageMagick-devel gcc
安装结束后执行
pecl install imagick


因为我们升级了php8 所有pecl无法使用,需要更新到最新的pecl

wget http://pear.php.net/go-pear.phar

php go-pear.phar


然后加载这个组件
echo ‘extension=imagick.so’ > /etc/php.d/imagick.ini

启动各种服务,并设置开机启动。
systemctl enable nginx.service
systemctl enable mariadb.service
systemctl enable php-fpm.service
systemctl start nginx.service
systemctl start mariadb.service
systemctl start php-fpm.service
初始化设置一下数据库
mysql_secure_installation
grant all privileges on *.* to ‘root’@’%’ identified by ‘*Pa55Word*’ with grant option;
flush privileges;

默认没有密码,首次回车。
然后根据提示设置root密码,是否远程登录,移除test数据,重载权限。结束后最好。
systemctl restart mariadb.service

来,试试能不能打开web。输入你的ip或者绑定域名。O shit!打不开。
被我们伟大的防火墙给阻止了,来添加两条条永久放行!
firewall-cmd –zone=public –add-service=http –permanent
firewall-cmd –zone=public –add-service=https –permanent
重载
firewall-cmd –reload
再试试,通啦!

这里告一段段落,我们reboot一下,开始配置nginx和php

——————–
这里备注几个命令。
lsof -i:1984
chcon
——————–

yum install svn

yum -y install cyrus-sasl cyrus-sasl-devel cyrus-sasl-plain

vi /etc/sysconfig/saslauthd

修改验证方式为:MECH=ldap

然后我们创建好通用svn配置文件

vi svnserve.conf

[general]
anon-access = none
auth-access = write
authz-db = authz
realm = svn_name

[sasl]
use-sasl = true

设置authz

[groups]
dev = 18638582808

[/]
*=

[wiosn:/]
@dev = rw

接下来我们来配置

vi /etc/saslauthd.conf

ldap_servers: ldap://127.0.0.1
ldap_search_base: ou=s2,dc=xiaowei-group,dc=com
ldap_filter: (x-uid=%u)

设置svn专用的验证方式

vi /etc/sasl2/svn.conf

pwcheck_method: saslauthd
mech_list: PLAIN LOGIN

重启认证服务

systemctl restart saslauthd.service

验证是否可以成功

testsaslauthd -u18638582808 -p

提示:

0: OK “Success.”

搞定!检出一下svn

开始配置svn服务

vi /etc/sysconfig/svnserve

OPTIONS="-r /wiosn/svnroot/repository --config-file /wiosn/svnroot/svnserve.conf --listen-port 3690"

–listen-port 3690 代表指定监听端口,svn默认是3690,

然后重启svn。就可以本地检出了!

接下来我们来捣鼓自动发布!

进入你的svnroot目录,修改hook的脚本

vi post-commit

#!/bin/sh

REPOS="$1"
REV="$2"

echo svn $REPOS,update to $REV >> /home/wiosn/svnroot/cache/logs/hooks.log
svn up /home/wiosn/svnroot/cache/wiosn
echo svn temdir update >> /home/wiosn/svnroot/cache/logs/hooks.log
echo rsync to wwwroot >> /home/wiosn/svnroot/cache/logs/hooks.log
rsync -avz --exclude-from=/home/wiosn/svnroot/cache/exclude --progress --delete /home/wiosn/svnroot/cache/wiosn/ /var/www/html/wiosn/

不要忘记 chmod +x post-commit

然后执行一下./post-commit

可以看到已经自动发布了。

打完收工!

发表回复