For the performance, please refer to the study of SaltwaterC at here.
This tutorial is writing for setting up the highest secured web server. Please also to apply the "Optional" steps mentioned below for making the highest secured web server.
Prerequisite
Select
OpenSSH
and Mail Server
when installing Ubuntu Server 11.10
.Update the fresh install system to the latest status.
sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade
If the kernel or kernel modules have been updated, you are required to reboot your system before going further.
Step 1 - Installation of PHP5 and MariaDB
Since MySQL is now owned by Oracle, the developers of previous MySQL reformed and developed MariaDB under GPL v2. It is compatible to MySQL and running much faster than MySQL too. You can use MariaDB as alternative. The commands and API are the same, such as "
mysql -u root -p
". Prepare for installation of MariaDB
sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 1BB943DB
sudo nano /etc/apt/sources.list.d/mariadb.list
Append the following lines.
deb http://mirror2.hs-esslingen.de/mariadb/repo/5.2/ubuntu natty main
deb-src http://mirror2.hs-esslingen.de/mariadb/repo/5.2/ubuntu natty main
** Yes, it is Natty as the Oneiric is not available at the moment. Natty version is compatible to Ubuntu 11.10.
Save the change and install the following.
sudo apt-get update
sudo apt-get install mariadb-server mariadb-client php5-cgi php5 php5-cli php5-mysql php5-curl php5-gd php5-idn php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl php5-xcache php5-suhosin php5-ffmpeg apache2-utils mini-httpd
Step 1a - Apparmour of MySQL (Essential)
Make change to the usr.sbin.mysqld at /etc/apparmor.d according to this link in order to make MariaDB compatible to MySQL in Ubuntu 11.10.
Step 2 - Installation of Hiawatha
Install required dependenices for Hiawatha.
sudo apt-get install libc6-dev libssl-dev dpkg-dev debhelper fakeroot libxml2-dev libxslt1-dev
Download the latest version of Hiawatha (the current version at this writing is 7.8.2).
wget http://www.hiawatha-webserver.org/files/hiawatha-7.8.2.tar.gz
tar -xzvf hiawatha-7.8.2.tar.gz
cd hiawatha-7.8.2
./configure
make deb
cd ..
sudo dpkg -i hiawatha_7.8.2_amd64.deb
or
sudo dpkg -i hiawatha_7.8.2_i386.deb
Step 3 - Configure PHP5 (Optional for security only)
The following settings are for making PHP5 more secure.
sudo nano /etc/php5/cgi/php.ini
Make changes as is.
cgi.rfc2616_headers = 1
zlib.output_compression = On
zlib.output_compression_level = 6
display_errors = Off
log_errors = On
allow_url_fopen = Off
safe_mode = On
expose_php = Off
enable_dl = Off
disable_functions = system, show_source, symlink, exec, dl, shell_exec, passthru, phpinfo, escapeshellarg, escapeshellcmd
cgi.fix_pathinfo = 0
*** According to the author of Hiawatha, the cgi.fix_pathinfo should be set to 0 at this moment.
*some PHP applications may require
safe_mode = off
Step 4 - Configure php-fcgi (PHP's FastCGI)
sudo nano /etc/hiawatha/php-fcgi.conf
Uncomment the following line and change it as is.
Server = /usr/bin/php5-cgi ; 127.0.0.1:2005 ; www-data ; /etc/php5/cgi/php.ini
sudo php-fcgi -c /etc/hiawatha/php-fcgi.conf
If you make any change on
php-fcgi.conf
, make sure to restart it by the following commands.sudo php-fcgi -k -c /etc/hiawatha/php-fcgi.conf
sudo php-fcgi -c /etc/hiawatha/php-fcgi.conf
Step 5 - Configure Hiawatha (Part 1)
sudo nano /etc/hiawatha/hiawatha.conf
Uncomment
ServerId
at GENERAL SETTINGS
.ServerId = www-data
Uncomment the following entries at
BINDING SETTINGS
.Binding {
Port = 80
# Interface = 127.0.0.1
MaxKeepAlive = 30
TimeForRequest = 3,20
}
Step 5a (Optional for security purpose) :
Add the following line at the
GENERAL SETTINGS
. ConnectionsTotal = 1000
ConnectionsPerIP = 30
SystemLogfile = /var/log/hiawatha/system.log
GarbageLogfile = /var/log/hiawatha/garbage.log
ExploitLogfile = /var/log/hiawatha/exploit.log
LogFormat = extended
ServerString = Apache
CGIwrapper = /usr/sbin/cgi-wrapper
Make changes for the following entries at
BANNING SETTINGS
. BanOnGarbage = 300
BanOnMaxPerIP = 300
BanOnMaxReqSize = 300
BanOnTimeout = 300
KickOnBan = yes
RebanDuringBan = yes
BanOnDeniedBody = 300
BanOnSQLi = 300
BanOnFlooding = 30/1:300
BanlistMask = deny 192.168.0.0/24, deny 127.0.0.1
ReconnectDelay = 3
Step 5b :
The entries at
COMMON GATEWAY INTERFACE (CGI) SETTINGS
should be looking like this.CGIhandler = /usr/bin/perl:pl
CGIhandler = /usr/bin/php5-cgi:php
CGIhandler = /usr/bin/python:py
CGIhandler = /usr/bin/ruby:rb
CGIhandler = /usr/bin/ssi-cgi:shtml
CGIextension = cgi
FastCGIserver {
FastCGIid = PHP5
ConnectTo = 127.0.0.1:2005
Extension = php, php5
SessionTimeout = 30
}
Step 5c :
Add the following line at
VIRTUAL HOSTS
.Include /etc/hiawatha/enable-sites/
*Make sure the make a directory
enable-sites
and disable-sites
under /etc/hiawatha
.sudo mkdir /etc/hiawatha/enable-sites
sudo mkdir /etc/hiawatha/disable-sites
Step 6 - Configure Hiawatha (Part 2)
If your domain is mysite.com, you are required to create a file namely
mysite.com
and place it under /etc/hiawatha/enable-sites/mysite.com
.VirtualHost {
Hostname = www.mysite.com, mysite.com
WebsiteRoot = /var/www/mysite
StartFile = index.php
AccessLogfile = /var/log/hiawatha/access.log
ErrorLogfile = /var/log/hiawatha/error.log
TimeForCGI = 15
# UseFastCGI = PHP5
# UseToolkit = banshee
#
DenyBody = ^.*%3Cscript.*%3C%2Fscript%3E.*$
DenyBody = ^.*%3CsCrIpT.*%3C%2FScRiPt%3E.*$
DenyBody = ^.*%3CScRiPt.*%3C%2FsCrIpT%3E.*$
DenyBody = ^.*%3CSCRIPT.*%3C%2FSCRIPT%3E.*$
DenyBody = ^.*%3CSCRIPT.*%3C%2Fscript%3E.*$
DenyBody = ^.*%3Cscript.*%3C%2FSCRIPT%3E.*$
#
# e.g.
Please make sure to re-do this step when the logwatch is updated or upgraded as it will overwrite the configure file.
Step 11 - Change the ownership of the log files
cd /var/log/hiawatha
sudo chown www-data:www-data access.log
sudo chown www-data:www-data error.log
sudo chown www-data:www-data exploit.log
* "php-fcgi.log
" and "system.log
" leave them untouched (root:root).
Step 11a - Change ownership of all directories and files at the /var/www/mysite
Put the web application files to /var/www/mysite and then change the ownership of all directories and files under /var/www/mysite to root:root.
cd /var/www/mysite
sudo chown -R root:root *
Step 12 - Start, Stop and Restart Hiawatha
sudo /etc/init.d/hiawatha start
sudo /etc/init.d/hiawatha stop
suod /etc/init.d/hiawatha restart
Step 13 - Performance tuning for MariaDB (Optional)
You can fine tune the MariaDB as per this link.
Step 14 - Performance tuning for Ubuntu (Optional)
You can fine tune the Ubuntu Server as per this link.
Step 15 - Secure your Ubuntu Server in a passive way (Optional)
Please refer to this link to secure your server in a passive way.
Step 16 - Setup a FTP server on Ubuntu Server (Optional)
This link shows you how to setup a vsFTPd server.
Step 17 - Bootless with Ksplice Uptrack on Ubuntu Server 11.04 (Optional)
This link shows you how to make your Ubuntu Server box bootless when kernel is updated or upgraded.
Remarks :
If you encounter "500 Internal Server Error
", you may consider to make the Apparmor to "Complain mode
".
sudo aa-complain hiawatha
After several days browsing the website, you may consider to turn the Apparmor to "Enforce mode
".
sudo aa-logprof
sudo aa-enforce hiawatha
It is because the captioned usr.sbin.hiawatha
may not 100% work for you.
That's all! See you.
No comments:
Post a Comment