Ubuntu 12.04 LTS (Precise Pangolin)安装配置Apache

Apache HTTP Web服务器(Apache)是非常受欢迎的开源Web服务器软件。 本指南解释如何在Ubuntu 12.04 LTS上安装和配置Apache Web服务器。

安装Apache

1.安装Apache2 web服务器,它的文档及工具。

  1. sudo apt-get install apache2 apache2-doc apache2-utils

2.编辑Apache配置文件,调整资源使用配置。下面的配置适用于2G内存服务器。
/etc/apache2/apache2.conf:

  1. KeepAlive Off
  2.  
  3.  
  4. <IfModule mpm_prefork_module>
  5.     StartServers        4
  6.     MinSpareServers     20
  7.     MaxSpareServers     40
  8.     MaxClients          200
  9.     MaxRequestsPerChild 4500
  10. </IfModule>

配置Apache

Apache支持基于名称的虚拟主机,允许你在只有一个IP的服务器上托管多个域名的虚拟主机。虽然有多种方法设置虚拟主机,不过推荐使用如下的方法。
1.禁用默认虚拟主机

  1. sudo a2dissite 000-default.conf

2.使用文本编辑器在/etc/apache2/sites-available中创建一个example.com.conf文件,用您自己的域URL替换配置文件和文件名中example.com:
/etc/apache2/sites-available/example.com.conf:

  1. <VirtualHost *:80>
  2.      ServerAdmin [email protected]
  3.      ServerName example.com
  4.      ServerAlias www.example.com
  5.      DocumentRoot /var/www/example.com/public_html/
  6.      ErrorLog /var/www/example.com/logs/error.log
  7.      CustomLog /var/www/example.com/logs/access.log combined
  8. </VirtualHost>

如果需要添加多个虚拟主机,重复以下步骤。
3.创建网站目录及日志目录

  1. sudo mkdir -p /var/www/example.com/public_html
  2. sudo mkdir /var/www/example.com/logs

4.激活网站

  1. sudo a2ensite example.com.conf

5.重启Apache

  1. sudo systemctl restart apache2

Apache模块及脚本支持

安装Apache模块

Apache的优势之一是能够使用模块进行定制。 Apache模块的默认安装目录是/etc/apache2/mods-available/目录。
1.列出可用的Apache模块

  1. sudo apt-cache search libapache2*

2.安装任何想要的模块

  1. sudo apt-get install [module-name]

3.所有的模块都位于/etc/apache2/mods-available目录中。 如果需要,编辑任何已安装模块的.conf文件,然后启用模块

  1. sudo a2enmod [module-name]

禁用目前已启用的模块

  1. a2dismod [module-name]

安装脚本支持模块

下面的命令安装的模块是使Apache支持服务端脚本语言PHP,Ruby,Python和Perl。支持这些语言是可选的,可以根据你的需求安装。
Ruby支持:

  1. sudo apt-get install libapache2-mod-ruby

Perl支持:

  1. sudo apt-get install libapache2-mod-perl2

Python支持:

  1. sudo apt-get install libapache2-mod-python

Python支持MySQL:

  1. sudo apt-get install python-mysqldb

PHP支持:

  1. sudo apt-get install libapache2-mod-php5 php5 php-pear php5-xcache

php5-suhosin – PHP安全模块:

  1. sudo apt-get install php5-suhosin

PHP支持MySQL:

  1. sudo apt-get install php5-mysql

Ubuntu 14.04 (Trusty Tahr)安装配置Apache mod_wsgi

WSGI规范为动态Web应用程序与Web服务器通信提供了一种标准和高效的方法。 mod_wsgi提供了一个用Apache简单部署WSGI应用程序的方法。 WSGI用于部署使用框架和工具(如Django,Web.py,Werkzug,Chery.py,TurboGears和Flask)编写的应用程序。

安装依赖

执行如下命令安装依赖

  1. apt-get install apache2 python-setuptools libapache2-mod-wsgi

配置WSGI处理器

为了使mod_wsgi能够对您的应用程序提供访问支持,您需要在应用程序目录中创建一个application.wsgi文件。 应用程序目录应位于DocumentRoot外部。 以下三个部分每个呈现一个不同的application.wsgi示例文件,以说明此文件的基本结构:

Hello World WSGI配置

在此示例中,应用程序存储在/var/www/html/example.com/application目录中。 修改此示例和所有以下示例以符合部署中使用的实际文件和位置。
/var/www/html/example.com/application/application.wsgi:

  1. import os
  2. import sys
  3.  
  4. sys.path.append(‘/var/www/html/example.com/application’)
  5.  
  6. os.environ[‘PYTHON_EGG_CACHE’] = ‘/var/www/html/example.com/.python-egg’
  7.  
  8. def application(environ, start_response):
  9.     status = ‘200 OK’
  10.     output = ‘Hello World!’
  11.  
  12.     response_headers = [(‘Content-type’, ‘text/plain’),
  13.                         (‘Content-Length’, str(len(output)))]
  14.     start_response(status, response_headers)
  15.  
  16.     return [output]

您必须将应用程序的路径追加到系统路径。 PYTHON_EGG_CACHE变量的声明是可选的,但在使用Web服务器的权限执行WSGI脚本时,某些应用程序可能需要声明。

Web.py WSGI配置

在此示例中,Web.py应用程序嵌入在application.wsgi文件中。 必须安装Web.py Framework才能使以下应用程序成功运行。
/var/www/html/example.com/application/application.wsgi:

  1. import web
  2.  
  3. urls = (
  4.     ‘/(.*)’, ‘hello’
  5. )
  6.  
  7. class hello:       
  8.     def GET(self, name):
  9.         if not name:
  10.             name = ‘World’
  11.         return ‘Hello, ‘ + name + ‘!’
  12.  
  13. if __name__ == "__main__":
  14.     app.run()
  15.  
  16. app = web.application(urls, globals(), autoreload=False)
  17. application = app.wsgifunc()

Django WSGI配置

以下示例的application.wsgi文件为Django应用程序的配置:
/var/www/html/example.com/application/application.wsgi:

  1. import os
  2. import sys
  3.  
  4. sys.path.append(‘/var/www/html/example.com/application’)
  5.  
  6. os.environ[‘PYTHON_EGG_CACHE’] = ‘/var/www/html/example.com/.python-egg’
  7.  
  8. os.environ[‘DJANGO_SETTINGS_MODULE’] = ‘settings’
  9.  
  10. import django.core.handlers.wsgi
  11. application = django.core.handlers.wsgi.WSGIHandler()

在使这个例子能正常工作之前,你的系统必须已经安装有一个正常工作的Django应用程序。 DJANGO_SETTINGS_MODULE指向您的应用程序的“settings.py文件”,在本示例的情况下,该文件位于/var/www/html/example.com/application/settings.py。

配置Apache

部署以下虚拟主机配置并修改路径和域名以符合你应用程序的要求:

  1. <VirtualHost *:80>
  2.    ServerName example.com
  3.    ServerAlias www.example.com
  4.    ServerAdmin [email protected]
  5.  
  6.    DocumentRoot /var/www/html/example.com/public_html
  7.  
  8.    ErrorLog /var/www/html/example.com/logs/error.log
  9.    CustomLog /var/www/html/example.com/logs/access.log combined
  10.  
  11.    WSGIScriptAlias / /var/www/html/example.com/application/application.wsgi
  12.  
  13.    Alias /robots.txt /var/www/html/example.com/public_html/robots.txt
  14.    Alias /favicon.ico /var/www/html/example.com/public_html/favicon.ico
  15.    Alias /images /var/www/html/example.com/public_html/images
  16.    Alias /static /var/www/html/example.com/public_html/static
  17. </VirtualHost>

在这个例子中,WSGIScriptAlias指令告诉Apache VirtualHost,所有请求由指定的WSGI脚本处理。 四个Alias指令允许Apache直接从DocumentRoot提供robots.txt和favicon.ico文件以及/ images和/ static位置下的所有资源,而无需WSGI应用程序的参与。 您可以根据需要添加任意数量的Alias配置参数。
虚拟主机配置好后,重启Apache

  1. service apache2 restart

每次application.wsgi文件更改时,您都需要重新启动Web服务器。 但是,对应用程序的所有其他修改不需要重新启动Web服务器。 恭喜! 您现在已经使用mod_wsgi成功部署了WSGI应用程序。

Ubuntu 14.04安装Pritunl VPN服务器(带控制面板)

开始之前

1.更新系统

  1. sudo apt-get update && sudo apt-get upgrade

2.添加Pritunl APT仓库并更新软件包列表

  1. echo "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.0 multiverse" > /etc/apt/sources.list.d/mongodb-org-3.0.list
  2. echo "deb http://repo.pritunl.com/stable/apt trusty main" > /etc/apt/sources.list.d/pritunl.list

3.添加repo keys

  1. apt-key adv –keyserver hkp://keyserver.ubuntu.com –recv 7F0CEB10
  2. apt-key adv –keyserver hkp://keyserver.ubuntu.com –recv CF8E292A

4.更新软件包缓存

  1. sudo apt-get update

5.如果服务器配置有防火墙,我们打开Pritunl相关端口

  1. sudo iptables -A INPUT -p udp -m udp –sport 9700 –dport 1025:65355 -j ACCEPT
  2. sudo iptables -A INPUT -p tcp -m tcp –sport 9700 –dport 1025:65355 -j ACCEPT
  3. sudo iptables -A INPUT -p `your protocol here` -m `your protocol here` –sport `your_port_here` –dport 1025:65355 -j ACCEPT

安装Pritunl

1.安装Pritunl及其依赖

  1. sudo apt-get install python-software-properties pritunl mongodb-org

2.启动Pritunl服务

  1. sudo service pritunl start

3.用浏览器打开https://123.45.67.89:9700,123.45.67.89替换成你服务器IP,会看到如下界面:
未分类
4.连接服务器,安装程序已经自动填充好数据库数据,如果没问题,点击“Save”即可。

配置Pritunl

1.使用如下账号登录Pritunl
Username: pritunl
Password: pritunl
2.初始配置如下:
未分类
填充表单,点击“Save”
注意:
如果你没有购买license,SMTP设置不是必须的。
如果想购买license,点击右上角的“Upgrade to Premium“,并输入license
3.切换到Users tab,这里你可以创建你的组织和用户。点击”Add Organization“输入一个名称,点击”Add Use“把用户添加到组织。
4.切换到Servers tab,点击”Add server“,界面如下:
未分类
5.点击”Attach Organization“,关联组织到服务器

连接VPN服务器

可以使用与OpenVPN兼容的客户端,如Android或iOS,可以使用OpenVPN Connect应用。Linux可以使用官方提供的客户端。

两步获取客户端密钥
1.在你的用户名相邻处,有一个Online/Offline指示器。这附近有两个按钮,一个是连接icon,一个是下载icon。
2.下载icon就是下载密钥文件的TAR文件

VPN配置第二部分 – 设置你的流量走OpenVPN通道

此文章是http://devops.webres.wang/2016/10/debian-8-setup-secure-openvpn/的第二部分

OpenVPN配置

OpenVPN服务端配置文件/etc/openvpn/server.conf,需要更改几处。
1.设置OpenVPN推送网关配置到客户端,以让客户端流量通过OpenVPN传送。
/etc/openvpn/server.conf:

  1. # If enabled, this directive will configure
  2. # all clients to redirect their default
  3. # network gateway through the VPN, causing
  4. # all IP traffic such as web browsing and
  5. # and DNS lookups to go through the VPN
  6. # (The OpenVPN server machine may need to NAT
  7. # or bridge the TUN/TAP interface to the internet
  8. # in order for this to work properly).
  9. push "redirect-gateway def1 bypass-dhcp"

2.推荐DNS服务器地址到客户端设备
/etc/openvpn/server.conf:

  1. # Certain Windows-specific network settings
  2. # can be pushed to clients, such as DNS
  3. # or WINS server addresses.  CAVEAT:
  4. # http://openvpn.net/faq.html#dhcpcaveats
  5. # The addresses below refer to the public
  6. # DNS servers provided by opendns.com.
  7. push "dhcp-option DNS 208.67.222.222"
  8. push "dhcp-option DNS 208.67.220.220"

3.重启OpenVPN

  1. sudo systemctl restart openvpn.service

网络规则

1.创建IPv4规则:
/etc/iptables/rules.v4

  1. *filter
  2.  
  3. # Allow all loopback (lo) traffic and reject traffic
  4. # to localhost that does not originate from lo.
  5. -A INPUT -i lo -j ACCEPT
  6. -A INPUT ! -i lo -s 127.0.0.0/8 -j REJECT
  7. -A OUTPUT -o lo -j ACCEPT
  8.  
  9. # Allow ping and ICMP error returns.
  10. -A INPUT -p icmp -m state –state NEW –icmp-type 8 -j ACCEPT
  11. -A INPUT -p icmp -m state –state ESTABLISHED,RELATED -j ACCEPT
  12. -A OUTPUT -p icmp -j ACCEPT
  13.  
  14. # Allow SSH.
  15. -A INPUT -i eth0 -p tcp -m state –state NEW,ESTABLISHED –dport 22 -j ACCEPT
  16. -A OUTPUT -o eth0 -p tcp -m state –state ESTABLISHED –sport 22 -j ACCEPT
  17.  
  18. # Allow UDP traffic on port 1194.
  19. -A INPUT -i eth0 -p udp -m state –state NEW,ESTABLISHED –dport 1194 -j ACCEPT
  20. -A OUTPUT -o eth0 -p udp -m state –state ESTABLISHED –sport 1194 -j ACCEPT
  21.  
  22. # Allow DNS resolution and limited HTTP/S on eth0.
  23. # Necessary for updating the server and keeping time.
  24. -A INPUT -i eth0 -p udp -m state –state ESTABLISHED –sport 53 -j ACCEPT
  25. -A OUTPUT -o eth0 -p udp -m state –state NEW,ESTABLISHED –dport 53 -j ACCEPT
  26. -A INPUT -i eth0 -p tcp -m state –state ESTABLISHED –sport 53 -j ACCEPT
  27. -A OUTPUT -o eth0 -p tcp -m state –state NEW,ESTABLISHED –dport 53 -j ACCEPT
  28.  
  29. -A INPUT -i eth0 -p tcp -m state –state ESTABLISHED –sport 80 -j ACCEPT
  30. -A OUTPUT -o eth0 -p tcp -m state –state NEW,ESTABLISHED –dport 80 -j ACCEPT
  31. -A INPUT -i eth0 -p tcp -m state –state ESTABLISHED –sport 443 -j ACCEPT
  32. -A OUTPUT -o eth0 -p tcp -m state –state NEW,ESTABLISHED –dport 443 -j ACCEPT
  33.  
  34. # Allow traffic on the TUN interface.
  35. -A INPUT -i tun0 -j ACCEPT
  36. -A FORWARD -i tun0 -j ACCEPT
  37. -A OUTPUT -o tun0 -j ACCEPT
  38.  
  39. # Allow forwarding traffic only from the VPN.
  40. -A FORWARD -i tun0 -o eth0 -s 10.8.0.0/24 -j ACCEPT
  41. -A FORWARD -m state –state ESTABLISHED,RELATED -j ACCEPT
  42.  
  43. # Log any packets which don’t fit the rules above…
  44. # (optional but useful)
  45. -A INPUT -m limit –limit 3/min -j LOG –log-prefix "iptables_INPUT_denied: " –log-level 4
  46. -A FORWARD -m limit –limit 3/min -j LOG –log-prefix "iptables_FORWARD_denied: " –log-level 4
  47. -A OUTPUT -m limit –limit 3/min -j LOG –log-prefix "iptables_OUTPUT_denied: " –log-level 4
  48.  
  49. # then reject them.
  50. -A INPUT -j REJECT
  51. -A FORWARD -j REJECT
  52. -A OUTPUT -j REJECT
  53.  
  54. COMMIT

2.导入新规则
sudo iptables-restore < /etc/iptables/rules.v4
3.应用路由规则,以便流量可以正常发送到VPN。

  1. sudo iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE

4.保存目前已加载的规则

  1. sudo dpkg-reconfigure iptables-persistent

5.允许IPv4流量转发

  1. echo ‘net.ipv4.ip_forward=1’ | sudo tee -a /etc/sysctl.d/99-sysctl.conf

6.应用生效

  1. sudo sysctl -p

7.重启OpenVPN

  1. sudo systemctl restart openvpn.service

VPN配置第一部分 – Debian 8安装配置安全的OpenVPN服务器

OpenVPN软件选择

目前openvpn有两个软件供选择:
一个是OpenVPN Community Edition,社区版本,它的服务端和客户端的配置文件需要手动编辑,客户端凭据需要使用SCP或者SFTP复制到各自的设备。
另一个是OpenVPN Access Server,服务端应用程序,允许你使用浏览器来配置OpenVPN。
对于小场景下应用,OpenVPN Access Server可能是较好的选择,因为它配置简单,用户体验好。
不过免费版本只允许两个并发用户。我们这里介绍OpenVPN Community Edition的使用。

网络规则

OpenVPN目前不支持单个实例同时使用IPv4和IPv6。只能选择其中一个。

IPv4防火墙

1.清除任何系统可能预先存在的规则和非标准的规则链

  1. sudo iptables -F && sudo iptables -X

2.使用如下规则:
/tmp/v4:

  1. *filter
  2.  
  3. # Allow all loopback (lo) traffic and reject anything
  4. # to localhost that does not originate from lo.
  5. -A INPUT -i lo -j ACCEPT
  6. -A INPUT ! -i lo -s 127.0.0.0/8 -j REJECT
  7. -A OUTPUT -o lo -j ACCEPT
  8.  
  9. # Allow ping and ICMP error returns.
  10. -A INPUT -p icmp -m state –state NEW –icmp-type 8 -j ACCEPT
  11. -A INPUT -p icmp -m state –state ESTABLISHED,RELATED -j ACCEPT
  12. -A OUTPUT -p icmp -j ACCEPT
  13.  
  14. # Allow SSH.
  15. -A INPUT -i eth0 -p tcp -m state –state NEW,ESTABLISHED –dport 22 -j ACCEPT
  16. -A OUTPUT -o eth0 -p tcp -m state –state ESTABLISHED –sport 22 -j ACCEPT
  17.  
  18. # Allow UDP traffic on port 1194.
  19. -A INPUT -i eth0 -p udp -m state –state NEW,ESTABLISHED –dport 1194 -j ACCEPT
  20. -A OUTPUT -o eth0 -p udp -m state –state ESTABLISHED –sport 1194 -j ACCEPT
  21.  
  22. # Allow DNS resolution and limited HTTP/S on eth0.
  23. # Necessary for updating the server and keeping time.
  24. -A INPUT -i eth0 -p udp -m state –state ESTABLISHED –sport 53 -j ACCEPT
  25. -A OUTPUT -o eth0 -p udp -m state –state NEW,ESTABLISHED –dport 53 -j ACCEPT
  26. -A INPUT -i eth0 -p tcp -m state –state ESTABLISHED –sport 80 -j ACCEPT
  27. -A INPUT -i eth0 -p tcp -m state –state ESTABLISHED –sport 443 -j ACCEPT
  28. -A OUTPUT -o eth0 -p tcp -m state –state NEW,ESTABLISHED –dport 80 -j ACCEPT
  29. -A OUTPUT -o eth0 -p tcp -m state –state NEW,ESTABLISHED –dport 443 -j ACCEPT
  30.  
  31. # Allow traffic on the TUN interface.
  32. -A INPUT -i tun0 -j ACCEPT
  33. -A OUTPUT -o tun0 -j ACCEPT
  34.  
  35. # Log any packets which don’t fit the rules above…
  36. # (optional but useful)
  37. -A INPUT -m limit –limit 3/min -j LOG –log-prefix "iptables_INPUT_denied: " –log-level 4
  38. -A FORWARD -m limit –limit 3/min -j LOG –log-prefix "iptables_FORWARD_denied: " –log-level 4
  39. -A OUTPUT -m limit –limit 3/min -j LOG –log-prefix "iptables_OUTPUT_denied: " –log-level 4
  40.  
  41. # then reject them.
  42. -A INPUT -j REJECT
  43. -A FORWARD -j REJECT
  44. -A OUTPUT -j REJECT
  45.  
  46. COMMIT

注意:
TUN虚拟接口是OpenVPN用来与你的硬件网卡eth0通信的。

你可以使用sudo iptables -S来加载上面的规则。

禁用IPv6

如果你的VPN仅仅使用IPv4,IPv6应该禁用掉。
1.在文件/etc/sysctl.d/99-sysctl.conf增加如下行

  1. net.ipv6.conf.all.disable_ipv6 = 1
  2. net.ipv6.conf.default.disable_ipv6 = 1
  3. net.ipv6.conf.lo.disable_ipv6 = 1
  4. net.ipv6.conf.eth0.disable_ipv6 = 1

2.激活上面的配置:

  1. sudo sysctl -p

3.编辑/etc/hosts,注释localhost的IPv6解析
/etc/hosts:

  1. #::1     localhost ip6-localhost ip6-loopback

4.增加ip6tables规则集来拒绝所有v6流量。rules.v6文件如下:
/etc/iptables/rules.v6:

  1. *filter
  2.  
  3. -A INPUT -j REJECT
  4. -A FORWARD -j REJECT
  5. -A OUTPUT -j REJECT
  6.  
  7. COMMIT

6.强制规则立即生效

  1. sudo ip6tables-restore < /etc/iptables/rules.v6

安装配置OpenVPN

1.开始安装OpenVPN

  1. apt-get install openvpn

2.OpenVPN的服务端配置文件/etc/openvpn/server.conf从一个压缩文件解压出:

  1. gunzip -c /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz > /etc/openvpn/server.conf

3.使用make-cadir脚本来从/usr/share/doc/openvpn/examples/复制必要的文件并创建工作目录。

  1. make-cadir /etc/openvpn/easy-rsa && cd /etc/openvpn/easy-rsa/

4.创建一个软链接

  1. ln -s openssl-1.0.0.cnf openssl.cnf

5./etc/openvpn/easy-rsa/keys的权限为0700,它不允许组和其它用户来访问。基于此原因,我们在server.conf的key设置绝对路径。
/etc/openvpn/server.conf:

  1. # Any X509 key management system can be used.
  2. # OpenVPN can also use a PKCS #12 formatted key file
  3. # (see "pkcs12" directive in man page).
  4. ca /etc/openvpn/easy-rsa/keys/ca.crt
  5. cert /etc/openvpn/easy-rsa/keys/server.crt
  6. key /etc/openvpn/easy-rsa/keys/server.key  # This file should be kept secret
  7.  
  8. # Diffie hellman parameters.
  9. # Generate your own with:
  10. #   openssl dhparam -out dh1024.pem 1024
  11. # Substitute 2048 for 1024 if you are using
  12. # 2048 bit keys.
  13. dh /etc/openvpn/dh4096.pem

6./etc/openvpn/easy-rsa/vars文件用来指定OpenVPN服务器的证书颁发机构的标识信息,然后将其传递到客户端证书。 更改这些字段是可选的,你可以保持不变以减小工作量。
/etc/openvpn/easy-rsa/vars:

  1. # These are the default values for fields
  2. # which will be placed in the certificate.
  3. # Don’t leave any of these fields blank.
  4. export KEY_COUNTRY="US"
  5. export KEY_PROVINCE="CA"
  6. export KEY_CITY="SanFrancisco"
  7. export KEY_ORG="Fort-Funston"
  8. export KEY_EMAIL="[email protected]"
  9. export KEY_OU="MyOrganizationalUnit"

7.在easy-rsa目录,导入vars脚本

  1. cd /etc/openvpn/easy-rsa && source ./vars

8.运行clean-all脚本确保在keys目录中没有使用样本文件或模板
./clean-all

生成Diffie-Hellman PEM

Diffie-Hellman参数是在创建客户端的会话密钥期间当建立加密连接时使用的随机生成的数据的块。 我们将在/etc/openvpn/dh*.pem创建文件,其中*表示Diffie-Hellman密钥的位长度。 2048位是默认值,但在这里我们将使用4096位的密钥。
使用如下命令创建pem文件

  1. openssl dhparam 4096 > /etc/openvpn/dh4096.pem

将输出:

Generating DH parameters, 4096 bit long safe prime, generator 2
This is going to take a long time

加固OpenVPN

1.对服务器和连接客户端之间的TLS握手涉及的所有数据包都需要匹配HMAC签名,没有此签名的数据包将被丢弃。 取消注释(通过删除;)并编辑行:tls-auth ta.key 0#This file is secret。
/etc/openvpn/server.conf:

  1. # For extra security beyond that provided
  2. # by SSL/TLS, create an "HMAC firewall"
  3. # to help block DoS attacks and UDP port flooding.
  4. #
  5. # Generate with:
  6. #   openvpn –genkey –secret ta.key
  7. #
  8. # The server and each client must have
  9. # a copy of this key.
  10. # The second parameter should be ‘0’
  11. # on the server and ‘1’ on the clients.
  12. tls-auth /etc/openvpn/easy-rsa/keys/ta.key 0 # This file is secret

生成HMAC密钥文件,之后我们会传输到每一个客户端设备

  1. openvpn –genkey –secret /etc/openvpn/easy-rsa/keys/ta.key

2.设置使用一个有权限限制的用户来运行OpenVPN

  1. adduser –system –shell /usr/sbin/nologin –no-create-home openvpn_server

取消user和group行的注释,并编辑user行,使用以上建立的用户名。这告诉OpenVPN启动时不使用root权限而切换到openvpn_server用户。
/etc/openvpn/server.conf:

  1. # It’s a good idea to reduce the OpenVPN
  2. # daemon’s privileges after initialization.
  3. #
  4. # You can uncomment this out on
  5. # non-Windows systems.
  6. user openvpn_server
  7. group nogroup

3.在CBC模式下,更改VPN的数据通道以使用256位密钥的AES。 Blowfish-128是默认值,但是AES_CBC通常被认为是OpenVPN支持的最安全的密码和模式组合(见openvpn –show-ciphers),并且可以利用AES-NI提高性能。

  1. echo ‘cipher AES-256-CBC’ >> /etc/openvpn/server.conf

4.将数据通道的认证摘要更改为SHA-512,一个SHA-2散列函数。 SHA-1是默认值; 请参阅openvpn –show-digest,了解所有支持的摘要。

  1. echo ‘auth SHA512’ >> /etc/openvpn/server.conf

5.将VPN的控制通道限制为强密码套件。 建议在这里尽可能限制,但不是所有的密码套件都可以与所有版本的OpenVPN一起使用。 通过使用SHA 2的TLS 1.2在GCM模式下将池限制为AES,并且在CBC模式下使用SHA 1在TLS 1.0上的AES和Camellia。
echo ‘tls-cipher TLS-DHE-RSA-WITH-AES-256-GCM-SHA384:TLS-DHE-RSA-WITH-AES-128-GCM-SHA256:TLS-DHE-RSA-WITH-AES-256-CBC-SHA:TLS-DHE-RSA-WITH-CAMELLIA-256-CBC-SHA:TLS-DHE-RSA-WITH-AES-128-CBC-SHA:TLS-DHE-RSA-WITH-CAMELLIA-128-CBC-SHA’ >> /etc/openvpn/server.conf

证书和密钥对

开始之前,先进入easy-rsa目录

  1. cd /etc/openvpn/easy-rsa

服务器凭据

1.根证书(有时也称为证书颁发机构)是证书和密钥对,用于生成客户端密钥对。 在每个提示下,添加或编辑要在证书中使用的信息(或将其留空)。 使用服务器的主机名或其他标识符作为公用名,并将挑战密码留空。

  1. ./build-ca

2.然后创建服务器的私钥; 再次根据需要在信息提示处添加或编辑:

  1. /build-key-server server

客户端凭据

连接到VPN的每个客户端设备应该有自己的唯一密钥。 此外,每个密钥应该有自己的标识符(client1,client2等),但所有其他证书信息可以保持不变。 如果您稍后需要添加用户,只需重复此步骤即可。

  1. cd /etc/openvpn/easy-rsa && source ./vars && ./build-key client1

客户端配置文件

1.复制client.conf模板文件并将其打开以进行编辑。 大多数客户端需要.ovpn文件格式而不是.conf,并且文件扩展名可以在提取期间更改:

  1. cp /usr/share/doc/openvpn/examples/sample-config-files/client.conf /etc/openvpn/easy-rsa/keys/client.ovpn

2.指定OpenVPN的服务器IP
/etc/openvpn/easy-rsa/keys/client.ovpn:

  1. # The hostname/IP and port of the server.
  2. # You can have multiple remote entries
  3. # to load balance between the servers.
  4.  
  5. remote 192.0.2.0 1194

3.使用普通用户启动openvpn客户端(非windows系统使用)
/etc/openvpn/easy-rsa/keys/client.ovpn:

  1. # Downgrade privileges after initialization (non-Windows only)
  2. user nobody
  3. group nogroup

4.指定crt,key文件路径
/etc/openvpn/easy-rsa/keys/client.ovpn:

  1. # SSL/TLS parms.
  2. # See the server config file for more
  3. # description.  It’s best to use
  4. # a separate .crt/.key file pair
  5. # for each client.  A single ca
  6. # file can be used for all clients.
  7. ca /path/to/ca.crt
  8. cert /path/to/client1.crt
  9. key /path/to/client1.key

5.告诉客户端使用先前生成的HMAC密钥。 如有必要,再次指定路径。
/etc/openvpn/easy-rsa/keys/client.ovpn:

  1. # If a tls-auth key is used on the server
  2. # then every client must also have the key.
  3. tls-auth /path/to/ta.key 1

6.由于VPN服务器被告知在其配置文件中强制某些加密设置,客户端必须具有相同的设置。 将这些行添加到client.ovpn的末尾:

  1. cipher AES-256-CBC
  2. auth SHA512

如果有为控制通道指定密码套件,将这些行添加到client.ovpn:

  1. tls-cipher TLS-DHE-RSA-WITH-AES-256-GCM-SHA384:TLS-DHE-RSA-WITH-AES-128-GCM-SHA256:TLS-DHE-RSA-WITH-AES-256-CBC-SHA:TLS-DHE-RSA-WITH-CAMELLIA-256-CBC-SHA:TLS-DHE-RSA-WITH-AES-128-CBC-SHA:TLS-DHE-RSA-WITH-CAMELLIA-128-CBC-SHA

7.将所有必要的客户端文件打包到一个tarball准备传输。 具体文件有:
/etc/openvpn/easy-rsa/keys/ca.crt
/etc/openvpn/easy-rsa/keys/client1.crt
/etc/openvpn/easy-rsa/keys/client1.key
/etc/openvpn/easy-rsa/keys/client.ovpn
/etc/openvpn/easy-rsa/keys/ta.key

  1. tar -C /etc/openvpn/easy-rsa/keys -cvzf /etc/openvpn/client1.tar.gz {ca.crt,client1.crt,client1.key,client.ovpn,ta.key}

设置开机启动

  1. sudo systemctl enable openvpn.service && sudo systemctl start openvpn.service

如何使用UFW配置防火墙

安装UFW

UFW已经默认包含在Ubuntu中,不过Arch和Debian系统应该也是安装好了的。 Debian可以使用systemd启动UFW,并能够开机启动,Arch则没有设置。默认情况下,UFW的规则集为空,因此即使UFW守护程序在运行,也不会强制执行任何防火墙规则。

Arch Linux

1.安装UFW:

  1. sudo pacman -S ufw

2.启动和激活UFW systemd unit:

  1. sudo systemctl start ufw
  2. sudo systemctl enable ufw

Debian / Ubuntu

1.安装UFW:

  1. sudo apt-get install ufw

使用UFW管理防火墙规则

设置默认规则

大多数系统只需要打开少量的端口让连接进入,其余的端口则关闭。 从一个简单基础的规则开始,可以使用ufw default命令来设对入站和出站连接设置默认响应规则。 要拒绝所有入站连接并允许所有出站连接,运行:

  1. sudo ufw default allow outgoing
  2. sudo ufw default deny incoming

ufw default命令还允许使用reject参数。

添加规则

可以通过两种方式添加规则:通过端口号或使用服务名称。
例如,要允许ssh端口22上的入站和出站连接,可以运行:

  1. sudo ufw allow ssh

也可以运行:

  1. sudo ufw allow 22

同样,要拒绝某个端口(在本例中为111)上的流量,您只需运行:

  1. sudo ufw deny 111

要进一步微调规则,您还可以允许TCP或UDP的数据包。 以下将允许端口80上的TCP数据包:

  1. sudo ufw allow 80/tcp
  2. sudo ufw allow http/tcp

而这将允许1725的UDP数据包:

  1. sudo ufw allow 1725/udp

高级规则

除了基于端口的设置允许或拒绝规则,UFW还允许您通过IP地址,子网和IP地址/子网/端口组合设置允许/阻止规则。
允许一个IP地址的连接:

  1. sudo ufw allow from 123.45.67.89

允许一个特定子网的连接:

  1. sudo ufw allow from 123.45.67.89/24

允许一个特定IP地址/端口的组合连接:

  1. sudo ufw allow from 123.45.67.89 to any port 22 proto tcp

proto tcp可以删除或改为proto udp,并且allow可以根据需要更改为deny。

删除规则

要删除规则,在规则前添加delete。 如果您不再希望允许HTTP流量,您可以运行:

  1. sudo ufw delete allow 80

激活防火墙

你的规则设置完成后,ufw初始运行状态可能会输出Status:inactive。 启用UFW并强制执行防火墙规则:

  1. sudo ufw enable

同样的,禁用防火墙规则:

  1. sudo ufw disable

使用TCP Wrappers来保护你的系统

TCP wrappers是基于主机的访问控制系统。它用来阻止非授权的访问,只允许特定客户访问你服务器上的服务。

为什么使用TCP wrappers

TCP wrappers在你的服务器与任何潜在的攻击者之间创建了额外的安全层。除了访问控制功能之外,它还提供日志记录和主机名验证。TCP wrappers在大多数Linux或者类UNIX系统能开箱即用,这使得它能够容易的配置,是对目前已经存在的防火墙的完美补充。

如何判断一个程序是否支持TCP wrappers

不是所有的程序都支持TCP wrappers。程序必须与libwrap库编译。常见的服务像sshd,ftpd和telnet默认支持TCP wrappers。我们可以使用如下命令来检查是否支持TCP wrappers:

  1. ldd /path-to-daemon | grep libwrap.so

ldd命令会打印出可执行文件共享依赖列表。把ldd命令输出管道给grep,来查找是否包含libwrap.so。如果有输出,则表示支持TCP wrappers。

如何使用TCP wrappers

TCP wrappers依赖两个文件/etc/hosts.allow和/etc/hosts.deny,如果这些文件不存在,先创建:

  1. touch /etc/hosts.{allow,deny}

编辑hosts.allow和hosts.deny

没有编辑过的/etc/hosts.deny文件内容如下:

  1. #
  2. # hosts.deny    This file contains access rules which are used to
  3. #       deny connections to network services that either use
  4. #       the tcp_wrappers library or that have been
  5. #       started through a tcp_wrappers-enabled xinetd.
  6. #
  7. #       The rules in this file can also be set up in
  8. #       /etc/hosts.allow with a ‘deny’ option instead.
  9. #
  10. #       See ‘man 5 hosts_options’ and ‘man 5 hosts_access’
  11. #       for information on rule syntax.
  12. #       See ‘man tcpd’ for information on tcp_wrappers
  13. #

hosts.deny里的规则由上往下匹配,如果上面的规则匹配了,就不再往下搜索,往下的将会被忽略。一条规则的语法如下:

  1. daemons : hostnames/IPs

多个daemons或者多个hostnames/IP可以使用空格分隔。

例子

拒绝所有:
这个hosts.deny例子将会阻止所有客户访问所有进程。

  1. ALL : ALL

允许访问:
在hosts.allow文件里的规则比hosts.deny里的规则优先级更高。这就允许我们使用hosts.allow来对禁用规则设置例外。下面的规则表示允许123.45.67.89访问sshd服务:

  1. sshd : 123.45.67.89

通配符

TCP wrappers支持通配符,允许你对一批IP地址或者主机名设置规则。可用通配符来ALL, LOCAL, UNKNOWN, KNOWN和PARANOID。
ALL – 匹配所有
LOCAL – 匹配不包含.的主机名
UNKNOWN – 匹配无法解析出IP的主机名
KNOWN – 匹配可以解析出IP的主机名

使用MTR诊断网络问题

MTR是一个功能强大的网络诊断工具,能让系统管理员诊断和定位出网络错误和能提供给网络状态报告给上游提供商。MTR类似于traceroute+ping。

安装MTR

在Debian和Ubuntu系统,执行如下命令来更新系统软件仓库和升级已安装的软件包,最后安装MTR:

  1. apt-get update
  2. apt-get upgrade
  3. apt-get install mtr-tiny

对于CentOS和Fedora系统,你可以执行如下命令来升级已安装软件,并安装MTR程序:

  1. yum update
  2. yum install mtr

在Arch Linux系统,执行如下命令来更新软件包并安装MTR:

  1. pacman -Sy
  2. pacman -S mtr

你可能也需要在你的本地工作站来使用MTR诊断网络问题。如果你本地系统是Linux,你可以按照以上方法来安装MTR。
如果你的系统是Mac OS X,你可以使用Homebrew或者MacPorts安装MTR,使用Homebrew安装,执行如下命令:

  1. brew install mtr

使用MacPorts安装,命令为:

  1. port install mtr

如果你的桌面系统为Windows,可以使用WinMTR,下载链接为http://sourceforge.net/projects/winmtr/

在类Unix系统使用MTR

一旦在Linux或者Mac OS X安装好MTR,你可以使用如下语法来生成MTR报告:

  1. mtr -rw [destination_host]

例如,测试路由和到主机example.com的网络连接质量,在源主机执行如下命令:

  1. mtr -rw example.com

读取MTR报告

由于MTR报告提供了大量的信息,第一次可能会比较难阅读。下面是一个从你本地网络到google.com的MTR报告示例:

$ mtr –report google.com
HOST: example Loss% Snt Last Avg Best Wrst StDev
1. inner-cake 0.0% 10 2.8 2.1 1.9 2.8 0.3
2. outer-cake 0.0% 10 3.2 2.6 2.4 3.2 0.3
3. 68.85.118.13 0.0% 10 9.8 12.2 8.7 18.2 3.0
4. po-20-ar01.absecon.nj.panjde 0.0% 10 10.2 10.4 8.9 14.2 1.6
5. be-30-crs01.audubon.nj.panjd 0.0% 10 10.8 12.2 10.1 16.6 1.7
6. pos-0-12-0-0-ar01.plainfield 0.0% 10 13.4 14.6 12.6 21.6 2.6
7. pos-0-6-0-0-cr01.newyork.ny. 0.0% 10 15.2 15.3 13.9 18.2 1.3
8. pos-0-4-0-0-pe01.111eighthav 0.0% 10 16.5 16.2 14.5 19.3 1.3
9. as15169-3.111eighthave.ny.ib 0.0% 10 16.0 17.1 14.2 27.7 3.9
10. 72.14.238.232 0.0% 10 19.1 22.0 13.9 43.3 11.1
11. 209.85.241.148 0.0% 10 15.1 16.2 14.8 20.2 1.6
12. lga15s02-in-f104.1e100.net 0.0% 10 15.6 16.9 15.2 20.6 1.7

生成这个报告的命令为mtr –report google.com。这个命令使用了report选项为向google.com发送10个数据包然后生成报告。没有–report选项的话,mtr会一直运行。

使用Iperf诊断网络速度

安装Iperf

Debian和Ubuntu

可以在Debian和Ubuntu使用apt-get来安装

  1. apt-get install iperf

CentOS

CentOS官方源没有Iperf。我们可以使用RPMForge源来安装,不过安装前需要把这个源安装到系统里。
CentOS 7:

  1. wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el7.rf.x86_64.rpm
  2. rpm -Uvh rpmforge-release-1.5.3-1.el7.rf.x86_64.rpm
  3. yum update
  4. yum install iperf

CentOS 6

  1. wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.2-1.el6.rf.x86_64.rpm
  2. rpm -Uvh rpmforge-release-0.5.2-1.el6.rf.x86_64.rpm
  3. yum update
  4. yum install iperf

Fedora:

  1. yum update
  2. yum install iperf

Arch Linux:

  1. pacman -S iperf

Gentoo:

  1. emerge iperf

使用Iperf

Iperf需要在你要测试连接的两台服务器上安装。

TCP Clients & Servers

Iperf需要在两台机器分别扮演客户端和服务端角色。客户端来连接你要测试网速的服务器。
1.在你要测的服务器上启动服务器模式:

  1. iperf -s

将输出如下:

————————————————————
Server listening on TCP port 5001
TCP window size: 85.3 KByte (default)
————————————————————

2.在你另一台机器,连接上一台机器,把198.51.100.5替换为以上的机器IP

  1. iperf -c 198.51.100.5

类似输出如下:

————————————————————
Client connecting to 198.51.100.5, TCP port 5001
TCP window size: 45.0 KByte (default)
————————————————————
[ 3] local 198.51.100.6 port 50549 connected with 198.51.100.5 port 5001
[ ID] Interval Transfer Bandwidth
[ 3] 0.0-10.0 sec 142 MBytes 119 Mbits/sec

3.在服务端将看到如下输出:

————————————————————
Server listening on TCP port 5001
TCP window size: 85.3 KByte (default)
————————————————————
[ 4] local 198.51.100.5 port 5001 connected with 198.51.100.6 port 50549
[ ID] Interval Transfer Bandwidth
[ 4] 0.0-10.2 sec 142 MBytes 117 Mbits/sec

4.CTRL + c来停止Iperf进程

UDP Clients & Servers

使用Iperf,你也可以来测试UDP连接的最大吞吐量
1.启动UDP Iperf服务器

  1. iperf -s -u

输出如下:

————————————————————
Server listening on UDP port 5001
Receiving 1470 byte datagrams
UDP buffer size: 208 KByte (default)
————————————————————

2.在客户端连接以上的服务器,替换198.51.100.5为你自己的

  1. iperf -c 198.51.100.5 -u

-u选择表示告诉Iperf使用UDP连接。输出如下:

————————————————————
Client connecting to 198.51.100.5, UDP port 5001
Sending 1470 byte datagrams
UDP buffer size: 208 KByte (default)
————————————————————
[ 3] local 198.51.100.6 port 58070 connected with 198.51.100.5 port 5001
[ ID] Interval Transfer Bandwidth
[ 3] 0.0-10.0 sec 1.25 MBytes 1.05 Mbits/sec
[ 3] Sent 893 datagrams
[ 3] Server Report:
[ 3] 0.0-10.0 sec 1.25 MBytes 1.05 Mbits/sec 0.084 ms 0/ 893 (0%)

从输出我们可以看到1.05 Mbits/sec比我们收到数据的速度要小。这是因为Iperf默认限制了UDP客户端为1Mbit每秒。
3.你可以使用-b选项来更改,如:

  1. iperf -c 198.51.100.5 -u -b 150m

输出如下:

———————————————————–
Client connecting to 198.51.100.5, UDP port 5001
Sending 1470 byte datagrams
UDP buffer size: 208 KByte (default)
————————————————————
[ 3] local 198.51.100.6 port 41083 connected with 198.51.100.5 port 5001
[ ID] Interval Transfer Bandwidth
[ 3] 0.0-10.0 sec 145 MBytes 122 Mbits/sec
[ 3] Sent 103625 datagrams
[ 3] Server Report:
[ 3] 0.0-10.3 sec 136 MBytes 111 Mbits/sec 13.488 ms 6464/103623 (6.2%)

CentOS 7安装配置LEMP(Nginx PHP FastCGI MariaDB)

系统配置

确保你的系统为最新版本:

  1. yum update

从EPEL安装Nginx

最快最容易安装Nginx的方式是使用第三方源EPEL

  1. sudo yum install epel-release
  2. yum update
  3. yum install nginx

这些命令指安装EPEL仓库,拉取最新的metadata,之后安装Nginx

配置Nginx

systemd启动Nginx

Nginx安装完成后,需要在systemd激活和启动,可以使用systemctl命令来做

  1. systemctl enable nginx.service
  2. systemctl start nginx.service

然后你可以检查其状态确保nginx始终启动

  1. systemctl status nginx.service

配置Nginx虚拟主机

一旦nginx安装完成,你需要在配置文件配置server块。每个server块需要一个server和location指令。你可以把server块分别建立不同文件,或者直接放到一个文件/etc/nginx/nginx.conf。在这个例子里,我们使用多个配置文件。默认地,Nginx会引用/etc/nginx/conf.d目录下的.conf文件
/etc/nginx/conf.d/example.com.conf:

  1. server {
  2. listen  80;
  3. server_name www.example.com example.com;
  4. access_log /var/www/example.com/logs/access.log;
  5. error_log /var/www/example.com/logs/error.log;
  6.     
  7. location / {
  8.     root  /var/www/example.com/public_html;
  9.     index index.html index.htm index.php;
  10.     }
  11. }

你想托管额外的网站时,可以把配置文件放在/etc/nginx/conf.d目录。一旦建立好配置文件,你需要为你的public html文件,日志目录创建目录:

  1. mkdir -p /var/www/example.com/{public_html,logs}

配置文件虚拟主机后,需要重启nginx以生效配置

  1. systemctl restart nginx.service

部署PHP FastCGI

如果你的网站是PHP写的,你需要实现PHP-FastCGI以便能让Nginx能正确地处理和解析PHP代码。你可以从EPEL仓库使用YUM安装

  1. yum install php-cli php spawn-fcgi

PHP-FastCGI安装好后,需要创建一个用来启动和控制php-cgi进程的脚本。
/usr/bin/php-fastcgi:

  1. #!/bin/sh
  2. if [ `grep -c "nginx" /etc/passwd` = "1" ]; then
  3.     FASTCGI_USER=nginx
  4. elif [ `grep -c "www-data" /etc/passwd` = "1" ]; then
  5.     FASTCGI_USER=www-data
  6. elif [ `grep -c "http" /etc/passwd` = "1" ]; then
  7.     FASTCGI_USER=http
  8. else
  9. # Set the FASTCGI_USER variable below to the user that
  10. # you want to run the php-fastcgi processes as
  11.  
  12. FASTCGI_USER=
  13. fi
  14.  
  15. /usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 6 -u $FASTCGI_USER -f /usr/bin/php-cgi

脚本创建好后,设置其有执行权限

  1. chmod +x /usr/bin/php-fastcgi

把PHP-FastCGI配置为服务

PHP-FastCGI安装好后,它不会自动地在systemd注册为服务。如果你想用systemd更方便地管理PHP-FastCGI,可以把它配置为systemd服务。需要创建一个服务文件指向/usr/bin/php-fastcgi:
/etc/systemd/system/php-fastcgi.service:

  1. [Unit]
  2. Description=php-fastcgi systemd service script
  3.  
  4. [Service]
  5. Type=forking
  6. ExecStart=/usr/bin/php-fastcgi start
  7.  
  8. [Install]
  9. WantedBy=multi-user.target

服务文件创建好后,需要重载systemd守护进程以激活此服务,然后启动它。

  1. systemctl daemon-reload
  2. systemctl enable php-fastcgi.service
  3. systemctl start php-fastcgi.service

安装MariaDB

CentOS 7官方源已经没有MySQL了,已经用MariaDB替代,不过放心,这个是完全兼容MySQL的,用法一样。
1.从官方源安装MariaDB

  1. yum install mariadb-server

2.安装完成后,使用systemctl来激活并启动mariadb

  1. systemctl enable mariadb.service
  2. systemctl start mariadb.service

3.使用mysql_secure_installation命令来加固MariaDB

  1. mysql_secure_installation

到此LEMP安装完成。