0x00 背景
最近在做这个web应用,服务器的配置也是花了一番功夫。
0x01 给apache添加virtualhost
因为我是两个子域名都在同一台服务器上,所以需要给apache添加虚拟主机。具体做法为:
打开/etc/apache2/sites-available/000-default.conf,添加一个虚拟主机
<VirtualHost *:80>
ServerName www2.example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html2
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
0x02 codeigniter把index.php从URL里剔除
- 在/var/www/html2下添加.htaccess文件,内容如下:
RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule .* index.php/$1 [PT,L]
- 在/etc/apache2/sites-available/000-default.conf里刚刚添加的虚拟主机下找到DocumentRoot /var/www/html2这一行,在这一行下面添加:
<Directory "/var/www/html2"> AllowOverride All </Directory>
- 最后执行如下命令即可:
a2enmod rewrite service apache2 restart
0x03 为网站添加https
参考该文章https://wzyboy.im/post/799.html。如果要强制用户访问时自动跳转到https,则在.htaccess文件里添加如下两行:
RewriteCond %{HTTPS} !on [NC]
RewriteRule (.*) https://www2.example.com%{REQUEST_URI} [R=301,NC,L]
然后重启服务器。
另外,在/etc/apache2/sites-available/000-default.conf里添加Redirect这行,这可以使得用Postman发http请求时也会被自动转到https,具体有什么用处尚不了解。
<VirtualHost *:80>
ServerName www2.example.com
Redirect / https://www2.example.com/
......
</VirtualHost>
0x04 安装RESTFulSever和RESTFulClient
略。
注意:
- 安装restclient的时候要装spark,libcurl,php5-curl。
- 在使用restclient发curl请求的时候要注意域名的值,必须写完整域名,而且在使用https的时候域名前应该明确写https而非http。
0x05 安全性设置
- 在/etc/php5/apache2/php.ini里加上session.cookie_secure = 1,这样可以使cookie仅仅在https链接里会被发送,http链接下无法使用cookie。但是如果使用了codeigniter,需要在application/config/config.php里将 $config['cookie_secure']设为TRUE才行。
-
在/etc/apache2/apache2.conf里添加如下的行:
ServerTokens ProductOnly ServerSignature Off
可以关闭发生错误时apache的版本号显示。
-
在/etc/php5/apache2/php.ini里添加expose_php = off可以使得php的信息不暴露在header里。
-
为了防止SSLStrip,建议加上HSTS header,方法如下:在/etc/apache2/sites-available/default-ssl.conf里加入:
Header set Strict-Transport-Security "max-age=31536000; includeSubDomains"
然后
a2enmod mod_headers
,重启apache即可生效。 -
你还可以在
https://www.ssllabs.com/ssltest/
检测你的网站的ssl强度,建议按如下修改/etc/apache2/mods-enabled/ssl.conf
配置:SSLProtocol all -SSLv2 -SSLv3 #disable SSLv2 and SSLv3 SSLHonorCipherOrder on #perfect forward secrecy SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5:!RC4 #disable RC4
0x06 最后的配置文件总结
最后你的/etc/apache2/sites-available/000-default.conf看起来应该是这样(仅显示第二个虚拟主机的配置):
<VirtualHost *:80>
ServerName www2.example.com
Redirect / https://www2.example.com/
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html2
<Directory "/var/www/html2">
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
而/etc/apache2/sites-available/default-ssl.conf看起来是这样:
<VirtualHost _default_:443>
ServerName www2.example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html2
<Directory "/var/www/html2">
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLEngine on
Header set Strict-Transport-Security "max-age=31536000; includeSubDomains"
SSLCertificateFile /path/to/ssl.crt
SSLCertificateKeyFile /path/to/ssl.key
SSLCertificateChainFile /path/to/intermediate.pem
<FilesMatch "\.(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory /usr/lib/cgi-bin>
SSLOptions +StdEnvVars
</Directory>
BrowserMatch "MSIE [2-6]" \
nokeepalive ssl-unclean-shutdown \
downgrade-1.0 force-response-1.0
# MSIE 7 and newer should be able to use keepalive
BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown
</VirtualHost>
default-ssl.conf里的虚拟主机也是两个,此处仅显示第二个。
而.htaccess文件应该看上去像这样:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$1 [PT,L]
RewriteCond %{HTTPS} !on [NC]
RewriteRule (.*) https://www2.example.com%{REQUEST_URI} [R=301,NC,L]
codeigniter的根目录的index.php中找到
define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');
把development改成production,即可关闭php错误显示。
0x07 更新(2017.05.15)
从2017年开始,chrome不再新人startcom ssl的证书了,所以我改用letsencrypt了。letsencrypt非常方便,按照这个网站操作即可。
注意如果把多个vhost写在一个.conf文件中,certbot的程序将无法parse这个.conf文件,所以我们可以将/etc/apache2/sites-available/default-ssl.conf分为两个文件(文件名任意),并删除/etc/apache2/sites-enabled/default-ssl.conf,然后再运行certbot即可。