So I was having problem with my previous server. Now that I’ve rebuilt everything I’ve realized that the problem I was having could have been easily fixed with my previous install. But, it doesn’t matter; it was time to upgrade from Ubuntu 12.04 to 14.04 anyway.
The goal of the server is simple: host two domains using Nginx. The problem I was having with the 12.04 install was that I could never get the second domain to serve up the right files; it would always serve up the first domain. This turned out to be a problem with the Nginx configuration. I thought this was the case at the time, but since the install wasn’t doing anything else anyway, I thought it was a good time to rebuild and start from scratch.
There are four articles of importance here:
- Initial server setup with Ubuntu 14.04
- How to install Nginx on Ubuntu 14.04 LTS
- How to set up Nginx server blocks (virtual hosts) on Ubuntu 14.04 LTS
- How to install and use Fail2Ban on Ubuntu 14.04
There is also a fifth article that may be useful to you: How to set up a host name with DigitalOcean.
The following is a quick summary of commands and configuration for the four articles listed above.
Initial server setup with Ubuntu 14.04
-
Login:
ssh root@[server_ip_address]
-
Change the root password:
passwd
-
Add a new user:
adduser [username]
-
Give root priveleges to new user:
visudo
-
BACK OUT IMMEDIATELY with CTRL-X and
export EDITOR=vim
;-) -
Give root privelges to new user:
visudo
[username] ALL=(ALL:ALL) ALL
-
Alter SSH configuration:
vim /etc/ssh/sshd_config
- Update port:
port [port_number]
- Refuse Root Login:
PermitRootLogin no
- Allow only certain users (careful!):
AllowUsers [username]
- Update port:
-
Restart SSH:
service ssh restart
-
Test SSH config from new terminal
ssh -p [port_number] [username]@[server_ip_address]
sudo vim
-
Exit the root terminal:
exit
How to install Nginx on Ubuntu 14.04 LTS
- Install Nginx:
sudo apt-get update
sudo apt-get install nginx
- Check the installation:
curl http://[server_ip_address]
orcurl http://[domain_uri]
How to set up Nginx server blocks (virtual hosts) on Ubuntu 14.04 LTS
-
Create a directory for each domain’s files:
sudo mkdir -p /var/www/[domain1]/html
sudo mkdir -p /var/www/[domain2]/html
-
Change ownership to each directory:
sudo chown -R $USER:$USER /var/www/[domain1]/html
sudo chown -R $USER:$USER /var/www/[domain2]/html
-
Make sure the permissions of the web roots are correct:
sudo chmod -R 755 /var/www
-
Create sample pages for each site:
vim /var/www/[domain1]/html/index.html
-> HTMLcp /var/www/[domain1]/html/index.html /var/www/[domain2]/html/index.html
vim /var/www/[domain2]/html/index.html
-> HTML
-
Create server blocks for each domain:
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/[domain]
sudo vim /etc/nginx/sites-available/[domain1]
server { listen 80; listen [::]:80; server_name [domain1] www.[domain1]; root /var/www/[domain1]/html; index index.html index.htm; location / { try_files $uri $uri/ =404; } }
sudo vim /etc/nginx/sites-available/[domain1] /etc/nginx/sites-available/[domain2]
- Adjust the second domain’s configuration to match the domain name and appropriate directory locations.
-
Enable the server blocks and restart Nginx
sudo ln -s /etc/nginx/sites-available/[domain1] /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/[domain2] /etc/nginx/sites-enabled/
sudo vim /etc/nginx/nginx.conf
and make the following change:server_names_hash_bucket_size: 64;
sudo service nginx restart
How to install and use Fail2Ban on Ubuntu 14.04
sudo apt-get update
sudo apt-get install fail2ban