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

In this tutorial, we will guide you how to add a new website or domain into your Ubuntu server with Apache and PHP.
Before proceeding further make sure you have a working Ubuntu server with Apache, PHP, and MySQL.
Login to your server via SSH, In this example, am using putty to connect the server with SSH.
Next, you will need to create the directory where you can put in all the files of your website. Most common directory name used worldwide is either “htdocs” or “public_html”. You may define your own name as you like.

mkdir -p /home/www/domainame.com/public_html/

Do not forget to replace domainname.com with your domain name and in this article, I am using public_html as root document.

ubuntu-create website1

Once created you have to give ownership permissions to that folder for your username.you can use the following command:

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

Ubuntu create website-2

You also need to change the permission of the /home/www/ directory so your server’s users can read and write files in it. Run the following command:

chmod -R 755 /var/www

ubuntu create website-3

Now we will create an index.html file into our web directory to check if everything is working good.

nano /home/www/cpanelplesk.com/public_html/index.html

Now its time to edit Apache virtual host file, first we can copy the default Apache configuration file by using the below command:

cp /etc/apache2/sites-available/default /etc/apache2/sites-available/domain.com

Now use nano editor to edit the config file:

nano /etc/apache2/sites-available/cpanelplesk.com

Once you are in Nano editor, edit a line named “ServerName” and type your domain name:

ServerName cpanelplesk.com

Ubuntu create website5

if you want to access your website using “www” prefix, you can also add “ServerAlias” line under the ServerName:

ServerAlias www.domain.com

Now define the document root directory whether “htdocs” or “public_html”. In this case, you need to edit the “DocumentRoot” line and change it as follows:

DocumentRoot /var/www/domain.com/public_html

ubuntu create website6

Once done, simply save and exit the editor.

Finally, restart or reload the apache2 service by using this command:

service apache2 restart
or
service apache2 reload

Congratulation you are done. This is all about setting up a new website on Apache-based Ubuntu server. Once you feel everything is set up correctly, you may proceed to build your full website (e.g: installing WordPress, Joomla, Drupal, forum script, etc). Now you can access your website through a web browser to test it. Obviously, you can do that after you’ve setup BIND9 server and update your domain NS at registrar end.

Give your opinion below if you experience any issues or to discuss your ideas and experiences.