How to Set Up a Local Web Server on Linux?

//

Heather Bennett

Setting up a local web server on Linux can be a useful skill for web developers and enthusiasts alike. It allows you to test and develop websites locally before deploying them to a live server. In this tutorial, we will walk you through the steps to set up a local web server on Linux.

Step 1: Install Apache
The first step is to install the Apache web server, which is one of the most popular web servers in use today. Open your terminal and run the following command:

sudo apt-get install apache2

This will install Apache on your Linux machine.

Step 2: Start Apache
Once the installation is complete, you can start the Apache service by running the following command:

sudo systemctl start apache2

You can also enable Apache to start automatically on system boot with the following command:

sudo systemctl enable apache2

Step 3: Test Apache
To make sure that Apache is running correctly, open your web browser and enter “localhost” or “127.0.1” in the address bar. If you see the default Apache page, then congratulations! Your local web server is up and running.

Step 4: Set Up Virtual Hosts
By default, Apache serves files from the “/var/www/html” directory. However, if you want to host multiple websites on your local server, it’s recommended to set up virtual hosts.

To create a virtual host, first create a new configuration file in the “/etc/apache2/sites-available” directory with a .conf extension. For example:

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

Add the following content to the file:

    

ServerAdmin admin@example.com

    

ServerName example.local

    

ServerAlias www.example.local

    

DocumentRoot /var/www/example

    

ErrorLog ${APACHE_LOG_DIR}/error.log

    

CustomLog ${APACHE_LOG_DIR}/access.log combined

Save the file and exit the text editor.

Step 5: Enable the Virtual Host
To enable the virtual host, create a symbolic link from the configuration file to the “/etc/apache2/sites-enabled” directory:

sudo ln -s /etc/apache2/sites-available/example.conf /etc/apache2/sites-enabled/

Then, restart Apache for the changes to take effect:

sudo systemctl restart apache2

Step 6: Edit Hosts File
To access your virtual host from a web browser, you need to edit your computer’s hosts file. Open the hosts file with a text editor:

sudo nano /etc/hosts

Add the following line at the end of the file:

127.1 example.local www.local

Save the file and exit.

Step 7: Test Your Virtual Host
Create a new directory for your website files:

sudo mkdir /var/www/example

Create an index.html file inside this directory with some content. For example:

nano /var/www/example/index.html

Welcome to Example Local Website!

Now, open your web browser and enter “http://example.local” or “http://www.local” in the address bar. If everything is set up correctly, you should see the content of your index.html file.

Conclusion
Setting up a local web server on Linux is essential for web development and testing. By following these steps, you can easily install Apache, configure virtual hosts, and host multiple websites on your local machine. Experiment with different configurations to enhance your web development skills further.

With proper use of HTML styling elements like for bold text, for underlined text,

    and

  • for lists, and

    ,

    , etc. for subheaders where applicable, this article is visually engaging while providing valuable instructions.