How to Create a Robust Ubuntu Web Server Using Apache, MySQL, PHP, and Virtual Hosts
Importance of a Web Server
A web server is the backbone of any website, serving as the platform that delivers content to users across the globe. The efficiency and reliability of your web server are critical to the success of your online presence.
Overview of Ubuntu, Apache, MySQL, PHP, and Virtual Hosts
This article will guide you through setting up a complete web server environment on Ubuntu, using Apache as the web server, MySQL as the database server, and PHP as the scripting language. We will also cover the creation and configuration of virtual hosts, which allow you to run multiple websites on a single server.
Purpose of the Article
The purpose of this guide is to provide a detailed, step-by-step process for setting up a robust web server on Ubuntu, tailored for both beginners and advanced users.
Preliminary Setup
Choosing the Right Hardware
Before diving into software installation, it’s important to ensure that your hardware is adequate for the tasks you’ll be performing. Consider factors such as CPU power, RAM, and storage capacity based on the expected load.
Installing Ubuntu Server
Download the latest version of Ubuntu Server from the official Ubuntu website.
Create a bootable USB drive and install Ubuntu Server on your machine.
Follow the on-screen instructions to complete the installation.
Updating and Upgrading Ubuntu
Once Ubuntu is installed, it’s essential to update and upgrade the system to ensure all packages are current.
sudo apt update
sudo apt upgrade
Installing Apache
Understanding Apache Web Server
Apache is one of the most widely used web servers, known for its robustness, flexibility, and extensive module support.
Steps to Install Apache
Install Apache using the following command:
sudo apt install apache2
Starting and Enabling Apache
Start the Apache service and enable it to start on boot:
sudo systemctl start apache2
sudo systemctl enable apache2
Verifying Apache Installation
To verify that Apache is running, use the following command:
sudo systemctl status apache2
Installing MySQL
Understanding MySQL Database Server
MySQL is a powerful relational database management system used to store and manage data for websites and applications.
Steps to Install MySQL
Install MySQL with the command:
sudo apt install mysql-server
Securing MySQL Installation
To secure your MySQL installation, run the security script:
sudo mysql_secure_installation
Follow the prompts to set the root password, remove anonymous users, and secure the database.
Testing MySQL Functionality
Log in to the MySQL shell to ensure it's working correctly:
sudo mysql -u root -p
Installing PHP
Understanding PHP Scripting Language
PHP is a popular server-side scripting language used for web development. It is especially suited for creating dynamic content and interacting with databases.
Steps to Install PHP
Install PHP using the following command:
sudo apt install php libapache2-mod-php php-mysql
sudo apt install php-mbstring php-mysql php-curl php-cli php-dev php-imagick php-soap php-zip php-xml php-imap php-xmlrpc php-gd php-opcache php-intl
Integrating PHP with Apache
After installing PHP, you may need to adjust the Apache configuration to prioritize PHP files:
sudo nano /etc/apache2/mods-enabled/dir.conf
Configuring Virtual Hosts
Explanation of Virtual Hosts
Virtual hosts allow you to host multiple domains on a single server. Each domain can have its own separate configuration, including document root, log files, and more.
Creating Directory Structure for Sites
Create a directory for your new site:
sudo mkdir /var/www/demo
Setting Proper Permissions
Ensure the correct ownership and permissions:
sudo chown -R $USER:$USER /var/www/demo
sudo chmod -R 755 /var/www/demo
Creating a Virtual Host File
Create a configuration file for your site:
sudo nano /etc/apache2/sites-available/demo.conf
Add the following configuration:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName demo
ServerAlias www.demo
DocumentRoot /var/www/demo
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Enabling the New Virtual Host
Enable the new site and test the configuration:
sudo a2ensite demo.conf
sudo apache2ctl configtest
Restarting Apache
Restart Apache to apply the changes:
sudo systemctl restart apache2
Editing the Hosts File
Map your domain to the local server by editing the hosts file:
sudo nano /etc/hosts
Add the following line:
127.0.0.1 demo
Testing Virtual Hosts Configuration
Finally, test your new virtual host by accessing http://demo/
in your web browser. You should see the welcome page created earlier.
Subscribe to my newsletter
Read articles from Ashim Rudra Paul directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Ashim Rudra Paul
Ashim Rudra Paul
I am a Software Engineer at SJ Innovation with over 2 years of experience specializing in the MERN stack. My expertise spans TypeScript, Next.js, React.js, React Native, Express.js, Firebase, Supabase, MongoDB, PostgreSQL, and MySQL. I hold a degree in Computer Science and Engineering from Sylhet Polytechnic Institute and have earned certifications in MERN stack web development and JavaScript, C, C++, Python programming. Previously a Team Lead at elPixala, I excel in collaborating with product and design teams to create seamless user experiences. I prioritize rigorous testing and debugging to ensure high-quality performance. What sets me apart is my passion for coding and problem-solving. I thrive on crafting innovative solutions to complex challenges. Recently venturing into competitive programming on platforms like HackerRank and Codeforces has further sharpened my skills. Choose me for my proven track record of delivering dynamic web applications that combine functionality with visual appeal. My commitment to quality ensures top-notch results every time.