Apr
26

Ubuntu Production Setup on VPS/Server for Drupal - Part 2

This is part 2 of the setup, where we concentrate on making the server production ready. Part 1 focus on the basic server setup to get Drupal running, you can find Part 1 here.

Performance

Opcode cache helps speed up your PHP considerably. We are using APC here, there are other alternatives such as XCache, eAccelerator, Zend Optimizer, etc.

Install APC Opcode

This will install APC 3.0.18-1

# aptitude install php-apc

Restart Apache for APC to take effect

# /etc/init.d/apache2 restart

Optimized MySQL

To get started quickly, get the tuning-primer.sh from MySQL Performance Tuning Primer Script

Get your MySQL root password ready

# chmod +x tuning-primer.sh
# ./tunning-primer.sh

It is recommended to let your site run for few days to gather enough query data to know how your DB performs. The tuning-primer.sh covers slow queries, worker threads, max connections, memory usage, key buffer, query cache, sort operations, join operations, open files limit, table cache, temp tables, table scans and table locking. You may want to refer Tuning your MySQL server for a better idea what each one does.

Throw in memcached to squeeze more out from the server.

To further boost webpage delivery performance, use Drupal built-in caching, boost module to serve static pages for anonymous user or deploy lightweight reverse proxy (Squid, nginx, Varnish cache)

Protect the Server

For SSH, it is a good practice to change the port from 22 and disable root login. Add a new user before disable root's SSH.

# adduser johndoe

Give yourself 'sudo' right to assume root user role for administration tasks.

# visudo
johndoe ALL=(ALL) ALL

Edit /etc/ssh/sshd_config

Protocol 2
Port 22
PermitRootLogin no
X11Forwarding no
/etc/init.d/ssh restart

Let's firewalled our server to make us feel more secure. =)
We are using Ubuntu Firewall for easy configuration

# aptitude install ufw

This will install iptables as well. Let's set the default to block all traffics by default, allow HTTP & SSH traffics and show the rules. Note that we enable ufw after allow SSH & HTTP, you don't want to block yourself out from the server. It takes port or service name.

# ufw default deny
# ufw allow http/tcp
# ufw allow 22/tcp
# ufw enable
# ufw status verbose
Status: loaded
Logging: on
Default: deny
New profiles: skip

To                         Action  From
--                         ------  ----
80/tcp                     ALLOW   Anywhere
22/tcp                     ALLOW   Anywhere

To remove a rule

# ufw delete allow 80/tcp

To deny

# ufw deny 8080

To disable ufw

# ufw disable

You can still use other firewall tools or iptables command as usual. Take a look of the generated firewall rules

# iptables -L

You may want to enable logging for the firewall

# ufw logging on

Apache

Make it harder for script kiddies, edit /etc/apache2/conf.d/security, change to the following

ServerTokens Prod
ServerSignature Off
TraceEnable Off

MySQL

Restrict MySQL to localhost only, edit /etc/mysql/my.cnf

bind-address            = 127.0.0.1

Give it Attention

Monitoring the services to ensure it is running fine, alert you when there is a sign of problem and help you fix them if the rule triggered. We are using Monit for this purpose. Alternatively you can use Nagios, Zabbix, or website online checker.

# aptitude install monit
# mkdir /etc/monit.d

Edit the configuration file, /etc/monit/monitrc. We changed it to check every minute, output to syslog, set to localhost smtp, keep the alerts under /var/monit up to 100 alerts, the user to receive all the alerts and include all configuration files found under /etc/monit.d we created in previous steps.

set daemon  60
set logfile syslog facility log_daemon
set mailserver localhost
set eventqueue basedir /var/monit slots 100
set alert johndoe@foo.bar
include /etc/monit.d/*

Head over to Configuration Examples and copy paste the pieces into separate file under /etc/monit.d for easy maintenance.

Backup, backup, backup

We use Backup and Migrate to dump the database to drupal files folder on schedule, which we backup together with the rest of web folder /var/www.

There are a lot of backup solutions depending on your need. You could use

These wrapped up our second part of server setup articles. A lot of areas are only briefly mentioned as they deserved a full blown article of their own. Share you experience with us using the comment form below.