Skip to content

Instantly share code, notes, and snippets.

@darrenterhune
Last active April 14, 2016 00:43
Show Gist options
  • Save darrenterhune/5020846 to your computer and use it in GitHub Desktop.
Save darrenterhune/5020846 to your computer and use it in GitHub Desktop.
Digital Ocean [Virtual Hosts, MySQL] Setup

Setup Virtual Hosts

Execute the following commands after ssh into vps:

sudo mkdir -p /var/www/example.com
sudo chown -R $USER:$USER /var/www/example.com
cd /var/www/
sudo chown www-data:www-data * -R 
sudo usermod -a -G www-data username-logged-in-as
cd
sudo nano /var/www/example.com/index.html

Add a basic html page for now:

<html>
<head>
<title>www.example.com</title>
</head>
<body>
<h1>Success: You Have Set Up a Virtual Host</h1>
</body>
</html>

Setup sites-available by copying the default setup:

sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/example.conf
sudo nano /etc/apache2/sites-available/example.conf

Next setup the domain virtual hosts file:

ServerAdmin webmaster@localhost
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com
Redirect 301 http://www.example.com http://example.com

Make sure we have AllowOverride set to All so .htaccess files work properly and make sure Indexes is removed from Options to prevent directory listing:

<Directory /var/www>
	Options FollowSymLinks MultiViews
	AllowOverride All
</Directory>

Let's also prevent hotlinking of files:

SetEnvIf Referer example\.com localreferer
<FilesMatch \.(jpg|png|gif|css|js|pdf|doc|xls|txt)$>
	Order deny,allow
	Deny from all
	Allow from env=localreferer
</FilesMatch>

Activate and restart:

sudo a2ensite example.conf
sudo service apache2 restart

MySQL

Add a new database and user for that database:

CREATE DATABASE example;
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON example . * TO 'newuser'@'localhost';
FLUSH PRIVILEGES;

Dump a database and import a database:

mysqldump -u [username] -p [database name] > [database name].sql
mysql -u [username] -p newdatabase < [database name].sql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment