Digital Ocean Apache

Follow this massive guide to run a world-class server on the cheap. If you've never even set up a server before, don't fret as it takes about 30 mins to follow this crash course. I recommend to follow the steps down to the popular modules and skim the rest if you're curious. Google if you get stuck. Good luck!

Quick Reference

Restart Apache service apache2 restart
Digital Ocean Reboot reboot
Digital Ocean MX records See Email
Digital Ocean Install Curl See Enable Popular Apache2 Modules
Digital Ocean Remote MySQL Access See Install PhpMyAdmin
Digital Ocean Install PhpMyAdmin See Install PhpMyAdmin
Digital Ocean vhosts See Running Multiple Websites

Initial Setup

  • Register a domain and set its nameservers to ns1.digitalocean.com and ns2.digitalocean.com.
  • Create a droplet. The 1 GB option will do nicely.
  • Go to the DNS manager in Digital Ocean.
  • Click Add Domain, type your-domain.com in and select your droplet from the dropdown.
  • Add a cname record, with * and @ for the name and hostname, respectively.

Remote Access

Mac users can enter the following command into Terminal to login to their Droplets. Once logged in, the commands you enter will be executed on the Droplet:
  • ssh root@your-domain.com
  • Your password is in an email from DigitalOcean.
  • root is the username if you need to use it again.
Windows users can download Putty and do the same thing. If you're having trouble with this step, it is most likely your ISP needs to be told to unblock port 22. Run these two commands to make sure the server has the most up to date information.
  • apt-get update
  • apt-get upgrade

Specify Your Hostname

  • nano /etc/hostname
  • Replace the contents of the file with your-domain.com

Install Apache

Apache plays the defining role of a server; to reply to requests coming from the web.
  • apt-get install apache2
Now you can navigate to your-domain.com and see the default website. To upload your actual site, use a good SFTP client like Coda (Mac) or FileZilla (Windows) to login using the credentials from step two. The default folder is /var/www.

Install PHP & MySQL

PHP adds the ability to create web pages programmatically, and MySQL is used to store larger data sets where a file wouldn't necessarily be the best option.
  • apt-get install php5 libapache2-mod-php5
  • apt-get install mysql-server

Install PhpMyAdmin

After this is installed you will be able to administrate your MySQL database by navigating to your-domain.com/phpmyadmin.
  • apt-get install phpmyadmin
  • Open /etc/apache2/apache2.conf and add the following line:
    Include /etc/phpmyadmin/apache.conf
  • service apache2 restart

Enable Popular Apache2 Modules

Rewrite lets you make vanity URLS and countless websites depends heavily on it. Headers lets PHP modify webpage headers, and CURL lets you pull in data from external websites.
  • a2enmod rewrite
  • a2enmod headers
  • apt-get install curl libcurl3 libcurl3-dev php5-curl
  • service apache2 restart

Email

This section now has its own guide!

Enable SSL

  • Upload the SSL certificates to /etc/apache2/ssl/
  • Add the following to /etc/apache2/sites-available/default
    ServerName  example.com/
    Redirect / https://example.com/
    
  • Add the following to /etc/apache2/sites-available/default-ssl
     
    SSLEngine on
    SSLCertificateFile /etc/apache2/ssl/example/example.crt
    SSLCertificateKeyFile /etc/apache2/ssl/example/server.key
    SSLCertificateChainFile /etc/apache2/ssl/example/AddTrustExternalCARoot.crt
    SSLCertificateChainFile /etc/apache2/ssl/example/PositiveSSLCA2.crt
    
  • service apache2 restart

Running Multiple Websites

To run multiple websites, open the vhosts file in /etc/apache2/sites-available/000-default.conf. Assuming you pointed the domain example.com at your Digital Ocean account, you can add the following code to serve files in /var/www/example to example.com and www.example.com
<VirtualHost *:80>
	DocumentRoot /var/www/example
	ServerName example.com
	ServerAlias www.example.com
</VirtualHost>
If you would like each user's code to run as the user and not a privileged user like www-data, you'll need to install MPM-ITK.
  • apt-get install apache2-mpm-itk
Then create a user for each website. Each user will not be able to access the other user's files and thus your server is more secure. The Adduser command will create a new home folder, sftp user and password. Use mkdir to create a www folder in the home directory.
  • adduser user_name
  • mkdir /home/user_name/www
  • chown -R user_name:user_name /home/user_name/www
  • Alter the vhost configuration's DocumentRoot and add the AssignUserId line as well.
    DocumentRoot /var/user_name/www
    AssignUserId user_name user_name