Tag Archives: vps

Deploying static websites to VPS using git

git is a distributed open source version control system. If you’re not already familiar with git then read some free tutorials listed here and come back after you have some basic understanding to follow through the tutorial. Whether it’s a static website or a fun project, it’s usually a good idea to use a version control system such as git. Additionally, git can also help you in deployment and make your development workflow much easier and simpler.

git deployment

Step #1. Server Setup

If you’ve not already selected a VPS then get one there on Digital Ocean or Linode. Make sure you select Ubuntu (preferably : 14.04 LTS) for the server OS. Otherwise, you may need to adjust few commands depending on the Linux distribution you’re using.

Step #2. Configure git on the server

SSH into the remote server and configure git for deployment.

ssh example_user@IP_ADDRESS
mkdir example.git
cd example.git
git init --bare

Next, create a git hook – a shell script that will be executed on git push (to update code on server).

nano ~/example.git/hooks/post-receive

Here is a sample post-receive script you can use : (use Ctrl+X to save and exit)

And make it executable.

chmod +x ~/example.git/hooks/post-receive

Step #3. Install and configure git on dev machine

First, install git, move into your project directory and commit it under git.

sudo apt-get -y install git-core
cd example.com
git init
git add .
git commit -am "MVP is ready for the wild"

Add remote repository for deployment server.

git remote add production ssh://example_user@IP_ADDRESS/~/example.git

And now, you’re ready to deploy. Just type (to push recent changes to live server) :

git push production master

Step #4. Configure nginx

ssh into the server and create a nginx config for the domain (example.com).

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

Here is a sample config you could use :

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

Now, update the DNS settings at the registrar and create two records – one for naked domain (e.g example.com) and other for www-version of the website (e.g www.example.com) – both pointing to the server IP. And, wait for the DNS to be propagated (few minutes).

digital-ocean vps

Digital Ocean Review : Cheap and Best SSD VPS Hosting!

Digital Ocean is a new VPS company which has gained a lot of popularity in a very small time span (recently, it crossed the 400k+ servers).

Current Promo Code : DODROPLET for $10 credit (worth two months of free hosting).

digital-ocean vps

Well, there are lots of VPS providers, what makes Digital Ocean so awesome ? I’ve been using Digital Ocean for around 5 months (for my web applications/Ruby on Rails Apps, e.g one is PC Builder) and here are few reasons – why Digital Ocean is one of the best VPS provider.

Why Digital Ocean is Awesome ?

  1. Very affordable price (plan starts at $5/month)
  2. Offers more than its competitors – 512MB RAM, 20GB SSD, 1TB Bandwidth, Automated Backups/Snapshots – all at $5/mo
  3. Easy to launch/manage servers with its nice Dashboard
  4. Great customer support (the average response time for a support ticket is less than 15 minutes – vow!!)
  5. Advanced Features, easy to use APIs (for managing servers, you don’t have to login to their control panel)
  6. Even if you’re a noob, you can easily learn all stuffs, they have awesome tutorials available in DO Community
  7. it’s a reliable company, backed by awesome team in NYC, they are not going anywhere.
  8. Login to your servers via ssh and you Have full control over your VPS
  9. Zero Downtime -If you use Digital Ocean, your servers are up no matter what. (In past 5 months, I never faced even a single downtime)
  10. Create backups easily using snapshots or automated backup feature. (creating manual snapshot is free but automated backup costs a little). You can also spin up new servers (in minutes) using your snapshots.

Hosting Plans available at Digital Ocean

If you’re starting out, the starting plan @ $5/month would work fine. You get 512MB RAM, 20GB SSD, 1TB Bandwidth. They also bills on Hourly basis, you can use either monthly or hourly plan – depending on your need. Another plan is $10/mo – for which you get 1GB RAM, 30GB SSD and 2TB data transfer, and so on.

checkout their website for larger plans

So, what are you waiting for ? 

Whether you want to Deploy a Web Applications (Ruby On Rails, Django, node.js etc) or a Blog or simple website – Digital Ocean would work great. You can get started now – (no credit card is required! try test mode)

Get a VPS at Digital Ocean

Current Promo Code : DODROPLET for $10 credit (worth two months of free hosting).

Update! Now, they’ve updated the dashboard and added few more features like Resource usage graph, new data center location for servers (also in asia region) and so. Added a promo code. Also added a video.

speed up wordpress

How to setup wordpress on Ubuntu 12.04 Server (VPS)

It’s a step by step guide for setting up wordpress on a vanila server (e.g Digital Ocean, don’t know about it? then read Digital Ocean VPS Review) running Ubuntu 12.04 LTS (latest LTS release, recommended for server). With, most of the VPS providers, you can deploy GNU/Linux distribution from their control panel itself, after that, everything will done over the ssh connection.

setting up wordpress

Step 1. Setting up the server

ssh into the server and create a user (as it’s not a good idea to use root account).

ssh root@vps_ip_address
apt-get -y update
adduser user_name --ingroup sudo

Now, logout from the current session and login again with the user newly created above.

exit
ssh user_name@vps_ip_address

Install the web server – Apache (you could also choose nginx)

sudo apt-get install apache2

Now, create a virtual host –
In this tutorial I’ll be using nano editor (a light-weight command line editor) So, if it’s not already installed – then install it using sudo apt-get install nano.

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

and add this contents (change few things according to your need such as domain name, user_name)

If you selected nginx instead of Apache, here is a sample configuration for nginx/wordpress blog.

Then enable the virtual host,

sudo a2ensite example.com
sudo service apache2 reload

Step 2. Installing Dependencies (PHP, MySQL)

During installation process, you will be asked to provide password for MySQL root account.

sudo apt-get install mysql-server
sudo apt-get install php5 libapache2-mod-php5 php5-mysql

For image manipulation, wordpress uses php-gd library, so you need to install that as well,

sudo apt-get install php5-gd

You may have to enable rewrite mod for apache2, to do that simply type :

sudo a2enmod rewrite
sudo service apache2 restart

Step 3. Creating Database

Login with MySQL root account and create the required users and databases (you need to remember these details for wp-config.php)

mysql -u root -p
create database DBNAME;
grant all on DBNAME.* to DB_USER@localhost identified by 'password';
exit

Step 4. Installing WordPress

Download the latest version of wordpress and extract its content.

cd ~
wget http://wordpress.org/latest.tar.gz
tar -xzvf latest.tar.gz

For simplicity, you can rename the wordpress directory to something like – your domain name e.g example.com.

mv wordpress example.com

Now, you can manually edit the wp-config.php file or go through the automated installation process (just visit the URL, I assume you have setup DNS correctly, or use IP address instead).

cd example.com
nano wp-config-sample.php

Then add the database details and random strings for session tokens etc and save the file (Ctrl+x, y). Now, rename the file.

mv wp-config-sample.php wp-config.php

That’s set, you’ve successfully installed wordpress, visit the URL/IP_ADDRESS and create the admin user.

Moving your Blog ?

If you’re moving your old wordpress blog to a new server, then you also need to copy wp-contents directory (from the old one to new server, it’s the directory – that holds your images, themes and plugins). Besides that you will also have to export/import database contents. Use your server control panel or a tool like PHPMyAdmin (which is usually installed on all shared hosting providers) to export your database contents in *.sql format.

I assume you already have an exported database file (*.sql), So you can easily import it, on new server using the single command –

mysql -h localhost -u DB_USER -p DB_NAME < db_backup.sql

Note : If you have any problem during installation, then leave a comment here, we’ll try to figure it out!

deploying-rails-app to vps

How to Deploy Rails Application to VPS

It seems like your cool rails application is ready to go wild, That’s great! sooner the better. Before you made up your mind for deploying rails application to a VPS – you should know that it’s not a very easy (and only) process. You could have deployed using some other way e.g Heroku, probably much faster.

Manually deploying your rails application to a VPS can be very frustrating for beginners but if you’re willing to learn all those stuffs (GNU/Linux, terminal commands, server administration stuffs and more) or may be you want full control over the things – then probably you made the best choice.

deploying-rails-app to vps

“step by step” guide for deploying a Rails application to VPS

1. get a VPS and install Ubuntu 12.04 LTS

If you don’t already own a VPS then buy one. I recommend Linode (I’ve been using it for my blog as well as for other rails applications, I absolutely love their service). They’ve plans starting at $20/month for 512MB 1024MB 2048 RAM. (which should be sufficient for testing and launch, you can upgrade easily so start with small one, unless you’re sure how much memory you need).

Checkout Linode VPS plans (starting @ $20/month)

Another Great Option – Digital Ocean (starting @$5/mo, 20 GB SSD, 1TB Data, 512 MB RAM, free backups and more!) You can use promo code DEPLOY2DO to get $10 credit (worth two months of free hosting), so you can give it a try without any risk.

I’ve written more about it recently – Digital Ocean Hosting Review.

After getting a VPS ready (usually within few minutes), choose an operating system – I recommend Ubuntu (unless you’ve some specific choice, due to some previous experience with a GNU/Linux distribution such as ArchLinux, Debian etc) – select latest LTS release, ie Ubuntu 12.04 LTS (precise pangolin).

Update! Now, Ubuntu 14.04 LTS is out and available. So, you should choose that. You may need to change few commands below. Although, if you go with 12.04, you can always upgrade.

2. Setting up the Server

When, the vps is ready (within few minutes, after you deploy and boot the server), SSH into the server and use your root account to login (you would get the root password from vps provider’s panel, while deploying the OS, in the above step #1).

 ssh root@vps_ip_address

Now, first update the repository cache and the packages.

apt-get -y update
apt-get -y upgrade

Creating a user

Using root account is not a good idea (of course for security reasons, that’s why there is something called sudo, that would allow you to execute commands with root privilages). So lets create a user, e.g mrhuman, add him to sudo group (so that he can execute commands with root power).

adduser mrhuman --ingroup sudo

(it will ask you to enter some details, as well as the password for the user)

Using password authentication is not secure and it’s also not very efficient. Logout from the current session or switch to the newly created user with su user_name command. In next step, we will be setting up the ssh-authentication.

setup ssh authentication

If you’ve not already generated public/private keys (most likely you already have a pair of public/private keys, e.g you might have generated it for github or bitbucket), then generate one and upload your public key to the vps server.

Type the command(on your local computer)

ssh-keygen
scp ~/.ssh/id_rsa.pub mrhuman@vps_ip_address:

Login to the remote server with newly created user(mrhuman, in this case) and type

mkdir .ssh
mv id_rsa.pub .ssh/authorized_keys

3. Install nginx server

In this guide I assume you’re using nginx server, Apache is another great option. Before installing nginx, first install the utility – python-software-properties, for installing packages via PPA, sometimes the packages available in repository are very outdated.

sudo apt-get -y install python-software-properties

Then install nginx from the PPA.

apt-add-repository -y ppa:nginx/stable
apt-get -y update
apt-get -y install nginx

After installing nginx you can type

sudo service nginx start

to start the nginx and visit the IP address your server, you should see the default page there.

4. Install ruby and other dependencies/gems

For managing ruby versions you should choose RVM or rbenv. In this guide I’m going with rbenv.

Before installing rbenv, lets install some common dependencies such as curl, git version control system, javascript runtime such as nodejs etc.

Installing nodejs

sudo apt-add-repository -y ppa:chris-lea/node.js
sudo apt-get -y update
sudo apt-get -y install nodejs

Installing curl and git

sudo apt-get -y install curl git-core

Now, install rbenv using an installer

curl https://raw.github.com/fesplugas/rbenv-installer/master/bin/rbenv-installer | bash

Add rbenv to your path variable

After installing rbenv, Just add the following lines to the top of your ~/.bashrc file.

export RBENV_ROOT=\"\${HOME}/.rbenv\"
if [ -d \"\${RBENV_ROOT}\" ]; then
  export PATH=\"\${RBENV_ROOT}/bin:\${PATH}\"
  eval \"\$(rbenv init -)\"
fi

Now, reload the shell by tying the command –

source ~/.bashrc

Install dependencies with rbenv-installer script

rbenv bootstrap-ubuntu-12-04

Installing Ruby

rbenv install 1.9.3-p327
rbenv rehash
rbenv global 1.9.3-p327

Installing Bundler & Rake

gem install bundler --no-ri --no-rdoc
gem install rake --no-ri --no-rdoc
rbenv rehash

Install MySQL and Create database/user

I assume you’re using MySQL in production (or whatver like Postgresql, MongoDB etc, you need to install the required packages/dependenices and create database/user).

Installing MySQL

sudo apt-get -y install mysql-server libmysql++-dev

Now, create database and users (enter the root password, as entred in above step)

mysql -u root -p
create database YOUR_DB_NAME;
grant all on YOUR_DB_NAME.* to DB_USER@localhost identified by 'your_password_here';
exit

5. Setting up Capistrano for Deployment

Now, it’s time to set up capistrano for automating the deployment process. You would also need to have the source code hosted on a private repository (unless you want the source code to be available public) such as on github or bitbucket and add the remote repository to list of identified hosts (to avoid some error during deployment). Simply, ssh into the remote host (remote repository) e.g ssh github.com or ssh bitbucket.org(if you’re using bitbucket) from the remote server.

add following gems into your Gemfile.

gem 'mysql2'
gem 'unicorn'
gem 'capistrano'

and install those gems, by running bundle install.

Now, type the command(of course, in root of your application directory)

capify .

It will create few files that you need to change. First update your Capfile (for the assets section), it should look like this:

Then, update database.yml file (!warning, you shouldn’t put this kind of file in git repository).

Next, add unicorn.rb and unicorn_init.sh (make it executable by running chmod +x unicorn_init.sh) file to config directory.

Next unicorn_init.sh,

Then update config/deploy.rb file.

Don’t forget to change the variable names in above files (e.g replace YOUR_APP_NAME by your application name, git repository URL by your_repository address and more!)

6. Deploy!

That’s all, now it’s time to deploy!

I assume you’re already using git, so commit the latest changes and push it to master branch.

git add .
git commit -am "add deployment configs"
git push master

First setup the deployment configurations using the command –

cap deploy:setup

Then deploy it,

cap deploy

If things look OK (in fact, it may take a while and you may get some errors, just read the messages and act accordingly, rather than freaking out) then open the browser and visit the IP address (or domain, now you can set up DNS for the VPS)

Run the database migrations and start.

cap deploy:migrate
cap deploy:start

Now, Your Rails app should be running fine, any problem? Drop a comment here, we’ll look into that.

Reference(s) and Recommended Link(s)