Setting an optimized $5 droplet on Digital Ocean - Ubuntu Nginx Php5-fpm

Written by Erika Heidi on Wednesday September 25, 2013 - Permalink - Lang: eng

digital-ocean-logo-4x3I recently migrated all my projects from an old plain dedicated server to DigitalOcean droplets. I'm very happy with the result, my sites are faster and much more organised now, in separated servers. Awesome specially considering the price! One of the best things about VPSs is that you can separate your applications through multiple inexpensive servers. I know Amazon is there for some years, but I could never give it a try because its just SO COMPLICATED to understand anything on that website ! It's just horrible. So now we have Digital Ocean, which in my humble opinion is the best option for professional hosting nowadays. With a very clean and straightforward interface, making it super easy to create new servers on the go - and they even have a Vagrant plugin. Profit! Anyways, this is a step by step guide to set up an optimized DO basic instance ($5 montly) for running PHP applications - I used this setup for my blog here and some other small applications, having an excellent result on performance. Basically, if you have more traffic, you might need to upgrade to a better one - but since you can do it very easily through DO panel, it's a good thing to start with the basic instance and test the performance to upgrade only if necessary. For following this guide, I'm assuming you already have the droplet created with Ubuntu, and you are logged in as root (and you are comfortable with linux system administration).

1. The swap trick

This is a well-known "trick" for optimizing Amazon EC2 micro instances, and works just as great for DigitalOcean. I used this to run Jira and Stash (both Java applications which consume lots of memory) on a basic DigitalOcean and it worked! Although 512MB was not enough for these applications to run *fast*, they at least could be kept running without crashing all the time as before.

root@sites:~# dd if=/dev/zero of=/swapfile bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 1.5293 s, 702 MB/s
root@sites:~# mkswap /swapfile 
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=0d2346ad-c43d-459b-87e7-d1f471227aed
root@sites:~# swapon /swapfile

Now add the swap to your fstab by editing /etc/fstab and adding this line:

/swapfile  swap      swap    defaults         0 0

Source: http://www.rownet.co.uk/adding-swap-space-to-an-amazon-ec2-micro-instance/ .

 2. Nginx FTW

I've been using Apache for years, but I decided to give Nginx a try after hearing so many great things about it. And it really makes an impressive difference regarding to performance! The installation and configuration is also very simple.

# apt-get update
# apt-get install nginx

Now lets set up our PHP environment and afterwards we come back to configure the nginx default site.

3. PHP5-FPM

# apt-get install php5-fpm Now we have some changes to make in php.ini. Edit /etc/php5/fpm/php.ini and change the option cgi.fix_pathinfo to 0 instead of 1.

cgi.fix_pathinfo=0

Its also a good opportunity to set other options on php.ini to reflect your needs, such as date.timezone. Another change must be made, edit the file /etc/php5/fpm/pool.d/www.conf and change the option listen = 127.0.0.1:9000 to listen = /var/run/php5-fpm.sock :

; The address on which to accept FastCGI requests.
; Valid syntaxes are:
;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific address on
;                            a specific port;
;   'port'                 - to listen on a TCP socket to all addresses on a
;                            specific port;
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = /var/run/php5-fpm.sock

Restart php5-fpm:

root@sites:~# service php5-fpm restart
 * Restarting PHP5 FastCGI Process Manager php5-fpm

4. Configure Nginx with PHP

Now let's configure the nginx default site, by editing the file /etc/nginx/sites-available/default . This is how it should look like (change the server_name to your host[s]).

server {
        listen   80;

        root /usr/share/nginx/www;
        index index.html index.htm index.php;

        server_name yourdomain.com www.yourdomain.com;

        location / {
                #try to find the requested uri or redirect to index.php with request args
                try_files $uri $uri/ /index.php?$args;
        }

        error_page 404 /404.html;

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /usr/share/nginx/www;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }

}

A note about url rewriting: nowadays all CMSs and Frameworks use url rewriting to a main php script (index.php mainly), that's what is set at line 10 - try $uri, $uri/ and if the path doesn't exist, redirect to index.php passing the request args. Restart nginx:

root@sites:~# service nginx restart
Restarting nginx: nginx.

Now create some php script inside /usr/share/nginx/www and try to access it through your browser, pointing to yourdomain.com/yourscript.php . If something goes wrong, verify the logs. The default nginx error log is located at /var/log/nginx/error.log , and the php5-fpm log is at /var/log/php5-fpm.log .

5. Mysql and other stuff

Now that your server is up and running, you can install other stuff like database and php packages.

# apt-get install mysql-server mysql-client
# apt-get install php5-mysql php5-gd php5-xdebug php-apc

And I guess this is it! Now is up to you. I didn't use phpmyadmin on my servers, started using MySQL Workbench and I really like it for everything regarding MySQL server administration. Just setup a connection using SSH (so you don't need to change rules on the server to accept remote connections) and your mysql root credentials, and you will be able to create databases and users to run your applications.    

comments powered by Disqus