CentOS 7下安装配置proftpd搭建ftp服务器

proftpd全称:Professional FTP daemon,是针对Wu-FTP的弱项而开发的,除了改进的安全性,还具备许多Wu-FTP没有的特点,能以Stand-alone、xinetd模式运行等。ProFTP已经成为继Wu-FTP之后最为流行的FTP服务器软件,越来越多的站点选用它构筑安全高效的FTP站点,ProFTP配置方便,并有MySQL和Quota模块可供选择,利用它们的完美结合可以实现非系统账号的管理和用户磁盘的限制。<摘抄百度百科>

本章通过下载源码的方式安装,可以到官网下载最新版本:http://www.proftpd.org/

1、首先安装lrzsz方便拷贝文件到要搭建服务器的linux路径下

yum install lrzsz

2、上传文件解压

将下载完成的proftpd-1.3.6.tar.gz文件上传到指定路径,使用tar zxvf proftpd-1.3.6.tar.gz解压

3、由于使用源码安装,所以需要安装gcc编译环境

yum install gcc gcc-c++ autoconf automake

3、配置并制定安装和配置文件路径

./configure --prefix=/usr/local/proftpd --sysconfdir=/usr/local/proftpd

4、安装

make&make install

5、修改配置文件,限制匿名用户可以上传下载,但是不能删除(本章暂不详细描述权限控制部分内容)

vi /usr/local/proftpd/proftpd.conf
# This is a basic ProFTPD configuration file (rename it to 
# 'proftpd.conf' for actual use.  It establishes a single server
# and a single anonymous login.  It assumes that you have a user/group
# "nobody" and "ftp" for normal operation and anon.

ServerName                      "Welcome to FTP Server"
ServerType                      standalone
DefaultServer                   on

# Port 21 is the standard FTP port.
Port                            21

# Don't use IPv6 support by default.
UseIPv6                         off

# Umask 022 is a good standard umask to prevent new dirs and files
# from being group and world writable.
Umask                           022

# To prevent DoS attacks, set the maximum number of child processes
# to 30.  If you need to allow more than 30 concurrent connections
# at once, simply increase this value.  Note that this ONLY works
# in standalone mode, in inetd mode you should use an inetd server
# that allows you to limit maximum number of processes per service
# (such as xinetd).
MaxInstances                    30

# Set the user and group under which the server will run.
User                            nobody
Group                           nobody

# To cause every FTP user to be "jailed" (chrooted) into their home
# directory, uncomment this line.
#DefaultRoot ~

# Normally, we want files to be overwriteable.
AllowOverwrite          on

IdentLookups            off
UseReverseDNS           off

# Bar use of SITE CHMOD by default
<Limit SITE_CHMOD>
  AllowAll
</Limit>

# A basic anonymous configuration, no upload directories.  If you do not
# want anonymous users, simply delete this entire <Anonymous> section.
<Anonymous /home>
  User                          ftp
  Group                         ftp

  # We want clients to be able to login with "anonymous" as well as "ftp"
  UserAlias                     anonymous ftp

  # Limit the maximum number of anonymous logins
  MaxClients                    50

  # We want 'welcome.msg' displayed at login, and '.message' displayed
  # in each newly chdired directory.
  DisplayLogin                  welcome.msg
  DisplayChdir                  .message

  # Limit WRITE everywhere in the anonymous chroot
  <Limit DELE RMD>
    DenyAll
  </Limit>
</Anonymous>

6、关闭防火墙

systemctl stop firewalld.service

systemctl disable firewalld.service

7、启动服务器

/usr/local/proftpd/sbin/proftpd

查看进程是否已经启动

[root@localhost ~]# ps -ef |grep proftpd
nobody    1140    1  0 12:13 ?        00:00:00 proftpd: (accepting connections)
root      3182  2270  0 12:50 pts/0    00:00:00 grep --color=auto proftpd  

8、添加到启动项避免每次重启后都需要手动启动

vi /etc/rc.d/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local
/usr/local/proftpd/sbin/proftpd

由于在CentOS7中/etc/rc.d/rc.local的权限被降低了,所以需要加上可执行的权限:

chmod +x /etc/rc.d/rc.local

Centos硬盘挂载

查看系统有几块硬盘

fdisk -l

找到需要格式化的硬盘

mkfs.xfs -f /dev/vdb

挂载硬盘

新建一个挂载点

mkdir /data

执行以下命令进行挂载:

mount -t xfs /dev/vdb /data/

最后执行以下命令查看挂载是否成功

df -h
/dev/vda1 50G 18G 30G 38% /
/dev/vdb 50G 33M 50G 1% /data

CentOS 7安装Oracle Express

Oracle官方文档说交换分区的大小要设2G或者内存的两倍,但是发现512M内存设置1G还是不给装,所以创建2G的交换分区

dd if=/dev/zero of=/swapfile bs=2048 count=1048576
mkswap /swapfile
swapon /swapfile
echo '/swapfile swap swap defaults 0 0' >> /etc/fstab
swapon -a

安装完后会自动创建oracle用户和dba组之类的,但是自动创建的不会在/home下面创建用户目录,于是手动创建oracle用户,并加-m参数

useradd -m oracle

下载Oracle Express并解压

开始安装

cd Disk1
rpm -ivh oracle-xe-11.2.0-1.0.x86_64.rpm

初始化配置

/etc/init.d/oracle-xe configure

配置环境变量/etc/profile

#Oracle
TMP=/tmp; export TMP
TMPDIR=$TMP; export TMPDIR
ORACLE_HOSTNAME=centos; export ORACLE_HOSTNAME
ORACLE_UNQNAME=XE; export ORACLE_UNQNAME
ORACLE_BASE=/u01/app/oracle; export ORACLE_BASE
ORACLE_HOME=$ORACLE_BASE/product/11.2.0/xe; export ORACLE_HOME
ORACLE_SID=XE; export ORACLE_SID
PATH=/usr/sbin:$PATH; export PATH
PATH=$ORACLE_HOME/bin:$PATH; export PATH
LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib; export LD_LIBRARY_PATH
CLASSPATH=$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib; export CLASSPATH

其中ORACLE_HOSTNAME、ORACLE_UNQNAME、ORACLE_HOME、ORACLE_SID要作对应修改,不然会出错

设置环境变量生效,或重新登录

source /etcprofile

切换到oracle用户,如无意外,能连上了

[oracle@centos ~]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.2.0 Production on Wed Dec 20 15:02:37 2017

Copyright (c) 1982, 2011, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production

SQL>

自动化运维工具Ansible使用教程

一、简介

1.1 基本概念

ansible是一个基于python开发的轻量级自动化运维管理工具,可以用来批量执行命令,安装程序,支持playbook编排。它通过ssh协议来连接主机,去中心化,相对比puppet和saltstack无需安装客户即可实现文件传输、命令执行、应用部署、配置管理、任务编排等,显得更为简单与轻量。ansible只是提供一种框架,其基于模块工作的,本身没有批量部署。

1.2 核心组件

(1)、连接插件connection plugins:负责和被监控端实现通信;

(2)、host inventory:指定操作的主机,是一个配置文件里面定义监控的主机;

(3)、各种模块核心模块、command模块、自定义模块;

(4)、借助于插件完成记录日志邮件等功能;

(5)、playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务。

1.3 工具特性

(1)、no agents:不需要在被管控主机上安装任何客户端;

(2)、no server:无服务器端,使用时直接运行命令即可;

(3)、modules in any languages:基于模块工作,可使用任意语言开发模块;

(4)、yaml,not code:使用yaml语言定制剧本playbook;

(5)、ssh by default:基于SSH工作;

(6)、strong multi-tier solution:可实现多级指挥。

1.4 流程架构

未分类

1.5 优缺点

优点:

  • 轻量级,无需在客户端安装agent,更新时,只需在操作机上进行一次更新即可;
  • 批量任务执行可以写成脚本,而且不用分发到远程就可以执行;
  • 使用python编写,维护更简单;
  • 使用push方式,控制节点向其他节点推方式,可先测试变更,方便控制管理。

缺点:

基于ssh,串行,故超过500台主机效率较低;

二、安装部署

名称  主机名         IP地址

A主机 ansible-A   172.20.4.10
B主机 ansible-B   172.20.4.11
C主机 ansible-C   172.20.4.12

2.1 Ansible安装

安装方式可使用源码编译安装,也可以更新yum源后yum安装,由于依赖较多模块,编译安装易出现异常,此次采用yum安装,CentOS 6.x安装epel源后,直接可以yum安装,python版本2.6以上,在各个节点均需要安装

rpm -ivh http://mirrors.sohu.com/Fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm
yum install ansible -y

2.2 各主机SSH互信

例如:在A主机执行以下命令,将公钥发送到B主机

ssh-keygen -t rsa                             #创建公钥与私钥
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]            #将公钥传输给对端服务器

此时A服务器可以免密码登录B服务器

未分类

同样方式,可以做A到C主机,如果控制端为B主机,需要B反向将公钥发布到A主机,实现互信。

2.3 命令参数介绍

Usage: ansible [options]

Options:
-a MODULE_ARGS, --args=MODULE_ARGS                 #制定调用的模块(ansible-doc查看模块)
module arguments
--ask-vault-pass      ask for vault password         #加密文件
-B SECONDS, --background=SECONDS              #后台等待多少秒
run asynchronously, failing after X seconds
(default=N/A)
-C, --check           don't make any changes; instead, try to predict some      #不执行命令,值执行命令检查
of the changes that may occur
-D, --diff            when changing (small) files and templates, show the
differences in those files; works great with --check
-e EXTRA_VARS, --extra-vars=EXTRA_VARS                                  #调用外部变量
set additional variables as key=value or YAML/JSON
-f FORKS, --forks=FORKS                                        #一次执行并发的连接数
specify number of parallel processes to use
(default=5)
-h, --help            show this help message and exit
-i INVENTORY, --inventory-file=INVENTORY                                #调用的hosts文件
specify inventory host path
(default=/etc/ansible/hosts) or comma separated host
list.
-l SUBSET, --limit=SUBSET                                      #限定主机列表中的某台主机执行
further limit selected hosts to an additional pattern
--list-hosts          outputs a list of matching hosts; does not execute        #列出直接列表中主机
anything else
-m MODULE_NAME, --module-name=MODULE_NAME                              #调用执行模块
module name to execute (default=command)
-M MODULE_PATH, --module-path=MODULE_PATH
specify path(s) to module library (default=None)
--new-vault-password-file=NEW_VAULT_PASSWORD_FILE 
new vault password file for rekey
-o, --one-line        condense output
--output=OUTPUT_FILE  output file name for encrypt or decrypt; use - for
stdout
-P POLL_INTERVAL, --poll=POLL_INTERVAL
set the poll interval if using -B (default=15)
--syntax-check        perform a syntax check on the playbook, but do not
execute it
-t TREE, --tree=TREE  log output to this directory
--vault-password-file=VAULT_PASSWORD_FILE
vault password file
-v, --verbose         verbose mode (-vvv for more, -vvvv to enable          #命令输出详细输出
connection debugging)
--version             show program's version number and exit
Connection Options:
control as whom and how to connect to hosts

-k, --ask-pass      ask for connection password                          #需要安装sshpass  输入密码
--private-key=PRIVATE_KEY_FILE, --key-file=PRIVATE_KEY_FILE
use this file to authenticate the connection
-u REMOTE_USER, --user=REMOTE_USER                                       #ssh执行命令的用户,默认为当前执行ansible的用户
connect as this user (default=None)
-c CONNECTION, --connection=CONNECTION
connection type to use (default=smart)
-T TIMEOUT, --timeout=TIMEOUT                                      #执行命令的超时时间 (default=10)
override the connection timeout in seconds
(default=10)
--ssh-common-args=SSH_COMMON_ARGS
specify common arguments to pass to sftp/scp/ssh (e.g.
ProxyCommand)
--sftp-extra-args=SFTP_EXTRA_ARGS
specify extra arguments to pass to sftp only (e.g. -f,
-l)
--scp-extra-args=SCP_EXTRA_ARGS
specify extra arguments to pass to scp only (e.g. -l)
--ssh-extra-args=SSH_EXTRA_ARGS
specify extra arguments to pass to ssh only (e.g. -R)

Privilege Escalation Options:
control how and which user you become as on target hosts

-s, --sudo          run operations with sudo (nopasswd) (deprecated, use
become)            #sudo
-U SUDO_USER, --sudo-user=SUDO_USER      #sudo
desired sudo user (default=root) (deprecated, use
become)
-S, --su            run operations with su (deprecated, use become)
-R SU_USER, --su-user=SU_USER                                       #su 的时候切换到那个用户
run operations with su as this user (default=root)
(deprecated, use become)

2.4 配置相关文件

修改主机文件inventory:,此文件定义执行命令的主机列表

未分类

设置ansible.cfg参数

inventory =/etc/ansible/hosts             #定义资源清单inventory文件的位置,一般保持默认
library =/usr/share/my_modules/           #library指向ansible模块的目录,一般保持默认
forks =10                                 #设置多少个进程同时工作
sudo_user=root                            #设置默认执行命令的用户,也可在playbook中重新设置此参数
remote_port=22                            #制定连接被管理的管理端口,默认为22
timeout =10                               #设置SSH连接的超时时间间隔,单位为秒

2.5 测试

ansible agent -m command -a "touch /tmp/aaa" -vvv
#-m  使用command模块  -a 使用command里面支持的命令参数 -vvv 查看详细过程

未分类

三、模块介绍

ansible模块较多,对应可以查看相关文档,此处列出一下日常工作中常用的模块

【copy】模块

ansible agent -m copy -a "src=/root/test.sh dest=/tmp"

【file】

调用-s 参数,需要客户端能够无密码使用sudo命令;

ansible agent -m file -a "dest=/tmp/test.sh mode=755 owner=root group=root" -s

【script】

ansible agent -m script -a "/tmp/test.sh"

【shell】创建用户

ansible agent -m shell -a "/tmp/test.sh" 

【group】创建组

ansible agent -m group -a "name=test1 state=present" -s

【user】

ansible agent -m user -a "name=xuel home=/home/xuel state=present" -s

【yum】

可以提供的status:absent,present,installed,removed,latest
ansible agent -m yum -a "name=httpd state=latest" -s

【server】

可以提供的status:running,started,stopped,restarted,reloaded

【cron】

ansible agent -m cron -a 'name="my job" minute=*/1 hour=* day=* month=* weekday=* job="/usr/sbin/ntpdate time1.aliyun.com"'

【get_url】

ansible agent -m get_url -a "url=http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm dest=/tmp"

【synchronize】需要安装rsync

ansible agent -m synchronize -a "src=/root/test.file dest=/tmp"
模块默认使用的为推送push,如果想使用pull功能需添加mode=pull
ansible agent -m synchronize -a "mode=pull src=/tmp/test.file dest=/root/"

【ini_file】

ansible agent -m ini_file -a "dest=/tmp/test.ini section=Mongo option=Host value=127.0.0.1"

该模块Python需要安装ConfigParser

四、ansible-playbook介绍

4.1 核心组件

hosts             #执行的远程主机列表
tasks             #任务集
varniables        #内置变量或自定义变量
templates         #可替换模版
handlers          #触发操作

4.2 命令

Usage: ansible-playbook playbook.yml
ansible-playbook test1.yml             #执行剧本
ansible-vault encrypt test1.yml         #加密剧本
ansible-vault decrypt test1.yml         #加密剧本
ansible-vault view test1.yml            #加密剧本 

4.3 YAML语法

1.“—”顶行首写

2.#代码注释

3.缩进统一,不可混用空格与tab

4.缩进级别椅子

5.区分大小写

6.k/v值可以同行写也可换行写,同行使用:分割,换行需要-分割

7.一个网址的功能代码需要最少的元素包括name:task

8.一个name只能包括一个task

4.4 安装并启动mysql playbook实例

---
- hosts: agent
  remote_user: root
  tasks:
  - name: install mysql-server
    yum: name=mysql-server state=present
  - name: start mysql-server
    service: name=mysqld state=started
  - name: check mysql service
    shell: ps -ef |grep mysqld

执行次playbook将mysql数据库安装到agent服务分组里:

未分类

未分类

CentOS 配置部署 NFS

对于服务器的文件共享,可以部署一台文件共享服务器,通过 NFS 去挂载使用。

安装

CentOS 6.x

下载安装

yum -y install rpcbind nfs

配置开机启动

chkconfig rpcbind on
chkconfig nfs on

Centos 7.x

下载安装

yum -y install rpcbind nfs-utils

配置开机启动

systemctl enable nfs-server.service
systemctl enable rpcbind.service

服务端配置

配置

在服务端,编辑配置文件 /etc/exports,添加如下行

/home/uploads 192.168.221.101(rw,all_squash,anonuid=0,anongid=0)

配置说明:

192.168.221.101 客户端 ip
rw 读写 ro 只读
all_squash 无论客户端以何用户连接服务端,对服务端而言均为匿名用户
anonuid 匿名用户的 uid
anongid 匿名用户的 gid

查看共享目录的 uid 和 gid,使用命令:

ls -nd /home/nfs

uid 和 gid 最好为客户端和服务端都存在的 uid 和 gid 值。

可用的配置说明列表:

未分类

exportfs -r # 生效

若在服务启动后修改了 /etc/exports 配置文件,执行该命令后,无需重启 nfs 服务和 rpcbind 服务。对该命令的说明如下:

-a 全部挂载或卸载 /etc/exports中的内容

-r 重新读取/etc/exports 中的信息 ,并同步更新/etc/exports、/var/lib/nfs/xtab

-u 卸载单一目录(和-a一起使用为卸载所有/etc/exports文件中的目录)

-v 在export的时候,将详细的信息输出到屏幕上。

防火墙端口配置

NFS 除 111 和 2049 外,其它端口均为随机生成,若 NFS 服务端需要启用防火墙,需要固定其它端口。

参考文章:http://linux.vbird.org/linux_server/0330nfs.php#nfsserver_security

# 先添加默认端口,随后添加额外端口
firewall-cmd  --permanent    --add-port=111/tcp
firewall-cmd  --permanent    --add-port=111/udp
firewall-cmd  --permanent    --add-port=2049/tcp
firewall-cmd  --permanent    --add-port=2049/udp
...

重启防火墙,重启 rpcbind:

rpcinfo -p

启动

查看本机共享目录:

showmount -e

CentOS 6.x

service rpcbind start
service nfs start

CentOS 7.x

systemctl start rpcbind.service
systemctl start nfs-server.service

客户端配置

查看服务端共享目录:

showmount -e server_ip

配置自动挂载:

vim /etc/fstab

添加一行

192.168.221.100:/home/nfs/ /home/test/uploads/ nfs defaults 0 0

在客户端,只需要启动 rpcbind 服务,但仍需要安装 nfs

# CentOS 6.x
chkconfig rpcbind on
service rpcbind start

# CentOS 7.x
systemctl enable rpcbind
systemctl start rpcbind

挂载测试

mount -a

查看挂载

nfsstat -m

错误解决

执行 mount 报错

clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)

关闭防火墙,或者执行如下:

rpcinfo  -p  192.168.221.100

将列出端口都添加至防火墙规则,放行。

Gogs:可能是比Gitlab更好的选择

Gitlab是一个很棒的Git托管服务,几乎像GitHub一样强大。 但是,有没有能和Gitlab/Github媲美但操作更简单的项目呢?我认为 Gogs 是很好的选择。

简介

现在,GitHub已经成为首选的代码托管平台。 因为它又很多很棒的功能,操作简单,几乎所有的开发者都喜欢它。

同时,谷歌的代码托管项目准备关闭,你可以把项目转移到这里。

但如果你在写Android应用程序,或者正在创建下一个伟大的iOS游戏,或者不想让别人看到你的代码没,该怎么办?

你当然可以购买私人GitHub库,但你肯定更想把资金投资到更紧迫的事情上。

这就是 Gitlab 和 Gogs 这样的软件诞生的原因。

他们提供的服务和GitHub非常相似,不同的是,你可以把项目放在你自己的服务器上,甚至是自己的工作站上。

下面一起来进行更深入的了解。

Gitlab

Gitlab是一个强大的git服务,基本功能和对手GitHub一样。 这是一个成熟的项目和并且在不断更新。

未分类

他们最近收购了Gitorius(另一个类似GitHub的服务),所以功能将有望得到扩展(查看有关收购的新闻剪辑)。

以前是手动安装,很耗时间,问题也很多。最近安装过程有所改善。

现在有 Linux DEB/RPM 的安装包可用(称为Omnibus),解决了所有的依赖问题,简化了安装过程。

升级是一个复杂的事情,尤其是从较早的版本升级到最新版,但也没那么复杂。

然而,你可以感受到很多东西是在后台执行的。 你可以运行 Sidekiq、Unicorn、Nginx、Ruby (包括 gems) 和 Gitlab本身。

自定义安装不是那么简单,有很多不确定因素,如果出了什么差错,要自己一个个排查。

输入二进制包

现在,我们有了Gogs。 你所要做的就是运行一个的二进制包。

它是用Go语言写的,能兼容各个系统。

它能运行于 Windows,MAC,Linux,ARM 等。

未分类

安装时只需要把压缩包解压到选定的文件夹中。 就是这样。 升级也一样:只要解压压缩包即可。

这就是Go语言二进制开发的美,你可以轻松在多个平台下开发。

Gogs占用资源少,所以它很容易在系统资源上运行(它可以在Raspberry Pi运行)。

你可以用默认的配置运行,或做一些小调整。

默认的配置文件位于安装文件夹的/conf/app.ini,文档建议把自定义配置写到/custom/conf/app.ini,这样升级就不会覆盖配置。

有三个可自定义的东西:

  • 库的位置
[repository]
ROOT = !! 库的地址 !!
  • 数据库位置
[database]
PATH = !! 数据库地址 !!
  • 公钥

未分类

注意:目前,你需要运行一个SSH服务器(OpenSSH也很好),这和gitlab相同。

比较

让我们比较一下这两个产品。 我将以GitHub作为参考。

未分类

代码审查(拉取请求)可以说是缺失的最重要的功能。 这是 Gogs 在Github issues 里首要问题,Gogs的主要开发者正在尽力开发中。

但总的来说,这是一个功能丰富的、私有的Git托管服务。

运行一个 Gogs docker

我以前描述我如何我的服务器环境docker化,所以我也会把gogs作为Docker容器来运行

让我们一步一步来做。

假设我有一个应用程序的文件在我的服务器根目录/home/kayak/apps,给每个作为Docker容器的应用创建子目录。

下载并解压最新版本gogs。

$ cd /home/kayak/apps
$ wget http://gogs.dn.qbox.me/gogs_v0.5.13_linux_amd64.zip
$ unzip gogs_v0.5.13_linux_amd64.zip
$ rm gogs_v0.5.13_linux_amd64.zip

自定义配置

$ cd gogs
$ mkdir -p custom/conf
$ cd custom/conf
$ nano app.ini
[repository]
ROOT = !! this is the location where you want to keep the repositories !!

[database]
PATH = !! this is the location of your database (sqlite3 by default) !!

注: 你也可以不把gogs作为docker容器,直接运行。

现在让我们创建dockerfile

$ cd /home/kayak/apps/gogs
$ nano Dockerfile

FROM ubuntu:14.04

ENV DEBIAN_FRONTEND noninteractive

RUN sed 's/main$/main universe multiverse/' -i /etc/apt/sources.list && 
    apt-get update && apt-mark hold initscripts && 
    apt-get install -y sudo openssh-server git && 
    apt-get clean

EXPOSE 22 3000

RUN addgroup --gid 501 kayak && adduser --uid 501 --gid 501 --disabled-password --gecos 'kayak' kayak && adduser kayak sudo

WORKDIR /home/kayak
ENV HOME /home/kayak

ENTRYPOINT ["/home/kayak/boot"]

dockerfile是基于最新的Ubuntu LTS版服务器(14.04)。

然后安装 sudo, openssh 和 git,暴露端口22(用于SSH)和3000(给gogs的Web界面)。

另外,我通常会创建一个用户(这里用 kayak),它具有与我的Max box用户相同的UID/GID,以防止访问权限问题。

最后,启动shell脚本运行。

$ touch boot
$ chmod +x boot
$ nano boot
#!/bin/bash

sudo -u kayak -H touch /home/kayak/.ssh/authorized_keys
chmod 700 /home/kayak/.ssh && chmod 600 /home/kayak/.ssh/authorized_keys

# start openssh server
mkdir /var/run/sshd
/usr/sbin/sshd -D &

exec sudo -u kayak /home/kayak/gogs web

这样就运行了ssh守护进程和gogs。现在是以kayak用户运行,这样比用默认的root用户好一些。

建立镜像

$ cd /home/kayak/apps/gogs
$ docker build --rm -t apertoire/gogs .

镜像建立好就可以运行了

$ docker run -d --name gogs 
-v /etc/localtime:/etc/localtime:ro 
-v /home/kayak/apps/gogs:/home/kayak 
-p 62723:22 
-p 3000:3000 
apertoire/gogs

可以在命令行查看运行情况。

未分类

现在可以打开网页界面,它会显示一个安装页面(首次运行)。

未分类

安装完成后,就搭建好一个功能丰富的Gogs了。

未分类

总结

Gogs 是一个轻量级的、易于设置、跨平台的Git托管服务,不逊色于 Gitlab 和 GitHub。

虽然不比这二者成熟,但它有很大的潜力。

它是开源的,所以你可以帮助改善它。

我用Gogs取代Gitlab几个月了,感觉不错。

我创建42个库,感觉它的性能非常好。

我绝对推荐Gogs作为你的Git自托管服务。

Centos 6设置iptables防火墙,简单防护DDOS和CC攻击

这个教程将向您展示如何在Centos内设置一个简单的防火墙。您可以自定义那个端口开放,那个端口关闭。同时还会展示如何防御一些较为简单的攻击。
当然这个教程只是简单的向您展示防火墙的基本用法 比如 开放Apache,SSH,Email的使用端口
iptables 是一个简单的防火墙,它被安装在大多数的Linux发行版上。iptables官方手册说它仅仅是个ipv4的封包过滤,NAT工具

防御攻击

我们添加几个简单的防御规则来阻止比较简单的攻击,如果我们的服务器收到大规模的攻击,这个防御规则也可以减少大部分的垃圾数据。

iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP

这条代码就是如果收到的数据包没有tcp标示的那么就丢弃它们。

iptables -A INPUT -p tcp ! --syn -m state --state NEW -j DROP

这条代码就是如果收到的SYN数据包长时间占用服务器资源,就会自动结束掉他。

iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP

同样,添加一个XMAS数据包。

开放端口

仅需开放我们需要开放的端口即可(一定要开放SSH连接端口,否则修改完成之后可能无法连接你的linux)

iptables -A INPUT -p tcp -m tcp --dport 需要开放的端口 -j ACCEPT

完成之后,我们仅需输入命令保存即可

iptables -L -n
iptables-save | sudo tee /etc/sysconfig/iptables

然后再输入命令启动服务即可

service iptables restart

重置CentOS 7的Root密码

1 – 在启动grub菜单,选择编辑选项启动

未分类

2 – 按键盘e键,来进入编辑界面

未分类

3 – 找到Linux 16的那一行,将ro改为rw init=/sysroot/bin/sh

未分类

4 – 现在按下 Control+x ,使用单用户模式启动

未分类

5 – 现在,可以使用下面的命令访问系统

chroot /sysroot

6 – 重置密码

passwd root     #需要输入两次新密码

7 – 更新系统信息

touch /.autorelabel

8 – 退出chroot

exit

9 – 重启你的系统

reboot

CentOS 7 Docker 防火墙简单配置

禁用 firewalld 服务

systemctl disable firewalld
systemctl stop firewalld

安装 iptables 防火墙服务

yum install iptables-services

创建 iptables 配置脚本

cat >> /usr/local/bin/fired.sh <<'EOF'
#!/bin/bash

iptables -F
iptables -X
iptables -Z
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -i eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A INPUT -i eth0 -p icmp --icmp-type 8 -j ACCEPT
#iptables -A INPUT -p tcp --dport 80 -i eth0 -m state -state NEW -m recent -update -seconds 60  -hitcount 50 -j DROP
#iptables -A OUTPUT -o eth0 -m owner -uid-owner vivek -p tcp --dport 80 -m state -state NEW,ESTABLISHED  -j ACCEPT
iptables -A INPUT -i eth0 -p tcp --sport 1:1023 --dport 1:1023 --syn -j DROP
iptables -A INPUT -p tcp -i eth0 --dport 22     --sport 1024:65534 -j ACCEPT
iptables -A INPUT -p tcp --dport 80     --sport 1024:65534 -j ACCEPT
iptables -A INPUT -p tcp --dport 2376   --sport 1024:65534 -j ACCEPT
iptables -A INPUT -p tcp --dport 3306   --sport 1024:65534 -j ACCEPT

# OpenVPN Configuration
# iptables -A POSTROUTING -t nat -s 10.8.0.0/24 -o eth0 -j MASQUERADE
# iptables -A FORWARD -i tun+ -j ACCEPT
# iptables -A INPUT -s 10.8.0.0/24 -j ACCEPT
# iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# iptables -A INPUT -p TCP -i eth0 --dport 10173 --sport 1024:65534 -j ACCEPT
# iptables -A INPUT -p UDP -i eth0 --dport 10173 --sport 1024:65534 -j ACCEPT
EOF

chmod +x /usr/local/bin/fired.sh

加入开机启动项

cat >> /etc/rc.d/rc.local <<EOF

# Firewall & Docker
/usr/bin/systemctl start iptables.service
/usr/local/bin/fired.sh
/usr/bin/systemctl start docker
EOF

chmod +x /etc/rc.d/rc.local

禁用相关服务自启动

# 注: Docker 启动时会自动加入一些

systemctl disable iptables.service
systemctl disable docker

CentOS Linux 6 / 7 离线编译安装 PHP 7

安装

1、先去官网下载最新版的 PHP:

http://php.net

未分类

目前 PHP 7.2 只是第一个版本( PHP 7.2.0 ),我们目前安装的是 PHP 7.1.12 这个较为稳定的版本。如下图所示,复制 PHP 的 tar.gz 包的下载链接:

未分类

2、进入你的 CentOS ,先使用 yum 安装 wget(如果已经安装过,可忽略这步):

[root@host ~]# yum -y install wget

3、Ricky 个人是喜欢把一些临时文件放到 /tmp 目录下,所以进入 /tmp 目录,使用 wget 命令把 PHP 的 tar.gz 包下载到这里,然后解压:

[root@host ~]# cd /tmp
[root@host tmp]# wget http://cn.php.net/distributions/php-7.1.12.tar.gz
[root@host tmp]# tar -zxvf php-7.1.12.tar.gz

4、在进行编译安装之前,还要先安装有关于 PHP 的依赖包:

[root@host tmp]# yum install -y libxml2-devel libtool* curl-devel libjpeg-devel libpng-devel freetype-devel

5、还要再编译安装一个名为 libmcrypt 的包:

[root@host tmp]# wget https://svwh.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
[root@host tmp]# tar -zxvf libmcrypt-2.5.8.tar.gz 
[root@host tmp]# cd libmcrypt-2.5.8
[root@host libmcrypt-2.5.8]# ./configure && make && make install

6、进入第 3 步解压出来的 php-7.1.12 文件夹,在这里执行编译安装操作:

[root@host libmcrypt-2.5.8]# cd ../php-7.1.12
[root@host php-7.1.12]# ./configure --prefix=/usr/local/php-7.1.12 --enable-fpm --enable-opcache --with-config-file-path=/usr/local/php-7.1.12/etc --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-static --enable-sockets --enable-wddx --enable-zip --enable-calendar --enable-bcmath --enable-soap --with-zlib --with-iconv --with-freetype-dir --with-gd --with-jpeg-dir --with-xmlrpc --enable-mbstring --with-sqlite3 --with-curl --enable-ftp --with-mcrypt --with-openssl --disable-safe-mode --with-gettext
[root@host php-7.1.12]# make && make install

7、设置一个软链接:

[root@host php-7.1.12]# cd /usr/local/
[root@host local]# ln -s php-7.1.12/ php

8、添加 PHP 命令到环境变量:

[root@host local]# vi /etc/profile

键入大写字母 G 定位到最后一行,再键入小写字母 o 进入编辑模式,将以下配置复制粘贴进最后一行:

export PATH=$PATH:/usr/local/php/bin

按一次 ESC 键退出编辑模式,然后键入 “ :wq ” 保存并退出。要使改动立即生效,请执行以下命令:

[root@host local]# ./etc/profile

[root@host local]# source /etc/profile

如需查看环境变量,请执行以下命令:

[root@host local]# echo $PATH

如需查看 PHP 版本,请执行以下命令:

[root@host local]# php -v
PHP 7.1.12 (cli) (built: Dec 14 2017 15:45:28) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies

9、复制配置文件和 PHP 的启动脚本,再把 PHP 的启动脚本添加到开机自启动(还要给 php-fpm 和 rc.local 文件添加可执行权限),最后执行 PHP 的启动脚本即可:

(1)复制配置文件和 PHP 的启动脚本:
[root@host local]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@host local]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
[root@host local]# cp /tmp/php-7.1.12/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

(2)给 php-fpm 和 rc.local 文件添加可执行权限:
[root@host local]# chmod +x /etc/init.d/php-fpm
[root@host local]# chmod +x /etc/rc.d/rc.local
[root@host local]# chmod +x /etc/rc.local

(3)把 PHP 的启动脚本添加到开机自启动:
[root@host local]# echo "/etc/init.d/php-fpm start" >> /etc/rc.local

(4)执行 PHP 的启动脚本:
[root@host local]# /etc/init.d/php-fpm start

10、现在 php-7.1.12 和 libmcrypt-2.5.8 这两个文件夹可以删除了:

[root@host local]# cd /tmp
[root@host tmp]# rm -rf php-7.1.12/
[root@host tmp]# rm -rf libmcrypt-2.5.8/

至此,安装完成。

疑问及如何升级到更新版本的 PHP

为什么要设置这个软链接?主要是为了方便升级和记录当前 PHP 的版本号。具体请参见该篇文章 https://ccie.lol/knowledge-base/linux-centos-install-tomcat/ 的 “ 疑问及如何升级到更新版本的 Tomcat ” 部分。

测试

在代码根目录新建一个 index.php 的文本文件,文件内容为:

<?php
phpinfo();
?>

若 PHP 配置成功,浏览器打开该页面将会显示以下内容:

未分类