Creating Your Own Webserver and Hosting A Website from Your Linux Box


Creating your own web server and hosting a website from your Linux box is a great way to learn about web development and gain valuable experience. In this article, we will walk you through the process of setting up a basic web server on your Linux box and hosting a website from it. We will cover the following topics −

  • Installing Apache

  • Configuring Apache

  • Creating a website

Installing Apache

Apache is the most widely used web server software in the world. It is free and open-source, and it runs on almost all operating systems, including Linux. To install Apache on your Linux box, open a terminal window and run the following command −

sudo apt-get update
sudo apt-get install apache2

The first command updates the package list on your system, and the second command installs Apache.

Configuring Apache

Once Apache is installed, you can configure it to serve your website. The main configuration file for Apache is located at /etc/apache2/apache2.conf. You can edit this file using any text editor, but we recommend using nano, which is a simple and easy-to-use text editor.

To open the Apache configuration file, run the following command in your terminal −

sudo nano /etc/apache2/apache2.conf

In the configuration file, you can make changes to the way Apache works. For example, you can change the default port that Apache listens on (which is 80) to a different port, such as 8080. You can also change the directory where Apache looks for website files.

To change the default port, look for the following line in the configuration file −

Listen 80

Change 80 to the desired port number, and save the file. Then, restart Apache to apply the changes −

sudo systemctl restart apache2

To change the directory where Apache looks for website files, you can edit the DocumentRoot directive. The DocumentRoot directive specifies the directory where Apache looks for website files by default. Look for the following line in the configuration file −

DocumentRoot /var/www/html

Change /var/www/html to the directory where you want to store your website files, and save the file. Then, restart Apache to apply the changes −

sudo systemctl restart apache2

Creating a Website

Once Apache is installed and configured, you can create a website and host it on your Linux box. To create a website, follow these steps −

  • Create a directory for your website files −

sudo mkdir -p /var/www/example.com/public_html

    This command creates a directory called example.com in the /var/www/ directory. The public_html directory is where you will store your website files.

  • Create an index.html file in the public_html directory −

sudo nano /var/www/example.com/public_html/index.html

    This command opens a text editor and creates a new file called index.html in the public_html directory. You can replace the default text with your own HTML code to create your website.

  • Set the permissions on the public_html directory −

sudo chmod -R 755 /var/www/example.com/public_html

    This command sets the permissions on the public_html directory so that Apache can access the files and serve them to visitors.

  • Create a virtual host configuration file −

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

    This command opens a text editor and creates a new configuration file for your website. Replace example.com with your domain name. Add the following configuration to the file −

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

    This configuration sets up a virtual host for your website on port 80, the default HTTP port. It also specifies the ServerName and ServerAlias for your website, which tells Apache which requests to handle for your website. Additionally, it sets the DocumentRoot, which is the directory where your website files will be stored, and the location of the error and access logs for your website.

    Once you have added the configuration, save and close the file.

  • Enable the Virtual Host

    To enable the virtual host, run the following command −

sudo a2ensite example.com.conf

    This command enables the virtual host by creating a symbolic link from the configuration file to the sites-enabled directory.

  • Restart Apache

    To apply the changes you've made, you need to restart Apache −

sudo systemctl restart apache2

Now, you should be able to access your website by entering your domain name or IP address in a web browser.

Improving Security and Performance

Now that you have set up a basic web server and hosted a website from your Linux box, there are some additional configurations you can make to improve its security and performance.

Enable HTTPS with SSL/TLS

HTTPS is a secure version of the HTTP protocol that uses SSL/TLS to encrypt data transmitted between the server and the client. Enabling HTTPS on your website is essential for securing user data and protecting against various attacks, such as eavesdropping and man-in-the-middle attacks.

To enable HTTPS, you need to obtain an SSL/TLS certificate from a trusted certificate authority and configure your web server to use it. Let's Encrypt is a popular certificate authority that offers free SSL/TLS certificates.

To obtain a Let's Encrypt certificate, you can use Certbot, a command-line tool that automates the certificate issuance process. Certbot is available in the default repositories of most Linux distributions.

To install Certbot on Ubuntu or Debian, run the following commands −

sudo apt update
sudo apt install certbot python3-certbot-apache

To obtain a certificate for your website, run the following command −

sudo certbot --apache -d example.com -d www.example.com

This command automatically obtains and installs an SSL/TLS certificate for your website and configures Apache to use it.

Enable Firewall

A firewall is a network security system that monitors and controls incoming and outgoing network traffic based on predefined security rules. Enabling a firewall on your Linux box can help protect your web server from various network-based attacks, such as DDoS attacks and port scans.

Ubuntu and Debian come with a built-in firewall called UFW (Uncomplicated Firewall). To enable UFW, run the following commands −

sudo apt update
sudo apt install ufw
sudo ufw enable

By default, UFW blocks all incoming connections and allows all outgoing connections. To allow incoming traffic on port 80 and 443 (HTTP and HTTPS), run the following commands −

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

To view the status of UFW, run the following command −

sudo ufw status

Install and Configure a Caching Mechanism

Caching is the process of storing frequently accessed data in memory or disk to improve performance and reduce the load on the server. Installing a caching mechanism on your Linux box can significantly improve the performance of your web server.

One of the most popular caching mechanisms for Apache is mod_cache. To install mod_cache on Ubuntu or Debian, run the following command −

sudo apt update
sudo apt install libapache2-mod-cache

To enable caching for your website, add the following configuration to your virtual host configuration file −

<IfModule mod_cache.c>
    CacheEnable disk /
    CacheHeader on
    CacheDefaultExpire 3600
    CacheMaxExpire 86400
    CacheIgnoreHeaders Set-Cookie
    CacheStoreNoStore on
</IfModule>

The above configuration enables caching for your website by using the disk cache, setting a default expiration time of 3600 seconds (1 hour), and a maximum expiration time of 86400 seconds (1 day). The CacheIgnoreHeaders directive is used to tell Apache to ignore Set-Cookie headers when caching pages, and CacheStoreNoStore is used to prevent caching of pages that contain the "no-store" directive in their Cache-Control header.

It is important to note that caching can have a negative impact on dynamic websites that rely on user-specific data. In such cases, you may need to configure caching to exclude certain pages or URLs.

To test whether caching is working for your website, you can use the Apache benchmarking tool (ab). Run the following command to install ab on Ubuntu or Debian −

sudo apt update
sudo apt install apache2-utils

Once installed, run the following command to test your website with 100 requests −

ab -n 100 http://example.com/

You should see a significant improvement in the response time of your website after enabling caching.

Conclusion

In this article, we have covered the basic steps to create your own web server and host a website from your Linux box. We started by installing Apache, creating a virtual host configuration, and testing our website. We also explored some additional configurations that you can make to your web server to improve its security and performance, such as enabling HTTPS, setting up a firewall, and installing a caching mechanism.

By following these steps, you can set up a powerful and flexible web server that can serve your website with speed and security. With a little bit of practice and experimentation, you can customize your web server to suit your specific needs and requirements.

Updated on: 23-Jun-2023

111 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements