1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

VPS Guide - From start to Finish

Discussion in 'Web Hosting' started by Xyz01, Nov 1, 2011.

  1. Xyz01

    Xyz01 Regular Member Premium Member

    Joined:
    Aug 8, 2011
    Messages:
    300
    Likes Received:
    126
    Hi All,

    This idea has been bouncing around my head for a while, and I had created a guide before but have finally decided to make a full start to finish guide for all those who are new to how VPSs work.

    If you are currently paying through the nose for multiple shared hosting servers, or want to understand more about VPS Servers, than this guide is for you.

    Moving away from Shared Hosting is always a good decision. Here is how to setup a VPS and set everything up to get your website online. I personally moved away from shared hosting (Dreamhost) in 2007 to VPS's, and have never looked back.

    So would should you move away from Shared Hosting?
    You should move away from Shared Hosting when your site has overcome the initial stage and you require too many resources for your server, or if your hosting provider isn't very good - many shared hosts "oversell" which means that you have thousands of users per server each having their website.

    This is bad for your site in terms of SEO (as Google usually considers shared IP Addresses to be a*bad thing) and in terms of speed - with many users on the same machine your performance usually takes a nose dive.

    If your blog is getting a large influx of visitors, or your provider is complaining that your script is taking up too many resources, this is also a key that you should move to a VPS.

    But how much will this cost me?
    The VPS price range is quite reasonable, with providers such as Linode, Slicehost, Rackspace etc all have good entry level plans to get you started.

    Alternatively you can also get a "cloud" server which is pretty much exactly the same as a VPS from a company such as Amazon EC2, or from Rackspace (I love Rackspace!).

    You can expect to pay between $10-20 a month for a VPS. Burstnet currently has a deal for a $5 a month VPS, which may be a good alternative for you as well. Beware low cost VPSs as these usually perform horribly and not worth the cost.

    So how do I get started?
    Usually you just need to sign up for a VPS, and they usually email you your SSH details. SSH is a method that you use to connect to the server securely and set things up. You use a program called PuTTY, which is a program that lets you remotely connect to Linux servers from Windows. Using a Mac or a Linux computer - you can just open up terminal and type in "ssh myserver.com" .

    For this example, I am using a Rackspace 512MB "Cloud Server" which is just a current fad marketing term for a VPS. You can however use any host you like. This is $21 a month. I'm also going to be using Ubuntu 10.04 which is the current Ubuntu LTS which means it will be supported until April 2015.

    What will we be installing?
    This will get you the following:

    nginx 1.0.x - The webserver.
    Varnish 3.x - The reverse proxy.
    MySQL 5.x - The database server.
    PHP 5.3.x
    Memcached
    APC

    NGINX - NGINX is a very powerful webserver. You may have heard of Apache2 before, but I've found that NGINX handles load better than Apache2. Powering over 40 million domains, powering companies such as Facebook, Zappos, Groupon, LivingSocial, Hulu, TechCrunch, Dropbox and WordPress, it's quite a nice piece of software.

    MySQL - MySQL is a database server, powering many powerful websites. This runs as a database for pretty much all opensource PHP code and is quite a nice database that scales fairly well.

    PHP - PHP is the program that runs Wordpress, Drupal, Joomla etc.

    APC - APC which is a op-code cache that drastically improves PHP performance. This is neccessary as it really does speed up frequently ran PHP code!

    Memcached - Memcached is a Key-value data cache which allows many PHP CMS's/CMF's to cache it's commonly accessed data to lower database load - meaning that your Wordpress/Drupal/Joomla site will now FLY. This is one of the biggest improvements you can do to your site if your site is database-driven.

    This will then be used like the following:
    Varnish (port80) --> nginx (port81) -> website

    Alternatively you can also setup Cloudflare instead of Varnish, however I usually use both on my sites (depending on the traffic).

    Step 0: Getting a good VPS
    This is the step where most people stumble. Sure there is a ton of VPS providers out there, however most of them are resellers. You can usually find a good deal on here (BHW), or find coupons and special offers from other forums who focus on Web Hosting.

    I personally use Rackspace(affiliate link), and have had no issues with them for months. I run multiple cloud servers off them, and have no issues with downtime (except scheduled maintenance which usually lasts 30 minutes, however this is rare).

    You can sign up here , which I would appreciate if you do as this earns me a couple of dollars.

    Now, when this is setup, you'll be able to login to the Management Console here, then Hosting -> Cloud Servers, then Add Server.
    [​IMG]

    A box with show, you will now need to select which Operating System/Distro you want to run. I highly recommend choosing Ubuntu 10.04, as this is very stable and will be supported with updates until April 2015.
    [​IMG]

    Now you need to select the size of the server, as well as the nickname for it. I run mine off 512MB with costs $22 a month.
    [​IMG]

    This will then go and create the server for you, and give you the root password for the server.
    [​IMG]
    This will take one or two minutes for the server to become Active.

    The next screen shows you the overview of the server, which supplies you with the IP Address and other handy information. Most of this can be ignored, but keep a note of the IP Address.
    [​IMG]

    Congratulations! You now have a basic VPS setup with Rackspace and can move onto the next steps!

    Step 1: SSH
    After your server is provisioned with your hosting company, you should be given an IP Address, and a username and password to connect to this server. Usually the username is root, however this could also be a normal user account.

    First, you will need to download a program called PuTTY to connect to your server, which you can find at http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html.

    If you are using Mac or Linux, you can just connect using Terminal, with:
    Code:
    ssh root@127.0.0.1
    Now, run this and the following window should appear:
    You will need to type in the IP Address given to you by your provider, and then press enter.
    A window will then appear, which you will need to type in your username and password. Please note that the password will not show anything as a security feature.
    Now, you should see the following window. As you can see, my server is running Ubuntu 10.04. You can see that it's named testing. If your server is not 10.04 LTS or newer (10.10 or 11.04), I highly recommend asking your host to upgrade.

    Step 2: Updating and upgrading the base system.
    Now, it's a good idea to update and upgrade our system to the latest version before we do anything. So, type in the following:
    apt-get update

    At the end, it should say:
    Code:
    Fetched 12.2MB in 7s (1708kB/s)
    Reading package lists... Done
    Now we should upgrade our packages. This can be done by typing:

    Code:
    apt-get upgrade
    And then type y when it asks us if we would like to upgrade our system.

    Now, I usually reboot at this stage as this is generally the last reboot I make for many months. This can be done by typing:
    Code:
    reboot
    Wait a moment and reconnect to your server as you did before.

    Step 3: Installing everything.
    I have a fork of a script created by VladGH, which installs everything you need to get running. My script also installs Varnish (a caching server), which is highly recommended if you are going to be hosting a large number of static sites, as well as Wordpress.

    To get this to run, simply type:

    Code:
    sudo agt-get install git-core screen unzip
    Now, you need to clone the current branch of the git repository, which you can do by:

    Code:
    mkdir ~/setup-files
    cd ~/setup-files
    wget http://static.seotal.com/lemp.zip
    unzip lemp.zip
    sudo chmod 777 install.sh
    sudo ./install.sh
    This will run through the installation steps for you, which is pretty easy. This may take some time depending on the internet speed, size of the server etc. This usually takes me 30 minutes on a Rackspace cloud server.

    After this has been completed, you will need to start getting your sites setup and working.

    By default, this will work fine. You will might want to tweak nginx or Varnish, however I've been running off this setup for a while and it works fine.

    So now if you access http://yourhostip you should be able to see the PHP info page - this tells you that:

    1) NGINX was installed.
    2) PHP was installed.
    3) You can check for APC/Memcached/MySQL as well.

    If all works fine, you can remove these files.
    Code:
    rm -rf /var/www/index.php
    rm -rf /var/www/apc.php
    
    Step 4: Website Configuration
    Now let's say that you have 3 websites:
    - example.com
    - subdomain.example.com
    - third.example.com

    You can have as many domains/subdomains with NGINX, but I'll just be focusing on these three as an example. Change these to your domain(s) as you go.

    Now you need to change directory to the nginx configuration folder, this can be done via:
    Code:
    cd /etc/nginx/sites-enabled
    Now, we want to remove the default configuration file that the script puts in here, and start adding a file for each domain.

    A technique that I do is running Wordpress Multisite which allows me to run as many Wordpress sites that I need off one installation, as well keeping the code uniform across this site. A best practices guide for this is coming soon.

    Let's just assume that we want a different Wordpress setup for each domain.

    So let's remove the default file:
    Code:
    sudo rm -rf /etc/nginx/sites-enabled/default
    Now, you can copy the below text and put in your own domain name, as well as the location. I recommend having a folder for each domain under /var/www/my-domain , this keeps everything tidy and in one standard location.

    So, let's use nano to create the configuration for example.com with Wordpress.

    Code:
    sudo nano /etc/nginx/sites-enabled/example
    
    Now, copy in the following, and change the details to match your site configuration:
    Code:
    server
    {
        server_name example.com;
        listen 801;
        server_tokens off;
        root /var/www/example;
        index index.php;
        access_log /var/log/nginx/example.access.log;
        error_log /var/log/nginx/example.error.log;
        
        # If you use feedburner, it's a good idea to rewrite.. however I don't use it.
        #if ($http_user_agent !~ FeedBurner) {
        #    rewrite ^/comment/feed/ http://feeds.feedburner.com/your-comment-feed last;
        #    rewrite ^/feed/ http://feeds.feedburner.com/your-feed last;
        #}
        
        # if the requested file exists, return it immediately
        if (-f $request_filename) 
        {
            break;
        }
    
        if (!-e $request_filename) 
        {
            rewrite . /index.php last;
        }
        if (!-e $request_filename) 
        {
            rewrite ^.*$ /index.php last;
        }
    
        location /
        {
            try_files $uri $uri/ index.php;
            gzip  on;
            gzip_http_version 1.0;
            gzip_vary on;
            gzip_comp_level 3;
            gzip_proxied any;
            gzip_buffers 16 8k;
        }
        location ~ \.php$ 
        {
            try_files $uri $uri/ index.php;
            include /etc/nginx/fastcgi.conf;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
        }
    
        location = /favicon.ico
        {
            log_not_found off;
            access_log off;
        }
        location ~ \..*/.*\.php$
        {
            return 403;
        }
        location ~* \.(js|css|png|jpg|jpeg|gif|ico)$
        {
            expires max;
            log_not_found off;
        }
        location ~ /\.ht
        {
            deny  all;
        }
        location ~ /\.git
        {
            deny  all;
        }
    }
    
    Before setting up Wordpress, let's setup PHPMyAdmin. PHPMyAdmin is a PHP application to manage your MySQL databases. You can find more information on its official site.

    Let's get it setup and running!

    I usually run PHPMyAdmin on a different port than 80, or run it through a subdomain (such as phpmyadmin.example.com). For this example I'll be running it through port 82.

    Code:
    mkdir /var/www/phpmyadmin
    cd /var/www/phpmyadmin
    wget http://voxel.dl.sourceforge.net/project/phpmyadmin/phpMyAdmin/3.4.7/phpMyAdmin-3.4.7-all-languages.zip
    mv phpMyAdmin-3.4.7-all-languages/* /var/www/phpmyadmin
    rm -rf /var/www/phpmyadmin/phpMyAdmin-3.4.7-all-languages/
    
    Now we need to configure nginx for phpmyadmin.

    Code:
    nano /etc/nginx/sites-enabled/phpmyadmin
    
    Paste in the following:
    Code:
    server
    {
        listen *:82;
        server_tokens off;
        root /var/www/phpmyadmin;
        index index.php;
        access_log /var/log/nginx/example.access.log;
        error_log /var/log/nginx/example.error.log;
        
        # if the requested file exists, return it immediately
        if (-f $request_filename) 
        {
            break;
        }
    
        location /
        {
            try_files $uri $uri/ index.php;
            gzip  on;
            gzip_http_version 1.0;
            gzip_vary on;
            gzip_comp_level 3;
            gzip_proxied any;
            gzip_buffers 16 8k;
        }
        location ~ \.php$ 
        {
            try_files $uri $uri/ index.php;
            include /etc/nginx/fastcgi.conf;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
        }
    
        location = /favicon.ico
        {
            log_not_found off;
            access_log off;
        }
        location ~ \..*/.*\.php$
        {
            return 403;
        }
        location ~* \.(js|css|png|jpg|jpeg|gif|ico)$
        {
            expires max;
            log_not_found off;
        }
        location ~ /\.ht
        {
            deny  all;
        }
        location ~ /\.git
        {
            deny  all;
        }
    }
    
    Now, restart nginx (and varnish if you used a subdomain):
    Code:
    sudo /etc/init.d/nginx restart
    sudo /etc/init.d/varnish restart
    
    Now, go to http://myip:82 (or your subdomain), and it should bring up Phpmyadmin. Use the details from before to login:
    [​IMG]

    Now, click Databases, then choose the database name, then click create. Make sure you remember this database, as this will be the one we are going to use with your CMS.
    [​IMG]

    Now, let's setup Wordpress under the folder:
    Code:
    sudo mkdir /var/www/example
    cd /var/www/example
    sudo wget http://wordpress.org/latest.zip
    sudo unzip latest.zip
    sudo mv wordpress/* /var/www/example
    rm -rf wordpress
    mv wp-config-sample.php wp-config.php
    chmod 777 wp-config.php
    nano wp-config.php
    
    Now go through this file and fill in the database name (example), user (root), and the database password. Press ctrl+o to save, then enter then press ctrl+x to quit.

    Now, we need to restart nginx and varnish, and install Wordpress.
    Code:
    sudo /etc/init.d/nginx restart
    sudo /etc/init.d/varnish restart
    
    Now, go to your domain (http://example.com/wp-admin/install.php) and see if the Wordpress installation shows.

    Now, go through the Wordpress installation using your details from before.

    Afterwards, it's a good practice to change wp-config.php permissions back:
    Code:
    chmod 655 wp-config.php
    
    All done! From here, you can either install a FTP server, or you can edit your files using a program such as WinSCP, which I highly recommend.

    If you have any additional questions, please feel free to leave a reply here or send me a PM and I'll help you out.
     
    • Thanks Thanks x 27
  2. tsanko

    tsanko Senior Member

    Joined:
    Aug 9, 2008
    Messages:
    833
    Likes Received:
    1,038
    Home Page:
    Please remove affiliate links!
     
  3. Xyz01

    Xyz01 Regular Member Premium Member

    Joined:
    Aug 8, 2011
    Messages:
    300
    Likes Received:
    126
    The only affiliate link is to rackspace, which says (affiliate link) afterwards. Is this an issue?
     
  4. finerpleasures

    finerpleasures Regular Member

    Joined:
    Feb 17, 2009
    Messages:
    322
    Likes Received:
    97
    Anything for windows servers?
     
  5. Xyz01

    Xyz01 Regular Member Premium Member

    Joined:
    Aug 8, 2011
    Messages:
    300
    Likes Received:
    126
    Sure, I might do one later. I recommend Linux due to it being cheaper, runs on less resources etc.

    However, I have ran Windows+NGINX+MySQL+PHP in the past and it worked fine.
     
  6. tzim_t

    tzim_t Registered Member

    Joined:
    Dec 26, 2009
    Messages:
    83
    Likes Received:
    3
    Location:
    Europe
    nice work mate ! thank you
     
  7. blackhataffiliate

    blackhataffiliate Senior Member

    Joined:
    Oct 19, 2008
    Messages:
    847
    Likes Received:
    1,364
    Location:
    USA

    Not allowed, see below

    4. AFFILIATE LINKS
    Affiliate links are only allowed in the Affiliate Programs section of the forum, or in your signature. This includes links to your websites or blogs which redirect to an affiliate link. If you wish to let people know about a new website or program, create ONE thread in the Affiliate Programs forum and provide detailed info about the program and include 2 links: one affiliate link and one non-affiliate link. You MUST give them the choice to sign up under you or not. Also, do not try to avoid our rules by making a post about an affiliate program you found, where you ask people to PM you for the info. This is no different than affiliate link spam. If we are notified of any violation of these rules, your affiliate link may be removed and an infraction or ban will be given at our discretion.

    Code:
    http://www.blackhatworld.com/blackhat-seo/misc.php?do=cfrules
     
  8. teeniegenie

    teeniegenie Supreme Member

    Joined:
    Aug 28, 2010
    Messages:
    1,296
    Likes Received:
    662
    Location:
    The Cool Part of Vegas
    Impressive post OP - plus rep!
     
  9. ericsson

    ericsson Elite Member Premium Member

    Joined:
    Apr 25, 2009
    Messages:
    2,642
    Likes Received:
    8,132
    Occupation:
    www
    Location:
    Swe
    Home Page:
    Yeah, remove your shitty affiliate link.. Which u meantion 23212312312 of times...
     
    • Thanks Thanks x 1
  10. Markthedude

    Markthedude Power Member

    Joined:
    Feb 26, 2010
    Messages:
    572
    Likes Received:
    266
    Occupation:
    Entrepreneur
    Location:
    United States
    This was obviously a lot of work to write up and I'm one of the people you were out to help and I want to say thank you for making literally a step-by-step guide!

    Thanks and rep given!
     
  11. Xyz01

    Xyz01 Regular Member Premium Member

    Joined:
    Aug 8, 2011
    Messages:
    300
    Likes Received:
    126
    No problems, removed.
     
  12. xpwizard

    xpwizard Junior Member

    Joined:
    Nov 6, 2010
    Messages:
    198
    Likes Received:
    122
    Just a note:

    I can't remember 100%, but I don't think nano is a default package in most OS's.

    Code:
    sudo apt-get install nano
    And instead of typing sudo all the time, use sudo -s or sudo su to change the terminal to root (in case you aren't on root already).

    Code:
    sudo su
     
    Last edited: Nov 2, 2011
  13. Xyz01

    Xyz01 Regular Member Premium Member

    Joined:
    Aug 8, 2011
    Messages:
    300
    Likes Received:
    126
    Mm, good point, I included sudo even though most VPS providers give you root access, since most people will ask about sudo.

    nano is included in ubuntu 10.04 which I use in this guide. Thanks for the notes how to install it as well, it's a great command line editor for lazy people.
     
  14. mojacho

    mojacho Junior Member

    Joined:
    Aug 27, 2009
    Messages:
    192
    Likes Received:
    20
    Very Nice tutorial with Pictures.
    Thanks mates for your hard works.
    really appreciated.
     
  15. Mutikasa

    Mutikasa Power Member

    Joined:
    May 23, 2011
    Messages:
    574
    Likes Received:
    207
    Is this what it looks like when u don't have control panel?
     
  16. Seo Lover

    Seo Lover Jr. Executive VIP Jr. VIP Premium Member

    Joined:
    Jan 30, 2011
    Messages:
    5,697
    Likes Received:
    4,127
    Gender:
    Male
    Occupation:
    Hanging Around Interwebs !
    Location:
    <-----------------Sin City
    Great post mate , step by step with images , really helpfull .
     
  17. Xyz01

    Xyz01 Regular Member Premium Member

    Joined:
    Aug 8, 2011
    Messages:
    300
    Likes Received:
    126
    Yep. One time setup then updates. :)
     
    • Thanks Thanks x 1
  18. samlim

    samlim Junior Member

    Joined:
    Jun 7, 2011
    Messages:
    153
    Likes Received:
    15
    Very nice guide thank you
    do you have a guide for reseller hosting ??
    like how to setup WHMCS with WHM/Cpanel ?
     
  19. Mutikasa

    Mutikasa Power Member

    Joined:
    May 23, 2011
    Messages:
    574
    Likes Received:
    207
    and this script to get all work together?
    so does nginx has fast-cgi or not? I'm confused, some tutorials say to instal lighttp to work, some swag-cgi, some php-fpm wich doesn't want to install on debian 5.0.3

    Also, I already installed nginx, php and mysql. Will i do something wrong if I run this script to install it again?

    This is what I get:

    Code:
    ./install.sh: line 273: /opt/php5/bin/phpize: No such file or directory
    32554 pts/0    00:00:00 install.sh
    ./install.sh: line 274: ./configure: No such file or directory
    32564 pts/0    00:00:00 install.sh
    make: *** No targets specified and no makefile found.  Stop.
    make: *** No rule to make target `install'.  Stop.
     
    Last edited: Nov 2, 2011
  20. Amelieetor

    Amelieetor Registered Member

    Joined:
    Jun 9, 2011
    Messages:
    54
    Likes Received:
    13
    Nice guide, thanks, its funny how rackspace have changed vps to cloud server, but it is really the same thing xD