How to Install Apache2, MySql & PHP

install-apache2-mysql-and-php-on-ubuntu-server

Install Apache2, MySQL & PHP or Lamp Stack On Ubuntu

Are you ready to set up your own website with a reliable server? This simple guideline is all about how to Install Apache2, MySql and PHP OR LAMP Stack, which stands for Linux, Apache, MySQL, and PHP. It’s the perfect setup for running your website on a virtual private server (VPS). First, you’ll need to choose a VPS provider like Contabo or DigitalOcean.

Once you’ve got your VPS, pick the Ubuntu server image to get started. We’ll focus on Ubuntu for our setup. If you’re on a Windows machine, you must access your server as the root user. To do this, use a program like Putty, which you can download from the official Putty website. Follow our guide, and you’ll install Apache2, MySQL, and PHP up and running smoothly on your server in no time.

Step 1: How To Install Apache2 On Ubuntu

Let’s Start When you log in as a root user in your server through putty in the window or terminal. So simply run the command which is given below to install Apache2 HTTP server on Ubuntu.

sudo apt-get install apache2

After completing the Apache2 server installation, you need to run two more commands, which are given below.

sudo service apache2 stop
sudo service apache2 start

You can also check the Apache2 web server status by running this command.

sudo service apache2 status  
or
sudo systemctl status service.apache2

After completing the Apache2 web server installation, please open your browser and enter your server IP address or http://localhost.Then you will see an Apache2 default page. Like this

Step 2: How To Install MySQL On Ubuntu

Now, We will install the MySQL Database Server on Ubuntu. So, please run the following commands to install the MySQL database server on a virtual private server. MariaDB is more secure, and it is open-source.

sudo apt install mariadb-server mariadb-client

After installing the MySQL Database Server on Ubuntu, You need to run the following commands. Stop and Start the MySQL database.

sudo service mysql stop 
sudo service mysql start

Now check your MySQL Database server status by running the following command. If your MySQL server status is not enabled, then run the help command, which is also given below.

sudo service mysql status
and
sudo systemctl enable mysql.service

After that, Run the following command to secure MariaDB by making a root password and disallowing remote root access.

sudo mysql_secure_installation

When you will run this command on your Ubuntu server. You will see Prompted questions, So please give answers to our guidelines.

  • Enter current password for root (enter for none): Just press the Enter
    Set a root password? [Y/n]: Y
    New password: Choose a password
    Re-enter new password: confirm password
    Remove anonymous users? [Y/n]: Y
    Disallow root login remotely? [Y/n]: Y
    Remove the test database and access it. [Y/n]: Y
    Reload privilege tables now? [Y/n]: Y

Now, Restart your MySQL Database server or MariaDB by running the following command.

sudo service mysql restart 
You can also check your database by running this command
sudo mysql -u root -p

Step 3: How To Install PHP and PHP Modules On Ubuntu

The final step is to install PHP and PHP Modules on Ubuntu 16.0.4 server. So, Please run the following commands to install PHP. PHP 7.1 is not available on Ubuntu default repositories. So, we will run the following command to add the third-party repository to upgrade.

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php

Then, we need to update and upgrade.

sudo apt-get update

Now run the following to install PHP 7.1

sudo apt install php7.1 libapache2-mod-php7.1 php7.1-common php7.1-mbstring php7.1-xmlrpc php7.1-soap php7.1-gd php7.1-xml php7.1-intl php7.1-mysql php7.1-cli php7.1-mcrypt php7.1-zip php7.1-curl

After installing the PHP, Create a new file in the Apache2 default root directory to test the PHP on your virtual private server. So please run the following command.

sudo nano /var/www/html/test.php
after opening a file then add following command and save

<?php phpinfo( ); ?>

Finally, Please open your browser and browse your IP address 1.2.345.56.66/test.php or localhost/test.php. Then, You will see a default PHP page. Like this

Now, Your virtual private server is ready to host your PHP website or WordPress website. If you want to Install WordPress, So please visit How to install wordpress on ubuntu with LAMP