How To Setup LAMP Stack on Linux
How To Setup LAMP Stack on Linux

Introduction

LAMP stands for Linux, Apache, MySQL, and PHP. It is an Open Source Web Development platform and installed together to host dynamic websites and web apps with enhanced customization and flexibility, cost-effectively. where Linux use as an operating system, Apache used as a web server, MY SQL as a database to store the data, and PHP as a dynamic content scripting language. 

Prerequisites

  • A Linux server up and running.
  • A non-root user with sudo privileges.

Steps to Setup LAMP Stack on Linux

  • Install MySQL Database
  • Install PHP
  • Setup Virtual Hosts
  • Verifying PHP on Browser

Install MySQL Database

Open the terminal and installing MYSQL in your Linux (Ubuntu) system.

 sudo apt-get update

The command and update if updates are available.

 sudo apt-get install mysql-server

This command installs the MySQL database in your ubuntu server. Provide a password for the “root” user. 

The Mysql is successfully installed !! 

[Note: Default password for the root user is “root”.] 

Install PHP

Using PHP component, you can display dynamic content. It is a component that can connect to your MySQL database and run queries, fetch data, run scripts, and can display the fetch information to the web browser where the application is running.

Using apt system to install PHP with some helper packages so that PHP code can talk to MySQL database and Apache server. Install using below command:

 sudo apt install php libapache2-mod-php php-mysql

Open the dir.conf file with root privileges in a text editor with below command:

sudo nano /etc/apache2/mods-enabled/dir.conf

The file will be as below:


    DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm

The index.php should be on first place so please move it from mid to first like below:


    DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm

Save and close the file.

Restart the services of Apache:

sudo systemctl restart apache2

You can review the status of Apache with below command:

sudo systemctl status apache2

Setup Virtual Hosts

The Virtual hosts are similar to server blocks in Nginx. It is used to hosts multiple domains from a single server. Let’s setup a domain e.g. zehncloud.com.

Note: Please replace this with your own domain name.

In Apache, there is one server block which is enabled by default used to server files or documents from /var/www/html directory.

sudo mkdir /var/www/zehncloud.com

Change the ownership from root user to Apache default user:

sudo chown -R www-data:www-data /var/www/zehncloud.com

Change the permission using below command:

sudo chmod -R 755 /var/www/zehncloud.com

Open index.html file with below command:

nano /var/www/zehncloud.con/index.html

Paste below lines inside index.html file:


    
        

The Zehncloud virtual host is working!


    

Save and close.

The best practice is to not edit the default configuration but create a new one for your domain:

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

After running above command to create a new file, paste below lines into it:


    ServerAdmin webmaster@localhost
    ServerName zehncloud.com
    ServerAlias www.zehncloud.com
    DocumentRoot /var/www/zehncloud.com
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

The above configuration is the copy of default configuration with some update as per our new domain. We have changed the Server Name, Server Alias and Document Root for our new domain.

Save and close above file.

Run below command to enable the file with a2ensite tool.

sudo a2ensite zehncloud.com.conf

And disable the default site with below command:

sudo a2dissite 000-default.conf

You can verify the apache configuration syntax with below command:

sudo apache2ctl configtest

The output will be like below:

Syntax OK

Restart the services of Apache

sudo systemctl restart apache2

Verifying PHP on Browser

You can now verify that apache is serving the domain name or not by navigating to your browser and enter the URL https://www.zehncloud.com and the output should be like below:

Hence, your virtual host is fully set up. Now we will test the PHP configuration before deploying any application.

Create a very basic PHP script and name it info.php to test that your server is configured correctly or not for PHP. This file must be served to the webroot directory to let Apache find this file and can serve it correctly.

sudo nano /var/www/zehncloud.com/info.php

Paste below lines into info.php file:

Save and close this file.

You can verify that the web server is displaying the content which is generated by the above PHP script correctly or not. Hit the below URL into the browser:

https://www.zehncloud.com/info.php

In this way the PHP is installed and running successfully!

Conclusion

In the above document, we have installed and configured MySQL database and PHP with Apache web server then created a virtual hosts block for deploying any web application and can be multiple using more Apache virtual hosts.

Related Posts