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:
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.
After the installation is complete, start the Apache service by running the following command:
Run the command below, so that when your server is rebooted, Apache will automatically start again when the operating system comes up.
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
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
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:
4. Now run this command to Start PHP-FPM at boot time:
4: Setup Apache to use PHP-FPM #
- To communicate between Apache and php-fpm you need to enable the “proxy_fcgi” and “proxy” modules. So run the following command:
2. Create a new configuration file for your virtual host using the following command:
Note: Replace “example.com” with your domain name.
3. In the created file, copy and paste the following content:
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:
6. Reload the Apache web server to apply the changes:
To test the Apache with PHP-FPM configuration, create a new PHP file in the document root directory of your website:
Add the following code into the file:
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.
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:
+ 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:
5: Install a TLS Certificate from Let’s Encrypt #
To secure your server via a TLS certificate, simply install Certbot:
Next, generate and install the TLS certificate by running the following command and following the steps provided.