Install PHP and MySQL with Apache

Today we will learn how to install php and MySQL with Apache. Installing PHP and MySql will help you run php on your Apache Server. This tutorial shows you, how to install PHP and Mysql on the ubuntu. Also we will learn how to secure the mysql by setting root password. Let’s see how to install php and MySQL with Apache

Install PHP on Apache

PHP is the component of your setup that will process code to display dynamic content. It can run scripts, connect to your MySQL databases to get information, and hand the processed content over to your web server to display.

Before install it, update the package index on your server

sudo apt update

Run below command to install the PHP

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

In most cases, you will want to modify the way that Apache serves files when a directory is requested. Currently, if a user requests a directory from the server, Apache will first look for a file called index.html. We want to tell the web server to prefer PHP files over others, so make Apache look for an index.php file first.

To do this, type this command to open the dir.conf file in a text editor with root privileges:

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

It will look like this:

<IfModule mod_dir.c>
    DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>

Move the PHP index file to the first position after the DirectoryIndex specification, like this:

<IfModule mod_dir.c>
    DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>

When you are finished, save and close the file by pressing CTRL+X. Confirm the save by typing Y and then hit ENTER to verify the file save location.

After this, restart the Apache web server in order for your changes to be recognized. Do this by typing this:

sudo systemctl restart apache2

Congrats!! You have successfully install the PHP on Apache server.

Installing MySql

On Ubuntu 18.04, only the latest version of MySQL is included in the APT package repository by default. At the time of writing, that’s MySQL 5.7

Now run the below command to install the MySql

sudo apt install mysql-server

The above command will give information about the packages it is going to install. It will ask for the continue, press y and hit enter

The above command will install the MySQL server on your Ubuntu, but will not prompt you to set a password or make any other configuration changes. Because this leaves your installation of MySQL insecure, we will address this next.

Configuring MySQL

For fresh installations, you’ll want to run the included security script. This changes some of the less secure default options for things like remote root logins and sample users. On older versions of MySQL, you needed to initialize the data directory manually as well, but this is done automatically now.

Run the security script:

sudo mysql_secure_installation

The above command will ask some question, Please read them carefully and answer them

Congrats!! You have successfully installed the MySQL

Login to MySQL

Type below command and hit enter it will ask to login into the MySQL

sudo mysql -u root -p

The above command will ask for the password. Enter the password what you enter while the securing the MySQL and hit enter will login into MySQL

If you have any questions or thoughts to share, use the comment form below to reach us.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *