Basic DevOps for deploying an application in Ubuntu VPS

As a senior dev guy in the office, I am always asked to help in setting up production environment ready for VPS based on Ubuntu or any other Linux distributions. Today I will cover some basic knowledge that help you deploying your application in any Linux VPS even you are not a DevOps Engineer in your current company. This knowledge also helps you to move forward your dev career in a new shape. In this blog post I used Ubuntu 14.04 LTS as my host for showing and describing all the example commands.

Access to the VPS

Open the terminal and type the following command. Either you will have the password or you will be authenticated by SSH.

ssh root@xxx.xxx.xxx.xxx

The best practice is securing the server access with Public Key Authentication. Last part of this blog post I will explain about setting up the public key authentication.

Okay, you are now in the server. Before jumping on anything I would recommend you to run following command keep updated your server (I am writing this blog post assuming you have very less idea about Linux).

#For debian/Ubuntu Distro
sudo apt-get update; sudo apt-get upgrade
#For Red Hat/Fedora/CentOS Distro
yum -y update

User creation

This is always suggested to create an application deployer user after first time login to the VPS. Following command describes app as a user and the home directory path: /home/app

useradd -s /bin/bash -m -d /home/app -c "app user" app

On the above line I mentioned login shell by -s, the home directory of new user by -d, and comment for new account by -c with useradd command. Putting comment and home directory path is optional event you don’t put it will create by itself.

For creating a password for the app user, type following command and system will ask to type the password twice:

passwd app

Now, you need to grant permission to the app user in order to execute root level commands. Type:

visudo

New windows will open by vim or nano, whatever is the default text editor. And, add following line

app ALL=(ALL) ALL

User creation for deployment is done. Now login as app user to that VPS. Type:

su app

or you can quit from terminal and can login as app user via SSH

ssh app@xxx.xxx.xxx.xxx

Some Important tools installation

#For Debian/Ubuntu distro
sudo apt-get install git git-core build-essential
#Red Hat/CentOS
sudo yum -y group install git git-core "Development Tools"

Before doing anything I will recommend you update the distro. Run following commands

#For Debian/Ubuntu distro
sudo apt-get update
sudo apt-get upgrade
#Red Hat/CentOS
sudo yum upgrade

Definitely, you need a web server to handle the request and pass to your application. I like nginx over apache 2 web server. So, I will cover some basics of Nginx block. Btw, you need to install the nginx server first.

#For Debian/Ubuntu distro
sudo apt-get install nginx
#Red Hat/CentOS
sudo yum install nginx

Nginx block

Open a new file in the directory: /etc/nginx/sites-available with root privileges and a text editor.

sudo nano /etc/nginx/sites-available/example.com

Simple HTML app

server {
  server_name example.com;
  listen 80;
 
  access_log /var/log/nginx/example.com.access.log;
  error_log /var/log/nginx/example.com.error.log;
 
  root /home/app/example.com;
  index index.html index.htm;
 
  location / {
    try_files $uri /index.html;    
  }
}

Node, Ruby or PHP app with build-in server

server {
    listen 80;
    server_name example.com;
 
    location / {
        proxy_pass http://localhost:5000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

You need to create a simulink of the block to the sites-enabled directory.

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com

Restart the Nginx

sudo /etc/init.d/nginx restart

You might need to update the iptables firewall in order to accept the request on 80 port.
Just type the following command:

sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT

Almost everything is done. Your VPS is ready to use. I will cover some extra. You can ignore this part.

https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-14-04

Authentication by public key

Generate your SSH key pair. How to: https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/

Login as an app user (or the user you created before) to remove server again. It will take you to the home directory. Now run the following commands.

mkdir .ssh
chmod 700 .ssh

Open a new file: authorized_keys with a text editor and insert your public key (you already generated)

nano .ssh/authorized_keys  #you can use any editor such as VIM

Now, again restrict the permissions of the authorized_keys for security purpose. Run the following command.

chmod 600 .ssh/authorized_keys

I would also recommend you to disable the root login. Just make PermitRootLogin no at /etc/ssh/sshd_config and then restart SSH.

Some other tools

MySQL (RDBMS) Database Installation

#For Debian/Ubuntu distro
sudo apt-get install mysql-server
sudo mysql_secure_installation
sudo mysql_install_db
 
#Red Hat/CentOS
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum update
sudo yum install mysql-server
sudo systemctl start mysqld
sudo mysql_secure_installation

MongoDB (NoSQL) Database Installation

#For Debian/Ubuntu distro
sudo apt-get install -y mongodb
 
#Red Hat/CentOS
sudo yum install -y mongodb-org

You are now good to go with your VPS. Happy hacking 🙂

 

Eftakhairul Islam

Hi, I'm Eftakhairul Islam, a passionate Software Engineer, Hacker and Open Source Enthusiast. I enjoy writing about technical things, work in a couple of startup as a technical advisor and in my spare time, I contribute a lot of open source projects.

 

One thought on “Basic DevOps for deploying an application in Ubuntu VPS

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Read previous post:
Upgrade to PHP 7

PHP 7 is released after 10 years of PHP 5 released.  It's a major release and it has a lot...

Close