Introduction

LAMP stack is a set of open-source software which contains Linux, Apache, MySQL and PHP. LAMP stack is a popular tech stack to host dynamic PHP websites.

In this tutorial, I will walk you through how to host your PHP website using the LAMP stack.

Prerequisites

In order to complete this tutorial you need to have up and running LAMP stack, to configure follow How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 20.04

Setup Hosting directory

One server block is enabled by default on Apache on Ubuntu, this block is configured to serve document form /var/www/html directory. If you open html directory you will see an index.html page that is already there, this is the default apache page it loads when you visit http://your_domin. We will upload our website code into the html directory and replace the index.html page.

Before uploading our application code you need to change ownership of html directory to the current system user, so the webserver can create and modify files in this directory. To change ownership run:

                sudo chown -R $USER:$USER /var/www/html
                

Now delete the default index.html page and upload/create a new index.php file. To upload file to the server you can use any FTP client such as FileZilla. I have uploaded a new index.php file into the /var/www/html directory, this file contains the following contents:

                
                <html>
                  <head>
                    <title>test page</title>
                  </head>
                  <body>
                    <h1>It works!!</h1>
                    <p>This is the landing page of your_domain.</p?>
                  </body>
                </html>
                
                

Now to go to your browser and access your server’s domain name or IP address once again, you will see a page like this:

If you see your application page or above page in this case, it means your website hosted successfully.

Test Web Server can process PHP Files

Now you have set up the hosting directory for your website, you also have tested the configuration with a simple html file. In this section, we will create a PHP script to confirm that our web server can handle and process requests for PHP files.

Create or upload a new file named info.php in /var/www/html directory, this file will contain the following code:

                
                <?php
                phpinfo();
                
                

Go to your web browser and visit http://your_domain_IP/info.php

You will see a page like this

If you see this page that means your PHP installation working as expected.

This page contains information about your server which is useful in debugging and ensuring that settings applied rightly.

In the production environment, it is recommended that remove this file from the server because it contains sensitive information about your PHP environment. You can always create this file when needed.

Conclusion

Now your hosting environment is ready, Upload your application code into /var/www/html directory, once uploading completed visit http://your_domain_IP you will see your website is up and running.