How to Use PHP Scripts with Apache on AlmaLinux
Categories:
PHP (Hypertext Preprocessor) is one of the most popular server-side scripting languages for building dynamic web applications. Its ease of use, extensive library support, and ability to integrate with various databases make it a preferred choice for developers. Pairing PHP with Apache on AlmaLinux creates a robust environment for hosting websites and applications.
In this detailed guide, we’ll walk you through the steps to set up Apache and PHP on AlmaLinux, configure PHP scripts, and optimize your environment for development or production.
Why Use PHP with Apache on AlmaLinux?
The combination of PHP, Apache, and AlmaLinux offers several advantages:
- Enterprise Stability: AlmaLinux is a free, open-source, enterprise-grade Linux distribution.
- Ease of Integration: Apache and PHP are designed to work seamlessly together.
- Versatility: PHP supports a wide range of use cases, from simple scripts to complex content management systems like WordPress.
- Scalability: PHP can handle everything from small personal projects to large-scale applications.
Prerequisites
Before you begin, ensure you have the following:
A Server Running AlmaLinux
With root orsudo
access.Apache Installed and Running
If Apache is not installed, you can set it up using:sudo dnf install httpd -y sudo systemctl start httpd sudo systemctl enable httpd
PHP Installed
We’ll cover PHP installation in the steps below.Basic Command-Line Knowledge
Familiarity with Linux commands and text editors likenano
orvim
.
Step 1: Install PHP on AlmaLinux
Enable the EPEL and Remi Repositories
AlmaLinux’s default repositories may not have the latest PHP version. Install theepel-release
andremi-release
repositories:sudo dnf install epel-release -y sudo dnf install https://rpms.remirepo.net/enterprise/remi-release-8.rpm -y
Select and Enable the Desired PHP Version
Usednf
to list available PHP versions:sudo dnf module list php
Enable the desired version (e.g., PHP 8.1):
sudo dnf module reset php -y sudo dnf module enable php:8.1 -y
Install PHP and Common Extensions
Install PHP along with commonly used extensions:sudo dnf install php php-mysqlnd php-cli php-common php-opcache php-gd php-curl php-zip php-mbstring php-xml -y
Verify the PHP Installation
Check the installed PHP version:php -v
Step 2: Configure Apache to Use PHP
Ensure PHP is Loaded in Apache
Themod_php
module should load PHP within Apache automatically. Verify this by checking the Apache configuration:httpd -M | grep php
If
php_module
is listed, PHP is properly loaded.Edit Apache’s Configuration File (Optional)
In most cases, PHP will work out of the box with Apache. However, to manually ensure proper configuration, edit the Apache configuration:sudo nano /etc/httpd/conf/httpd.conf
Add the following directives to handle PHP files:
<FilesMatch \.php$> SetHandler application/x-httpd-php </FilesMatch>
Restart Apache
Apply the changes by restarting the Apache service:sudo systemctl restart httpd
Step 3: Test PHP with Apache
Create a Test PHP File
Place a simple PHP script in the Apache document root:sudo nano /var/www/html/info.php
Add the following content:
<?php phpinfo(); ?>
Access the Test Script in a Browser
Open your browser and navigate to:http://<your-server-ip>/info.php
You should see a page displaying detailed PHP configuration information, confirming that PHP is working with Apache.
Remove the Test File
For security reasons, delete the test file once you’ve verified PHP is working:sudo rm /var/www/html/info.php
Step 4: Configure PHP Settings
PHP’s behavior can be customized by editing the php.ini
configuration file.
Locate the PHP Configuration File
Identify the activephp.ini
file:php --ini
Typically, it’s located at
/etc/php.ini
.Edit PHP Settings
Open the file for editing:sudo nano /etc/php.ini
Common settings to adjust include:
Memory Limit:
Increase for resource-intensive applications:memory_limit = 256M
Max Upload File Size:
Allow larger file uploads:upload_max_filesize = 50M
Max Execution Time:
Prevent scripts from timing out prematurely:max_execution_time = 300
Restart Apache
Restart Apache to apply the changes:sudo systemctl restart httpd
Step 5: Deploy PHP Scripts
With PHP and Apache configured, you can now deploy your PHP applications or scripts.
Place Your Files in the Document Root
By default, the Apache document root is/var/www/html
. Upload your PHP scripts or applications to this directory:sudo cp -r /path/to/your/php-app /var/www/html/
Set Proper Permissions
Ensure theapache
user owns the files:sudo chown -R apache:apache /var/www/html/php-app sudo chmod -R 755 /var/www/html/php-app
Access the Application
Navigate to the application URL:http://<your-server-ip>/php-app
Step 6: Secure Your PHP and Apache Setup
Disable Directory Listing
Prevent users from viewing the contents of directories by editing Apache’s configuration:sudo nano /etc/httpd/conf/httpd.conf
Add or modify the
Options
directive:<Directory /var/www/html> Options -Indexes </Directory>
Restart Apache:
sudo systemctl restart httpd
Limit PHP Information Exposure
Prevent sensitive information from being displayed by disablingexpose_php
inphp.ini
:expose_php = Off
Set File Permissions Carefully
Ensure only authorized users can modify PHP scripts and configuration files.Use HTTPS
Secure your server with SSL/TLS encryption. Install and configure a Let’s Encrypt SSL certificate:sudo dnf install certbot python3-certbot-apache -y sudo certbot --apache
Keep PHP and Apache Updated
Regularly update your packages to patch vulnerabilities:sudo dnf update -y
Step 7: Troubleshooting Common Issues
PHP Script Downloads Instead of Executing
Ensure
php_module
is loaded:httpd -M | grep php
Verify the
SetHandler
directive is configured for.php
files.
500 Internal Server Error
Check the Apache error log for details:
sudo tail -f /var/log/httpd/error_log
Ensure proper file permissions and ownership.
Changes in
php.ini
Not Reflected
Restart Apache after modifyingphp.ini
:sudo systemctl restart httpd
Conclusion
Using PHP scripts with Apache on AlmaLinux is a straightforward and efficient way to create dynamic web applications. With its powerful scripting capabilities and compatibility with various databases, PHP remains a vital tool for developers.
By following this guide, you’ve configured Apache and PHP, deployed your first scripts, and implemented key security measures. Whether you’re building a simple contact form, a blog, or a complex web application, your server is now ready to handle PHP-based projects. Happy coding!