Recommended VPS Complete list of best VPS hosting providers.

How To Add New Site Into Your Apache-based Debian Server

Alright so you installed Apache web server on your VPS running Debian OS on top of it. But then how can you host your websites in it and how to add new website on it? The answer is Apache Virtual Host file. It is a configuration file where you can put necessary configuration regarding websites you want to host it on your server / vps. In Debian (and Ubuntu) you can simply create a specific Virtual Hosts file for each domain or subdomain then enable or disable it using simple command. Curious? Let’s move on to the steps.

Prerequisites

  1. A SSH client like Putty and basic knowledge about how to use it.
  2. Basic knowledge about common Unix command to SSH to your server.
  3. A server or VPS with at least 512MB of RAM (in OpenVZ you can use 256MB but not really recommended). If you not already have one then you can read my list of recommended VPS providers or this top low end cloud server providers. Quick recommendation: Atlantic.net, Digital Ocean and RamNode.
  4. Have one already? Now install Ubuntu or Debian OS on it. In this tutorial I use Debian 8 32-bit on Atlantic’s 512MB cloud server.
  5. Install Apache, MySQL / MariaDB and PHP on it. How to install LAMP on Debian and Ubuntu.
  6. About 20 minutes of your time
  7. a cup of coffee or tea.

The Steps

Step 1 – Login to your server as root or as user with root / sudo privilege. In this article I simply login as root so I ditch sudo, however if you logged in as user, then do not hesitate to add sudo prefix to each command.

2015-07-12_132849

Step 2 – Now you have to firstly create the directory where you will put all your site’s files on it (html, php, jpg, png, css, js, etc). This is being main directory of your websites.

mkdir -p /var/www/domain.com/public_html

# or you can also name it htdocs
mkdir -p /var/www/domain.com/htdocs

change domain.com with your own domain name. Structuring web directories to each domain name will come handy in the future. Here’s an example:

2015-07-15_144616

Step 2 – Now you will also need to grant ownership of that directory to a user and group you wish. Use command below:

chown -R $USER:$USER /var/www/domain.com/public_html

However if you are using WordPress and you want for Auto update to work within Admin page, you can simply chown that directory to the webserver:

chown -R www-data:www-data /var/www/domain.com/public_html

example:

2015-07-15_145704

Step 3 – Make sure everyone can read files to the Apache web root directory, so chmod it to 755:

chmod -R 755 /var/www/

screenshot:

2015-07-15_145848

Step 4 – Now you can upload all your web files to that directory using your favorite FTP client. In this example, for the test purpose you can simply create a new .html file, name it index.html

nano /var/www/domain.com/public_html/index.html

then enter this in that new file:

<html>
 <head>
 <title>Test Page for Domain.com</title>
 </head>
 <body>
 <h1>Welcome To Domain.com website</h1>
 </body>
</html>

example:

2015-07-15_150350

inside Nano editor:

2015-07-15_150332

Save that file and exit (In Nano it is Control+O then Control+X)

Step 5 – Now it is time to create Virtual Hosts file for that website.

nano /etc/apache2/sites-available/domain.com.conf

pic:

2015-07-15_151631

do not forget to always replace domain.com with your own domain name:

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

        ServerAdmin your@email.com
        ServerName domain.com
        ServerAlias www.domain.com
        DocumentRoot /var/www/domain.com/public_html

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

change with your real email and domain name. Example:

2015-07-15_151335

Save changes and exit the editor which in Nano it is Control+O then Control+X.

Step 6 – The Virtual Hosts file has been created and now you can enable it using following command:

a2ensite domain.com.conf

You can understand the command as acronym of “apache 2 enable site“. Also, you need to reload apache service after that:

service apache2 reload

example:

2015-07-15_152120

Step 7 – Now give it a test. Open up your browser and type your domain name in it. You’ll then see the test page we created earlier:

2015-07-15_152504

That’s all.

FAQ

Q: I can’t use the command with Nano part in it?
A: You have to firstly install it:

apt-get install nano -y

Q: I open up my browser but I can’t open my website?
A: You have to firstly setup a proper DNS entry for your domain name. For this, you can use third-party free DNS manager or use your own DNS server if you have it installed. As per my example above, I just use dummy domain name and simply edit my local hosts file (on Windows). Useful tutorial:

Q: I want to add another website or domain, how can I do that?
A: Simply repeat steps above for your another domain.

Enjoy! Do not hesitate to drop comment below.. Have a nice day!

4 Comments

Add a Comment

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

Get more stuff like this
in your inbox

Subscribe and get interesting stuff plus faster updates to your email.