How to Use PHP Scripts with Apache on AlmaLinux
Categories:
5 minute read
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 orsudoaccess.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 httpdPHP Installed
We’ll cover PHP installation in the steps below.Basic Command-Line Knowledge
Familiarity with Linux commands and text editors likenanoorvim.
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-releaseandremi-releaserepositories:sudo dnf install epel-release -y sudo dnf install https://rpms.remirepo.net/enterprise/remi-release-8.rpm -ySelect and Enable the Desired PHP Version
Usednfto list available PHP versions:sudo dnf module list phpEnable the desired version (e.g., PHP 8.1):
sudo dnf module reset php -y sudo dnf module enable php:8.1 -yInstall 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 -yVerify the PHP Installation
Check the installed PHP version:php -v
Step 2: Configure Apache to Use PHP
Ensure PHP is Loaded in Apache
Themod_phpmodule should load PHP within Apache automatically. Verify this by checking the Apache configuration:httpd -M | grep phpIf
php_moduleis 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.confAdd 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.phpAdd the following content:
<?php phpinfo(); ?>Access the Test Script in a Browser
Open your browser and navigate to:http://<your-server-ip>/info.phpYou 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.inifile:php --iniTypically, it’s located at
/etc/php.ini.Edit PHP Settings
Open the file for editing:sudo nano /etc/php.iniCommon settings to adjust include:
Memory Limit:
Increase for resource-intensive applications:memory_limit = 256MMax Upload File Size:
Allow larger file uploads:upload_max_filesize = 50MMax 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 theapacheuser owns the files:sudo chown -R apache:apache /var/www/html/php-app sudo chmod -R 755 /var/www/html/php-appAccess 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.confAdd or modify the
Optionsdirective:<Directory /var/www/html> Options -Indexes </Directory>Restart Apache:
sudo systemctl restart httpdLimit PHP Information Exposure
Prevent sensitive information from being displayed by disablingexpose_phpinphp.ini:expose_php = OffSet 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 --apacheKeep 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_moduleis loaded:httpd -M | grep phpVerify the
SetHandlerdirective is configured for.phpfiles.
500 Internal Server Error
Check the Apache error log for details:
sudo tail -f /var/log/httpd/error_logEnsure proper file permissions and ownership.
Changes in
php.iniNot 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!
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.