The best training: How to Install Apache with PHP-FPM on Ubuntu

Today, to run PHP codes on personal servers and clouds, installing PHP and Apache is the first step that must be done correctly. As you know, Apache is the most widely used and famous web server that is used on servers. There are different ways to install and connect PHP and Apache, we recommend using PHP-FPM.
In the following, we will provide a step-by-step guide on how to install Apache with PHP-FPM on Ubuntu.

What is PHP used for? #

PHP is widely used to develop interactive and dynamic websites. It is also the last layer of the LAMP stack (Linux, Apache, MySQL, PHP), popular open source components for web development. It runs on a browser and is embedded in HTML pages to add functionality to a website without the need of calling external files. PHP code is first executed on the server, and the result is finally rendered on a web browser.

Popular sites that run PHP include Facebook, Pinterest, Wikipedia, WordPress, Slack, and many others. Currently, PHP 8.2 is the latest release. It ships with numerous features and improvements. Check out the release page for a comprehensive list of new features and enhancements.

 

1: Update your Ubuntu #

Before installing any new software, you should update your operating system to ensure that all packages are up to date. You can do this by running the following command:

Code
sudo apt update && sudo apt upgrade

2: Install Apache #

Do you want to install Apache on Ubuntu with a simple command? It’s simple, run the following command in the terminal.

Code
sudo apt install apache2

After the installation is complete, start the Apache service by running the following command:

Code
sudo systemctl start apache2

Run the command below, so that when your server is rebooted, Apache will automatically start again when the operating system comes up.

Code
sudo systemctl enable apache2

 

3: Install PHP-FPM #

With the necessary packages, we’re ready to integrate Ondřej Surý’s PHP PPA into our system. We’ll use this PHP repository to install the latest PHP versions, often more recent than those available in Ubuntu’s default APT repository.

1. Let’s go ahead and import the repository with the following command

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

2. Now, with the following command, we install PHP and PHP-FPM at the same time with one command

Code
sudo apt install php8.2 php8.2-fpm

Note: Replace “8.2” with your desired PHP versions (Like: 8.1, 7.4, etc).

3. Once the installation is complete, you can start the PHP-FPM service by running the following command:

Code
sudo systemctl start php8.2-fpm

4. Now run this command to Start PHP-FPM at boot time:

Code
sudo systemctl enable php8.2-fpm

 

4: Setup Apache to use PHP-FPM #

  1. To communicate between Apache and php-fpm you need to enable the “proxy_fcgi” and “proxy” modules. So run the following command:
Code
sudo a2enmod proxy_fcgi proxy

2. Create a new configuration file for your virtual host using the following command:

Code
sudo nano /etc/apache2/sites-available/example.com.conf

Note: Replace “example.com” with your domain name.

3. In the created file, copy and paste the following content:

Code
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/html

<Directory /var/www/html>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>

<FilesMatch \.php$>
SetHandler “proxy:unix:/run/php/php8.2-fpm.sock|fcgi://localhost/”
</FilesMatch>

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Note: maybe need to replace “ with ” in the above configuration file.

Note: Adjust the “ServerName”, “ServerAlias”, and “DocumentRoot” directives, and PHP FPM socket path to match your domain and desired file path.

4. Now Save and close the file.

5. Enable the new virtual host by running the following command:

Code
sudo a2ensite example.com.conf

6. Reload the Apache web server to apply the changes:

Code
sudo systemctl reload apache2

 

To test the Apache with PHP-FPM configuration, create a new PHP file in the document root directory of your website:

Code
sudo nano /var/www/html/test.php

Add the following code into the file:

Code
<?php phpinfo(); ?>

Save the file and exit the editor. Then go to “http://example.com/test.php”. If everything is configured correctly, you should see the PHP information page.

Check PHP version with phpinfo
Check PHP version with phpinfo

At this point, you have successfully installed Apache2 with php8.2-fpm. Note that the path of the php.ini is ‘/etc/php/8.2/fpm/php.ini’.

If you need, we can increase the memory limit and the maximum file size for uploads as well as other values by editing:

Code
nano /etc/php/8.2/fpm/php.ini

+ memory_limit = 256M
+ upload_max_filesize = 16M
+ post_max_size = 16M

Note: The values above are examples, the required value is determined depending on the server capacity and your needs.

To apply the changes, restart php8.2-fpm:

Code
/etc/init.d/php8.2-fpm restart

5: Install a TLS Certificate from Let’s Encrypt #

To secure your server via a TLS certificate, simply install Certbot:

Code
apt-get install certbot python3-certbot-apache

Next, generate and install the TLS certificate by running the following command and following the steps provided.

Code
certbot –apache -d yourDomain.com,www.yourAlais.com

Powered by BetterDocs