How to Setup Web Hosting Server on Ubuntu

Hello Dev, welcome to our journal article on how to setup web hosting server on Ubuntu. In this article, we will guide you through the process of configuring a web server on Ubuntu operating system. We will be using Apache as the web server software and MySQL as the database management system.

Step 1: Install Ubuntu and Updates

The first step towards setting up a web hosting server is installing an operating system. Here, we will be using Ubuntu 20.04 LTS version. After the installation process, the next step is updating the system to ensure it has the latest security patches and software.

To update your Ubuntu operating system, use the following command:

Command
Description
sudo apt update
Updates package lists for upgrades and installs
sudo apt upgrade
Upgrades packages

This will update your system to the latest available packages.

Step 2: Installing Apache Web Server

Apache is a free and open-source web server that is widely used for hosting websites. To install Apache on Ubuntu, use the following command:

Command
Description
sudo apt install apache2
Installs Apache 2 web server

After the installation of Apache is complete, you can check the status of the Apache server by running the following command:

Command
Description
sudo systemctl status apache2
Checks status of Apache 2 web server

If the installation was successful, the output should indicate that the Apache service is active and running.

Step 3: Installing MySQL Database Management System

MySQL is a popular open-source relational database management system. It is used to store and manage data for websites and applications. To install MySQL on Ubuntu, use the following command:

Command
Description
sudo apt install mysql-server
Installs MySQL server

After the installation of MySQL is complete, you can check the status of the MySQL server by running the following command:

Command
Description
sudo systemctl status mysql
Checks status of MySQL server

If the installation was successful, the output should indicate that the MySQL service is active and running.

Step 4: Installing PHP

PHP is a scripting language that is commonly used to build dynamic web pages. It can be used with Apache and MySQL to develop dynamic web applications. To install PHP on Ubuntu, use the following command:

Command
Description
sudo apt install php libapache2-mod-php php-mysql
Installs PHP, libapache2-mod-php, and php-mysql packages

After the installation of PHP is complete, you can check the status of PHP by running the following command:

Command
Description
php -v
Checks version of PHP installed

If the installation was successful, the output should display the version of PHP that is installed on your system.

Step 5: Configuring Apache for PHP

After installing PHP, you need to configure Apache to use PHP. To do this, you need to modify the Apache configuration file. To open the file, use the following command:

Command
Description
sudo nano /etc/apache2/mods-enabled/dir.conf
Opens the Apache configuration file in nano editor

In the configuration file, you need to move the PHP module to the beginning of the list of modules. To do this, you need to modify the file to look like this:

<IfModule mod_dir.c>DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm</IfModule>

After making the changes, save the file and restart Apache by using the following command:

Command
Description
sudo systemctl restart apache2
Restarts Apache 2 web server
READ ALSO  Everything Dev Needs to Know About XSI Host Server

Step 6: Creating a Database and User in MySQL

Before you can host a website, you need to create a database and a user in MySQL. You can create both by logging into the MySQL shell using the following command:

Command
Description
sudo mysql
Logs into MySQL shell

After logging in, you can create a database by using the following command:

CREATE DATABASE dbname;

Where ‘dbname’ is the name of the database you want to create.

You can create a user and grant them access to the database by using the following command:

GRANT ALL ON dbname.* TO 'username'@'localhost' IDENTIFIED BY 'password';

Where ‘dbname’ is the name of the database, ‘username’ is the name of the user, and ‘password’ is the password for the user.

Step 7: Uploading Website Files to the Server

Once you have completed the previous steps, you can upload your website files to the server. You can use a tool like FileZilla to transfer files from your local machine to the server.

Step 8: Configuring Apache Virtual Hosts

After uploading your website files, you need to configure Apache virtual hosts to serve your website. To create a new virtual host, you need to create a new configuration file in the ‘/etc/apache2/sites-available/’ directory. You can use the following command to create a new configuration file:

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

Where ‘example.com’ is the name of your website.

In the configuration file, you need to add the following information:

<VirtualHost *:80>ServerName example.comServerAlias www.example.comDocumentRoot /var/www/html/example.comErrorLog ${APACHE_LOG_DIR}/error.logCustomLog ${APACHE_LOG_DIR}/access.log combined</VirtualHost>

Where ‘example.com’ is the name of your website and ‘/var/www/html/example.com’ is the path to your website files.

After creating the configuration file, you need to enable the virtual host by using the following command:

sudo a2ensite example.com.conf

Where ‘example.com.conf’ is the name of your configuration file.

After enabling the virtual host, you need to restart Apache by using the following command:

sudo systemctl restart apache2

Step 9: Testing Your Website

After completing all the previous steps, you can test your website by opening a web browser and navigating to your website URL.

FAQ

What is a web hosting server?

A web hosting server is a computer that is used to store and deliver web pages and files to users over the internet. It is responsible for serving web pages to users when they request them through a web browser.

What is Ubuntu?

Ubuntu is a free and open-source operating system that is based on the Linux kernel. It is widely used for hosting websites and applications due to its stability and security features.

What is Apache?

Apache is a free and open-source web server software that is used to host websites and applications. It is the most widely used web server software in the world.

What is MySQL?

MySQL is a free and open-source relational database management system. It is used to store and manage data for websites and applications.

What is PHP?

PHP is a server-side scripting language that is used to create dynamic web pages. It can be used with Apache and MySQL to develop dynamic web applications.