Preparing the System
Adding Required Repositories
To install PHP 8.1 on Rocky Linux 9, you need to add the EPEL (Extra Packages for Enterprise Linux) and REMI repositories. These repositories provide extra packages and updates not found in the default Rocky Linux repositories.
EPEL Repository
Install the EPEL repository:
sudo dnf install epel-release
Benefits of EPEL:
- Access to more software packages
- Regular updates and security patches
- Community-maintained repository with good packages
Tip: Verify EPEL Repository
After installing the EPEL repository, you can verify its presence by running:
dnf repolist | grep epel
This command will display the EPEL repository if it's correctly installed.
REMI Repository
Install the REMI repository:
sudo dnf install http://rpms.remirepo.net/enterprise/remi-release-9.rpm
Installing DNF Utilities
DNF utilities offer extra functions for managing packages and repositories. To install the dnf-utils package, run:
sudo dnf install dnf-utils
Configuring PHP 8.1
Enabling PHP 8.1 Remi Repository
To enable the PHP 8.1 Remi repository on Rocky Linux 9:
-
Reset the PHP module:
sudo dnf module reset php
-
Install PHP 8.1 module:
sudo dnf module install php:remi-8.1
Installing PHP 8.1 and Extensions
Install PHP 8.1 and common extensions:
sudo dnf install php php-cli php-fpm php-mysqlnd php-zip php-devel php-gd php-mcrypt php-mbstring php-curl php-xml php-pear php-bcmath php-json
Popular PHP 8.1 extensions and their uses
Extension | Use Case |
---|---|
php-mysqlnd | MySQL database connection |
php-zip | ZIP file handling |
php-gd | Image processing |
php-mbstring | Multibyte string handling |
php-curl | Making HTTP requests |
Configuring PHP-FPM
-
Start PHP-FPM service:
sudo systemctl start php-fpm
-
Enable PHP-FPM to start on boot:
sudo systemctl enable php-fpm
PHP-FPM configuration tips
- Adjust
pm.max_children
based on server resources - Set proper
memory_limit
in php.ini - Configure
upload_max_filesize
for file uploads
Optimizing PHP 8.1 for Production
Performance tuning
- Enable OPcache
- Adjust
memory_limit
andmax_execution_time
- Configure PHP-FPM process manager
Security considerations
- Disable expose_php in php.ini
- Set correct file permissions
- Keep PHP and extensions updated
Installing PHP 8.1
Core Installation
To install PHP 8.1 on your Rocky Linux 9 web server, run this command:
sudo dnf install php
This command installs the main PHP 8.1 package and its dependencies.
Tip: Check PHP Repository
Before installing PHP, make sure you have the correct repository enabled. You can check available PHP versions with:
sudo dnf module list php
If PHP 8.1 is not listed, you may need to enable the Remi repository:
sudo dnf install epel-release
sudo dnf install https://rpms.remirepo.net/enterprise/remi-release-9.rpm
sudo dnf module enable php:remi-8.1
Verifying the Installation
After installation, check the PHP version:
php -v
You should see output like:
PHP 8.1.x (cli) (built: ...) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.x, Copyright (c) Zend Technologies
PHP Configuration
The main PHP config file is at /etc/php.ini
. Here are some settings you might want to change:
Memory Limit
memory_limit = 256M
Maximum Upload File Size
upload_max_filesize = 20M
post_max_size = 20M
Error Reporting
error_reporting = E_ALL
display_errors = Off
log_errors = On
error_log = /var/log/php_errors.log
PHP-FPM Configuration
If you're using PHP-FPM (FastCGI Process Manager), you can install it with:
sudo dnf install php-fpm
Start and enable PHP-FPM:
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
Testing PHP Installation
Creating a PHP Info File
To test your PHP installation, create a PHP info file:
-
Go to the web root directory:
cd /var/www/html
-
Create and edit the info.php file:
sudo nano info.php
-
Add the phpinfo() function to the file:
<?php phpinfo(); ?>
-
Save the file and exit the editor (in nano, press Ctrl+X, then Y, then Enter).
Tip: Backup Before Editing
Before creating or editing any system files, it's a good practice to create a backup. You can use the following command to make a copy of an existing file:
sudo cp /var/www/html/info.php /var/www/html/info.php.bak
This creates a backup file named info.php.bak, allowing you to revert changes if needed.
Accessing the PHP Info Page
To view your PHP configuration through a web browser:
-
Open a web browser on your local machine.
-
Enter this URL, replacing
your_server_ip
with your server's IP address or domain name:http://your_server_ip/info.php
-
You should see a page showing details about your PHP installation.
Key Information Displayed
The PHP info page provides data about your setup:
- PHP version
- Loaded modules
- Server environment
- PHP settings
Examples
- Version Check: A developer needs to check if the server supports PHP 8.1 features like enums.
- Module Verification: A project needs the GD library for image processing.
- Environment Details: Troubleshooting session issues by checking the session save path.
- Configuration Review: Adjusting
max_execution_time
for a long-running script.
Security Precautions
After testing, remove the info.php file:
sudo rm /var/www/html/info.php
This step prevents unauthorized access to your server configuration details.
Finalizing the Installation
Restarting the Web Server
After installing PHP 8.1 and its modules, restart the Apache web server. This step applies the changes and makes PHP work correctly with Apache.
Restart Commands
Use one of these commands to restart the Apache web server:
Command | Description |
---|---|
sudo systemctl restart httpd |
Modern command using systemd |
sudo service httpd restart |
Traditional command for older systems |
Both commands restart Apache, but systemctl
is more modern and offers more options.
Tip: Using systemctl for Service Management
When using systemctl, you can perform various operations on services:
- Start a service:
sudo systemctl start httpd
- Stop a service:
sudo systemctl stop httpd
- Restart a service:
sudo systemctl restart httpd
- Check service status:
sudo systemctl status httpd
These commands provide a consistent interface for managing system services.
Checking Apache Status
After restarting, check if Apache is running:
sudo systemctl status httpd
This command shows the status of the Apache service. Look for "active (running)" in the output.
Troubleshooting
If you see errors, check the Apache error log:
sudo tail -f /var/log/httpd/error_log
This command displays the last few lines of the error log and updates in real-time.
Common Issues and Solutions
- Permission Errors: Check file permissions for your web files.
- Module Conflicts: Check if new modules conflict with existing ones.
- Port Conflicts: Check if another service is using port 80 or 443.
Enabling Apache on Boot
To make Apache start when your server boots up, run:
sudo systemctl enable httpd
Testing PHP Installation
After restarting Apache, test your PHP 8.1 installation:
-
Create a test file:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/phpinfo.php
-
Access the file in your web browser:
http://your_server_ip/phpinfo.php
-
Review the PHP configuration information displayed.
Next Steps
- Set up virtual hosts for your websites.
- Install PHP extensions for your projects.
- Configure PHP settings in
php.ini
for good performance. - Set up a database server if needed (e.g., MySQL, PostgreSQL).
Your PHP 8.1 installation on Rocky Linux 9 is now ready. You can start developing PHP applications or install content management systems that use PHP.