
PHP is one of the most popular web languages. Common uses include server-side scripting and automation. This guide documents the installation and configuration of PHP 8.1 FastCGI Process Manager (FPM) on a Ubuntu 20.04 server running an Nginx or Apache webserver.
demo.example.com in this document)To support the installation of PHP 8.1 and ensure the most up-to-date version, add the main repository supported by one of the Ubuntu developers.
If you plan on using Nginx as your web server, add the Nginx specific repository:
If you plan on using Apache as your web server, add the Apache specific repository:
After adding the repositories, update the local apt sources and update any required files:
Install Nginx by running:
Install Apache by running:
Install PHP and various common extensions by running:
To support PHP, install unzip and composer. Composer is an open-source PHP dependency manager.
Ubuntu 20.04 comes with UFW installed, which is a local firewall that prevents web server traffic. Allow HTTP and HTTPS traffic by running:
Nginx and Apache both have a default page. Open a browser and visit http://demo.example.com/ and ensure the server is running and serving HTML pages.
Most web servers have multiple sites running on them. To secure the server effectively, every site should have its own user and group, which also helps troubleshoot and track problems. This document uses demo.example.com as the address, so create demoweb as the webserver user. The following commands add a group, add a user, and then assign the user to the group and make the group's home directory the default website directory for both Nginx and Apache.
Create a backup copy of the default PHP-FPM configuration and then rename the original file to associate it with the web user:
Change the associated user and socket associated with the pool by editing the new configuration file:
Change four lines:
[www] to [demoweb]user = www-data to user = demowebgroup = www-data to group = demoweblisten = /run/php/php8.1-fpm.sock to listen = 127.0.0.1:9000Save the file and restart the PHP-FPM service:
This section covers the configuration of Nginx. If you are using Apache, skip ahead to section 5.
To help secure Nginx, add a snippets.d directory with more configurations that the webserver accesses:
After creating the directory, create supplemental files to secure content. Each file represents a file type or extension it blocks.
Create a file to deny .git files:
Place the following snippet in this file:
Create a file preventing composer cache, JSON, and lock files:
Place the following snippets in this file:
Create a file to deny .htaccess files:
Place the following snippet in this file:
Create a file to deny .env files:
Create a file to deny license and readme files:
Place the following snippets in this file:
Create a file that adds secure headers to every request.
Place the following three lines in this file:
Remove the default site configuration:
Create a new site:
Add the following to the file (make sure to change demo.example.com to match your DNS entry):
NOTE: You could use one line include snippets.d/*.conf. However, that allows for a malicious configuration file injected and loaded erroneously. Instead, save the file and then link it to the active file:
After saving the supplemental files and making the site configuration changes, check the Nginx configuration by running:
If there are no errors, Nginx returns:
At this point, restart Nginx by running service nginx restart.
Remove the default HTML file:
Create a test file
Put the following in the file:
Visit http://demo.example.com/. The PHP Version Information page should display. Search for the Environment section and ensure demoweb is the associated user.
Enable the FPM and proxy binaries for PHP:
Remove the default site configuration:
Create a new site:
Add the following to the file (make sure to change demo.example.com to match your DNS entry):
Link it to the active file:
After saving the supplemental files and making the site configuration changes, check the Apache configuration by running:
If there are no errors, Apache returns:
At this point, restart Apache by running service apache2 restart.
Remove the default HTML file:
Create a test file:
Put the following in the file:
Visit http://demo.example.com/. The PHP Version Information page should display. Search for the Environment section and ensure demoweb is the associated user.
Install Certbot using snap:
Run certbot, following the prompts, to secure the webserver. Certbot requests an SSL certificate and modifies the configuration, sending all traffic to the secure site.
PHP is a powerful web scripting and command-line programming language. Adding composer further extends the functionality of PHP, granting access to multiple libraries, taking your applications to the next level.
0 Comments
Be the first to comment and share your perspective with the community.