A registered domain name for configuring the virtualhost file (recommended). The domain name should point to the public IP or elastic IP of the instance.<\/li><\/ul>\n\n\n\nStep 1: Install Apache HTTP Server<\/h2>\n\n\n\n The Apache package is available in the default Ubuntu 22.04 repositories, so you can easily install it using the APT package manager.<\/p>\n\n\n\n
First, log in to your server instance and update the local package index as follows:<\/p>\n\n\n\n$ sudo apt update<\/code>\n\n\n\nNext, install Apache HTTP as follows:<\/p>\n\n\n\n$ sudo apt install apache2 -y<\/code>\n\n\n\nOnce installed, Apache HTTP service starts automatically and no intervention is required to start it. To verify that the service is running, run the following command:<\/p>\n\n\n\n$ sudo systemctl status apache2<\/code>\n\n\n\n <\/figure>\n\n\n\nConsider enabling the Apache service to start automatically as a daemon service at start up as follows:<\/p>\n\n\n\n$ sudo systemctl enable –now apache2<\/code>\n\n\n\nUbuntu 22.04 comes with the firewall already installed. When enabled, all inbound traffic is blocked by default. As such, you need to allow Apache traffic through the firewall in case it\u2019s enabled. To achieve this, run the command:<\/p>\n\n\n\n$ sudo ufw allow “Apache Full”<\/code>\n\n\n\nThis profile opens both port 80 (normal, unencrypted web traffic) and port 443 (TLS\/SSL encrypted traffic). You could also use the profile \u201cApache\u201d which opens only port 80, or \u201cApache Secure\u201d which opens only port 443.<\/p>\n\n\n\n
Then reload the firewall for the changes to come into effect.<\/p>\n\n\n\n$ sudo ufw reload<\/code>\n\n\n\n <\/figure>\n\n\n\nTo confirm the changes made on the firewall, run the following command: <\/p>\n\n\n\n$ sudo ufw status<\/code>\n\n\n\n <\/figure>\n\n\n\nTo ensure that the web server is accessible from a web browser, browse your server\u2019s IP address or registered domain name.<\/p>\n\n\n\n
The default Apache welcome page will be displayed:<\/p>\n\n\n\n <\/figure>\n\n\n\nIf you are able to view this page, your web server has been set up successfully.<\/p>\n\n\n\n
Step 2: Install MariaDB Database Server<\/h2>\n\n\n\n The next component of the LAMP stack is the MySQL database server. However, in this guide, we opt to install MariaDB instead due to its high performance, and wealth of features and functionality. <\/p>\n\n\n\n
To install MariaDB on Ubuntu 22.04 run the following command:<\/p>\n\n\n\n$ sudo apt install mariadb-server -y<\/code>\n\n\n\nLike the Apache service, MariaDB service starts automatically once MariaDB is installed. To confirm this, run the command:<\/p>\n\n\n\n$ sudo systemctl status mariadb<\/code>\n\n\n\n <\/figure>\n\n\n\nOnce the installation is complete, start and enable MariaDB as follows:<\/p>\n\n\n\n$ sudo systemctl enable mariadb<\/code>\n\n\n\nTo check the version of MariaDB installed, run the command:<\/p>\n\n\n\n$ mariadb –version<\/code>\n\n\n\n <\/figure>\n\n\n\nFrom the above output, you can see that we have installed MariaDB 10.6.7.<\/p>\n\n\n\n
Hardening MariaDB<\/h3>\n\n\n\n The default settings that come with MariaDB are weak in the face of database intrusions or breaches. Your database is easily prone to external attacks. As such, you need to take an extra step and secure the database by running the mysql_secure_installation <\/strong>script.<\/p>\n\n\n\nTo start this process, run the shell script as follows:<\/p>\n\n\n\n$$ sudo mysql_secure_installation<\/code>\n\n\n\nThe script walks you through a couple of prompts. First, you will be required to specify the root password for the database server. <\/p>\n\n\n\n
Press \u201cENTER\u201d since you have configured it already and decline switching to Unix Socket authentication which is considered less secure. <\/p>\n\n\n\n
Next, provide a strong root password and confirm.<\/p>\n\n\n\n <\/figure>\n\n\n\nFor the remaining prompts, type \u201cY<\/strong>\u201d to secure MariaDB to the best security standards. This removes anonymous users, disallows root login from remote locations, removes the test database and access to it, and finally saves the changes.<\/p>\n\n\n\n <\/figure>\n\n\n\nStep 3: Install PHP and PHP Extensions<\/h2>\n\n\n\n Now that you have Apache and MariaDB installed, proceed to install PHP and the required extensions. PHP is available in the default Ubuntu repositories. You’ll also need php-mysql, a PHP module that enables PHP to interact with MySQL-based databases. To allow Apache to handle PHP files, you also need libapache2-mod-php.<\/p>\n\n\n\n
The dependencies for core PHP packages will be installed automatically. Run the following command:<\/p>\n\n\n\n$ sudo apt install php libapache2-mod-php php-mysql<\/code>\n\n\n\nOnce the installation is complete, confirm the version installed using the following command:<\/p>\n\n\n\n$ php -v<\/code>\n\n\n\n <\/figure>\n\n\n\nNow, let\u2019s test out PHP integration with Apache web server. Create a sample info.php <\/strong>file in the document root directory.<\/p>\n\n\n\n$ echo “” | sudo tee \/var\/www\/html\/info.php<\/code>\n\n\n\nRestart Apache to apply the changes made.<\/p>\n\n\n\nsudo systemctl restart apache2<\/code>\n\n\n\nNext, open a web browser and go to the following address:<\/p>\n\n\n\n
http:\/\/server-ip\/info.php<\/strong><\/p>\n\n\n\nYou should see the following webpage detailing the version of PHP and information regarding PHP modules:<\/p>\n\n\n\n <\/figure>\n\n\n\nStep 4: Set Up a Virtual Host for Your Website (Optional)<\/h2>\n\n\n\n A virtual host file is an Apache directive that enables you to host multiple websites on a single host. By default, Apache comes with a virtual host file called 000-default<\/strong> located in the \/etc\/apache2\/sites-available\/ <\/strong>path.<\/p>\n\n\n\nThis section demonstrates how to set up a virtual host file for your registered domain. For illustrative purposes, we will use the linuxtechgeek.info<\/strong> domain.<\/p>\n\n\n\nFirst, create the website directory that will contain the websites.<\/p>\n\n\n\nsudo mkdir -p \/var\/www\/html\/linuxtechgeek.info<\/code>\n\n\n\nNext, configure the required directory permissions and ownership for the website directory.<\/p>\n\n\n\n$ sudo chown www-data:www-data -R \/var\/www\/html\/linuxtechgeek.info<\/code>\n$ sudo chmod 775 -R \/var\/www\/html\/linuxtechgeek.info<\/code>\n\n\n\nThen, create a sample HTML page for testing the virtual host on the browser.<\/p>\n\n\n\n$ sudo vim \/var\/www\/html\/linuxtechgeek.info\/index.html<\/code>\n\n\n\nPaste the sample HTML code as shown:<\/p>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n<\/code>\n\n\n\nSave and exit. Next, create a virtual host configuration file. <\/p>\n\n\n\n$ sudo vim \/etc\/apache2\/sites-available\/linuxtechgeek.info.conf<\/code>\n\n\n\nPaste the following lines of code and be sure to replace every instance of linuxtechgeek.info<\/strong> with your own registered domain name.<\/p>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n<\/code>\n\n\n\nSave the changes and exit the virtual host file. Next, enable the virtual host as follows:<\/p>\n\n\n\n$ sudo a2ensite linuxtechgeek.info<\/code>\n\n\n\nThen reload Apache.<\/p>\n\n\n\n$ sudo systemctl reload apache2<\/code>\n\n\n\nConfirm the Apache configuration file is error-free.<\/p>\n\n\n\n$ sudo apache2ctl configtest<\/code>\n\n\n\n <\/figure>\n\n\n\nNow head over to your browser and check if the virtual host is enabled and working.<\/p>\n\n\n\n
http:\/\/domain<\/strong><\/p>\n\n\n\n <\/figure>\n\n\n\n