Hackernoon logoHow to Install and Configure PHP for Apache and MySQL by@Bairesdev

How to Install and Configure PHP for Apache and MySQL

Author profile picture

@BairesdevBairesDev

Powerful insights in business, technology, and software development.

At some point, your business is going to need to run a full LAMP (Linux Apache MySQL PHP) stack. It might be for a website, a web application, a backend, a frontend, or anything in between. If you're a developer, you certainly understand the importance of getting PHP to work with web and database servers. And if you work with one of the many PHP development services on the market, this either is or will be your bread and butter.

And although you might be a rockstar PHP developer, it doesn't necessarily mean you know how to install PHP, MySQL, and Apache and get them configured and working with one another.

Fortunately, help isn't far away. In fact, that's exactly what we're going to do nowโ€”install PHP, Apache, and MySQL and then find out how to configure PHP.

We'll take a look at this process on the Linux operating system (both Ubuntu- and Red Hat-based platforms). If Linux isn't your tool of choice, you only need to modify a few bits of the process to make it work with either macOS or Windows.

And with that said, let's get down to work.

Installing PHP, Apache, and MySQL

There are a number of ways to get this to work. You can install the pieces individually, or as a group (on Ubuntu). First, let's find out how to do this, piece by piece.

First on Ubuntu. Remember, we want to install PHP such that it'll work with both Apache and MySQL.ย 

Log into your Ubuntu server and install Apache with the command:

sudo apt-get install apache2 -y

When that installation completes, install the MySQL server with the command:

sudo apt-get install mysql-server -yย 

Both of the above commands will also pick up a few other dependencies along the way. When that completes, make sure to secure the database server with the command:

sudo mysql_secure_installation

Finally, install PHP and the necessary bits to connect it to both Apache and MySQL with the command:

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

You now have Apache, MySQL, and PHP installed on Ubuntu Server, along with the software necessary for them to work together. Of course, you can make that a lot easier, by installing them all together with a single command:

sudo apt-get install lamp-server^ -y

Once everything is installed, you want to make sure both Apache and MySQL are started and enabled with the commands:

sudo systemctl start apache2
sudo systemctl enable apache2
sudo systemctl start mysql
sudo systemctl enable mysql

To do this on a Red Hat based distribution (such as CentOS 8 Stream), the commands are quite different.ย 

To install Apache, the command is:

sudo dnf install httpd -y

To install the MySQL database server, the command is:

sudo dnf install mysql-server -y

Secure the database installation with the command:

sudo mysql_secure_installation

Enable both Apache and MySQL with the commands:

sudo systemctl start httpd
sudo systemctl enable httpd
sudo systemctl start mysql
sudo systemctl enable mysql

Finally, to install PHP (and the connecting bits), you must first enable a specific repository (the EPEL repo) with the command:

sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm

Next, add the Remi repository with the command:

sudo dnf install https://rpms.remirepo.net/enterprise/remi-release-8.rpm

If you get a certificate error after running this command, you might need to temporarily disable certificate checks. To do that, issue the command:

sudo nano /etc/yum.conf

At the bottom of that file add the line:

sslverify=false

Save and close the file and then re-run the above command to add the Remi repository.

Finally issue the following command to install PHP and the various components:

sudo dnf -y install php php-{cli,fpm,mysqlnd,zip,devel,gd,mbstring,curl,xml,pear,bcmath,json}

After installing PHP, make sure to remove the sslverify=false line from the yum.conf file.

Unfortunately, CentOS doesn't have an equivalent to installing lamp-server^, so you must install them one piece at a time.

Configuring PHP

No matter what operating system you use, PHP is configured in the php.ini file. The trick is knowing where that file is located. For example, on CentOS 8 Stream, that file is in /etc. To edit the php.ini file in CentOS 8 Stream, you'd issue the command:

sudo nano /etc/php.ini

On Ubuntu-based servers, the php.ini file is found in /etc/php/X/apache2/ (where X is the PHP release number). For example, on Ubuntu 20.04, you'd open the file for editing with the command:

sudo nano /etc/php/7.4/apache2/php.ini

In Windows, that path would be C:\Program Files\PHP\X\php.ini (Where X is the release number).

In macOS, that path would be /etc/php.ini.

The php.ini file is quite large. As of PHP 7.4, the file is 1673 lines long, so there's a lot of configuration options to look through. However, the configuration options you'll want to edit will depend on what your needs are. However, there are a few options that are more commonly changed within the php.ini file. Those options are (listed with their default values):

memory_limit = 128M - the maximum amount of memory a script may consume.
session.gc_maxlifetime = 1440 - how much time a user can be idle before a session is expired.
upload_max_filesize = 2M - the maximum size of the file that can be uploaded to the webserver.
post_max_size = 8M - the maximum value that the post variable should hold.
allow_url_fopen = On -ย  assists in opening url objects (such as files).
max_execution_time = 30 the maximum time a script can run before being terminated (in seconds).

The above list consists of common settings used with PHP in conjunction with Apache and MySQL. You might find, however, that there are other settings you need to alter to make things work a bit more smoothly. That will depend on your needs and the software you have installed on your LAMP stack.

Once you have everything in place, you can test PHP by creating an info.php file with the command:

sudo nano /var/www/html/info.php

In that file, paste the following contents:

<?php
phpinfo();
?>

Save and close the file. Point a web browser to http://SERVER/info.php (Where SERVER is the IP address or the domain of the hosting server). You should then see a page listing out all of the information about your PHP installation.

Conclusion

And there you have it, the installation and configuration of PHP for Apache and MySQL. Once you have this stack up and running, it's ready for you to hand it over to the PHP developers in your company or those you've hired from a third-party development firm to work their magic.

Tags

Join Hacker Noon

Create your free account to unlock your custom reading experience.