In today’s rapidly evolving digital landscape, having a robust, efficient, and easy-to-use web server is crucial for developers and businesses alike. The Caddy web server, known for its simplicity and zero-configuration setup, combined with the power of PHP 8.1, offers a compelling solution for modern web development needs. This guide will walk you through installing Caddy with PHP 8.1 on AlmaLinux 9, providing step-by-step instructions that newcomers can follow with ease, while also offering depth for more experienced users.
Step 1: System Update
Always start with an up-to-date system to ensure compatibility and security. Open your terminal and execute the following command:
sudo dnf update -y
This command updates all your system’s packages to their latest versions.
Step 2: Install Caddy
Caddy is not available in the default AlmaLinux repositories, but installing it is straightforward thanks to the official Caddy script. Run the following command to download and install Caddy:
sudo dnf install -y 'dnf-command(copr)'
sudo dnf copr enable @caddy/caddy
sudo dnf install caddy -y
This set of commands enables the Caddy repository and installs the Caddy web server.
Step 3: Verify Caddy Installation
To ensure Caddy is installed correctly and running, use the command:
sudo systemctl enable --now caddy
Then, verify the status of the Caddy service:
sudo systemctl status caddy
If everything is set up correctly, you’ll see an active (running) status.
Step 4: Install PHP 8.1
PHP 8.1 introduces new features and performance improvements. To install PHP 8.1 on AlmaLinux 9, execute:
sudo dnf module reset php
sudo dnf module enable php:8.1
sudo dnf install php php-fpm php-cli php-common -y
These commands reset the PHP module, enable PHP 8.1, and install PHP along with the most common extensions.
Step 5: Configure PHP-FPM
PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size. To configure PHP-FPM to work with Caddy:
- Start and enable PHP-FPM:
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
- Configure PHP-FPM to listen on a Unix socket:
Edit the PHP-FPM pool configuration file. For PHP 8.1, it’s typically named www.conf. You can find it in /etc/php-fpm.d/.
Change the listen line to use a Unix socket:
listen = /run/php-fpm/www.sock
- Restart PHP-FPM to apply the changes:
sudo systemctl restart php-fpm
Step 6: Configure Caddy to Use PHP
Create a new Caddyfile or edit the existing one in /etc/caddy/Caddyfile and add the following configuration to serve PHP files:
your_domain.com {
root * /var/www/your_domain
php_fastcgi unix//run/php-fpm/www.sock
file_server
}
Replace your_domain.com with your actual domain and /var/www/your_domain with the path to your website’s root directory.
Step 7: Restart Caddy
Apply the changes by restarting Caddy:
sudo systemctl restart caddy
Integrating with Shape.host Services
After successfully setting up Caddy with PHP 8.1 on AlmaLinux 9, optimizing your web server for performance and security is crucial. Shape.host offers Linux SSD VPS services that provide the perfect infrastructure for deploying web applications powered by Caddy and PHP. With Shape.host, you can enjoy blazing-fast SSDs, robust security features, and scalable resources to meet the demands of your growing website or application. Whether you’re hosting a small blog, an eCommerce site, or a large-scale enterprise application, Shape.host’s Linux SSD VPS solutions ensure your web presence is fast, secure, and reliably online.