Setting Up a Web Server in Linux: A Step-by-Step Guide for Seamless Hosting

If you have ever thought over the idea of hosting your own website or establishing your own digital product then you have come to the right place because in this article we will go through the journey of setting up your web server in linux. This will be a step-by-step roadmap to transform your Linux machine into a powerful hosting hub. Whether you’re an experienced developer or just a curious enthusiast, you can learn about the process from the ground up and gain the tools you need to publish your work online.

Introduction: Unveiling the Power of Web Server in Linux

If you are new to the Linux world, the I should tell you that you’re stepping into a playground of possibilities. Today, we’re going to set up a web server in Linux – an open-source powerhouse that not only offers stability and security but also grants you several customization options.

Why Linux for Web Servers?

Linux is a popular choice for web servers because of its dependability and versatility. One of the best things about working with open-source collaboration is that you can customize your environment to exactly fit your needs. We’ll take strengths of Linux OS in this tutorial to build a solid framework for hosting your website.

Step 1: Installing a Linux Distribution on your machine

Begin by installing Ubuntu Server on your machine. We have a detailed guide on the installation and setup of a Linux distro on a machine. The installation wizard will guide you through the process, allowing you to choose predefined server setups tailored to your requirements, such as web hosting. You can visit the linux official page to download one of the linux distribution on your machine.

Step 2: Updating and Upgrading Packages

sudo apt update
sudo apt upgrade

Step 2: Install Apache Web Server

Apache, a robust and widely used web server, is our choice for this guide. Install it with. The apache2-utils package will install some useful utilities like Apache HTTP server benchmarking tool (ab).

sudo apt install -y apache2 apache2-utils

Start and enable Apache to ensure it launches on boot:

sudo systemctl start apache2

sudo systemctl enable apache2

Step 3: Configuring Firewall Settings

sudo ufw allow 80

Step 4: Checking Your Web Server – Is Apache Up and Running?

sudo systemctl status apache2

Copy your server’s IP address and paste it into your browser’s address bar. If all is well, you’ll see the default Ubuntu Apache web page.

Step 5: Setting Up Virtual Hosts – Your Domain, Your Rules

If you are planning to host multiple domains on a single server then you will need to setup virtual hosts. We’ll create a domain called “your_domain” (replace it with yours). Create a directory for your_domain and then assign ownership of the directory with $USER enviroment variable and then you have to give read, write and execute permissions to owner and read and write permission to groups and others.

sudo mkdir /var/www/your_domain

sudo chown -R $USER:$USER /var/www/your_domain

sudo chmod -R 755 /var/www/your_domain

Create a sample index.html:

sudo nano /var/www/your_domain/index.html

Paste in some HTML, save, and close:

<html>
    <head>
        <title>Welcome to Your_domain!</title>
    </head>
    <body>
        <h1>Success! The your_domain virtual host is working!</h1>
    </body>
</html>

Save and close the file when you are finished. We have to create a virtual host file in order for Apache to serve the html content.

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

Paste the following configuration and you have to replace your_domain with your domain name.

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName your_domain
    ServerAlias www.your_domain
    DocumentRoot /var/www/your_domain
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Save and close the file when you are finished. Let’s enable the file with the a2ensite tool:

sudo a2ensite your_domain.conf

Disable the default site defined in 000-default.conf:

sudo a2dissite 000-default.conf

You need to restart Apache to implement your changes:

sudo systemctl restart apache2

Apache will start serving your domain name. You can test this by navigating to http://your_domain and you will find your html page showing.

Managing the Apache Server – Starting, Stopping, and More

Now the web server is running, we can go over some basic management commands using systemctl to manage the apche server.

Stop the server

sudo systemctl stop apache2

Start the server

sudo systemctl start apache2

Restart the server

sudo systemctl restart apache2

Reload configuration

sudo systemctl reload apache2

By default, Apache starts automatically when the server boots. If you want to change this behavior:

Disable Apache Server auto-start

sudo systemctl disable apache2

Re-enable Server auto-start

sudo systemctl enable apache2

Conclusion: Your Web Server Adventure Begins!

Congratulations! You’ve set up a web server with Linux, and your digital journey has just begun. Feel free to experiment, host different domains, and explore the vast possibilities that your Linux-powered web server offers.

Leave a comment