DEV Community

Cover image for How to Create a Robust Ubuntu Web Server Using Apache, MySQL, PHP, and Virtual Hosts
Ashim Rudra Paul
Ashim Rudra Paul

Posted on

How to Create a Robust Ubuntu Web Server Using Apache, MySQL, PHP, and Virtual Hosts

Easily Deploy and Manage Your Web Development Environment with This Complete Ubuntu Setup Guide

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Starting and Enabling Apache

Start the Apache service and enable it to start on boot:

sudo systemctl start apache2
sudo systemctl enable apache2
Enter fullscreen mode Exit fullscreen mode

Verifying Apache Installation

To verify that Apache is running, use the following command:

sudo systemctl status apache2
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Securing MySQL Installation

To secure your MySQL installation, run the security script:

sudo mysql_secure_installation
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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:

Add the Ondrej PHP PPA, which always provides the latest stable PHP versions:

sudo add-apt-repository ppa:ondrej/php
sudo apt update
Enter fullscreen mode Exit fullscreen mode

Install the latest PHP version:

sudo apt install php libapache2-mod-php
Enter fullscreen mode Exit fullscreen mode

Install common PHP extensions:

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
Enter fullscreen mode Exit fullscreen mode

Restart Apache

sudo systemctl restart apache2
Enter fullscreen mode Exit fullscreen mode

*Install Composer For Laravel *

Update Package Manager

First, ensure your system is updated :

sudo apt update
Enter fullscreen mode Exit fullscreen mode

Install Required Dependencies

Make sure you have curl and php-cli installed:

sudo apt install curl php-cli unzip
Enter fullscreen mode Exit fullscreen mode

Download and Install Composer

Run the following commands to install Composer on Ubuntu :

curl -sS https://getcomposer.org/installer -o composer-setup.php
php composer-setup.php --install-dir=/usr/local/bin --filename=composer
Enter fullscreen mode Exit fullscreen mode

Verify the Installation

Check if Composer is installed successfully:

composer --version
Enter fullscreen mode Exit fullscreen mode

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/
Enter fullscreen mode Exit fullscreen mode

Setting Proper Permissions

Ensure the correct ownership and permissions :

sudo chown -R $USER:$USER /var/www/
sudo chmod -R 777 /var/www/
Enter fullscreen mode Exit fullscreen mode

Creating a Virtual Host File

Create a configuration file for your site :

sudo nano /etc/apache2/sites-available/000-default.conf
Enter fullscreen mode Exit fullscreen mode

Add the following configuration:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerAlias *
    UseCanonicalName Off
    VirtualDocumentRoot /var/www/%0

    <Directory "/var/www">
        AllowOverride All
        Require all granted
        Options Indexes FollowSymLinks
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Enter fullscreen mode Exit fullscreen mode

Enabling the New Virtual Host

Enable the new site and test the configuration:

sudo a2ensite 000-default.conf
sudo apache2ctl configtest
Enter fullscreen mode Exit fullscreen mode

Restarting Apache

Restart Apache to apply the changes:

sudo systemctl restart apache2
Enter fullscreen mode Exit fullscreen mode

Editing the Hosts File

Map your domain to the local server by editing the hosts file:

sudo nano /etc/hosts
Enter fullscreen mode Exit fullscreen mode

Add the following line:

127.0.0.1       demo
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
sarwarasik profile image
Sarwar Hossain

Thanks dear for share