Configure local website – Ubuntu, Linux

Share and Enjoy !

Shares

Configure Local Website on local development setup is very important in development process. Local setup gives you freedom to test your new idea, development process, design, concept, themes and plugins ( WordPress ). It helped developer to test website before upload on production server. We can work on local website without internet connections, so performance of website also not limited by internet connection speed. Working with live server is very time taking process. Developer need to upload files on server to test it. But working on local setup is faster as you are exchanging information within your computer.

Configure Local Website

To setup local website we need web server to process our web request. We will call it Local Server. Enabling localhost, your local computer turn in server. You will need to download and install softwares. We are working on Ubuntu, need to initizliaz The Apache is one of leading web server ( Feb 2020 serve : 37.47% ) and favourite way to host web content on Internet. It is very flexible and loaded with other useful features. In this small tutorial, we will see How To Configure website on local machine .

Install Apache

So Apache is open source web server and available in Ubuntu’s default repositories. So let update local package index to get updated package.

$ sudo apt-get update

Let Install apache2 package

$ sudo apt-get install apache2 

Check Apache is running or not with following command.

$ sudo systemctl status apache2 
Output

apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2020-05-30 10:53:41 IST; 1 weeks 1 days ago
Docs: https://httpd.apache.org/docs/2.4/
Main PID: 1386 (apache2)
Tasks: 11 (limit: 9377)
Memory: 30.6M
CGroup: /system.slice/apache2.service
├─ 1386 /usr/sbin/apache2 -k start
├─116040 /usr/sbin/apache2 -k start
├─116041 /usr/sbin/apache2 -k start
├─116043 /usr/sbin/apache2 -k start
├─116044 /usr/sbin/apache2 -k start

Apache comes with default basic website. Now check it in your browser http://local_server_ip

Setup Virtual Host

Let set up our local website using Apache Virtual Host. We can configure multiple websites under different domains. We are using my_website domain for example, you will replace it with your domain.

Create a Directory structure to save all required pages of our websites. So we are creating my_website ( Replace it with your domain or website name ) folder in apache default root folder /var/www/my_website

$ sudo mkdir -p /var/www/html/my_website/public_html

Let give access to non root user to modify, delete and create file in working folders

$ sudo chown -R $USER:$USER /var/www/html/my_website/public_html

We also give read and execute permission to parent folder so website will work properly.

$ sudo chmod -R 755 /var/www

In the next step, we create an Apache conf file for the website. This file carries importance details like folder path – Document root, Domain / Server Name, and other details. Apache comes with default conf file 000-default.conf in /etc/apache2/sites-available/ We will make a copy of the default file and will change the details according to our requirements.

$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/my_website.conf

Write following command to Edit my_website.conf to change details.

$ sudo nano /etc/apache2/sites-available/my_website.conf
<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName mywebsite.com
    ServerAlias www.mywebsite.com
    DocumentRoot /var/www/html/my_website/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Save and close .conf file, Enable it and check configuration error for any syntax errors .

$ sudo a2ensite my_website.conf - ( Enable Config )

$ sudo apache2ctl configtest - ( Check Error )

Output: Syntax OK

To access our website, we will setup custom domain in hosts file under /etc/hosts

$ sudo nano /etc/hosts

Now Enter following record in the hosts file, Save, and Exit. The local custom domain name ( ServerName ) which we used in my_website.conf. Restart apache2 to work it properly.

127.0.1.1 mywebsite.com

$ sudo systemctl restart apache2

So We hope you follow all steps properly and access your website properly. Now we have a web server, host, and test your content developed in different web technologies before uploading in production servers.

Hostinger

Share and Enjoy !

Shares

Leave a Reply