To install PHP on a Linux VPS, run sudo apt install php php-fpm (Ubuntu/Debian) or sudo dnf install php php-fpm (AlmaLinux/Rocky). The difference between a working installation and a production-ready one comes down to the configuration steps that follow.
This guide covers installing PHP, enabling common extensions, and tuning the most important php.ini settings so your VPS serves PHP applications with solid performance and without obvious security gaps.
Before You Start: Ubuntu/Debian vs. AlmaLinux/Rocky
Commands differ slightly by distro, but the logic is the same. Connect via SSH with a user who has sudo privileges and update packages first:
# Ubuntu / Debian
sudo apt update && sudo apt upgrade -y
# AlmaLinux / Rocky Linux / CentOS Stream
sudo dnf update -y
If you're not sure how to connect via SSH, start with the articles in our VPS servers section first.
Installing PHP and Essential Extensions
Install PHP along with the extensions most commonly required by applications like WordPress, Laravel, or any standard PHP app:
# Ubuntu / Debian
sudo apt install -y php php-fpm php-mysql php-mbstring php-xml \
php-curl php-zip php-gd php-intl php-bcmath php-opcache
# AlmaLinux / Rocky Linux
sudo dnf install -y php php-fpm php-mysqlnd php-mbstring php-xml \
php-curl php-zip php-gd php-intl php-bcmath php-opcache
Check the installed version:
php -v
You should see something like PHP 8.3.x (cli). If you need a specific version (e.g., PHP 8.1 for an older project), see the next section.
Installing a Specific PHP Version (Ubuntu/Debian)
Add the Ondřej Surý PPA, which maintains all active PHP versions:
sudo apt install -y software-properties-common
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
sudo apt install -y php8.1 php8.1-fpm php8.1-mysql php8.1-mbstring php8.1-xml php8.1-curl
Replace 8.1 with the version you need. Multiple versions can coexist and be switched with update-alternatives.
Enabling and Verifying PHP-FPM
PHP-FPM (FastCGI Process Manager) is the recommended mode for serving PHP with Nginx and also with Apache via mod_proxy_fcgi. It manages a pool of PHP worker processes independently of the web server, improving both performance and process isolation.
# Enable and start PHP-FPM
sudo systemctl enable php8.3-fpm
sudo systemctl start php8.3-fpm
# Verify it's running
sudo systemctl status php8.3-fpm
The service name includes the version: php8.3-fpm, php8.1-fpm, etc. On AlmaLinux/Rocky it's simply php-fpm.
Unix Socket vs. TCP
By default, PHP-FPM listens on a Unix socket (/run/php/php8.3-fpm.sock). This is faster than TCP for local communication. You can switch to TCP (127.0.0.1:9000) if your Nginx or Apache config requires it by editing /etc/php/8.3/fpm/pool.d/www.conf.
Configuring php.ini for Production
The php.ini file controls PHP's behavior. There are two copies: one for the CLI (/etc/php/8.3/cli/php.ini) and one for FPM (/etc/php/8.3/fpm/php.ini). The FPM copy is the one that affects your website.
Open the file:
sudo nano /etc/php/8.3/fpm/php.ini
The most important settings for a production server:
| Setting | Recommended value | Why |
|---|---|---|
memory_limit |
256M |
Enough for most apps; raise to 512M for WooCommerce or Magento |
upload_max_filesize |
64M |
Allows uploading reasonably sized images and documents |
post_max_size |
64M |
Must be equal to or greater than upload_max_filesize |
max_execution_time |
60 |
Prevents slow scripts from tying up server resources |
display_errors |
Off |
Never expose errors to end users in production |
log_errors |
On |
Log errors to a file for troubleshooting |
expose_php |
Off |
Hides the PHP version from HTTP response headers |
opcache.enable |
1 |
OPcache speeds up PHP by compiling and caching bytecode in memory |
After saving, reload PHP-FPM for changes to take effect:
sudo systemctl reload php8.3-fpm
Enabling OPcache for Better Performance
OPcache ships with PHP since version 5.5 and is one of the most impactful performance improvements you can make. It compiles PHP scripts once and stores the result in shared memory, eliminating recompilation on every request.
Find or add these lines in php.ini:
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=10000
opcache.revalidate_freq=2
opcache.fast_shutdown=1
To verify OPcache is active, create a temporary info file:
echo "
Visit http://your-ip/info.php in a browser and look for the opcache section. Delete the file immediately after checking — leaving phpinfo() exposed in production is a security risk.
sudo rm /var/www/html/info.php
Want your VPS configured correctly from day one? The team at elenlace.com sets up full VPS server stacks tailored to your project's needs.
Key takeaways
- PHP installs with a single command on Ubuntu/Debian or AlmaLinux; the Ondřej PPA gives you version-specific installs on Debian-based distros.
- PHP-FPM is the recommended execution mode for production; the Unix socket is faster than TCP for local communication.
- Minimum
php.inisettings for production:display_errors Off,expose_php Off, correctmemory_limit, and OPcache enabled. - OPcache can reduce PHP response times by 30–70% without changing a single line of application code.
- Always reload PHP-FPM with
systemctl reloadafter editingphp.ini.
With PHP properly installed and tuned, your VPS is ready to serve applications quickly and securely. If you'd rather hand off the full server configuration, elenlace.com offers managed VPS services so you can stay focused on building.
FAQ
Which PHP version should I install on my VPS?
Install the latest active version that's compatible with your application. As of mid-2025, PHP 8.3 is the main stable release. Avoid end-of-life versions (like PHP 7.4 or 8.0) that no longer receive security patches. Check the support table at php.net/supported-versions.php.
What's the difference between PHP-FPM and mod_php?
mod_php loads PHP inside the Apache worker process, while PHP-FPM runs as a separate process communicating with Apache or Nginx over FastCGI. PHP-FPM uses less memory, allows per-site pool tuning, and is the recommended choice for production environments.
How do I switch PHP versions without reinstalling everything?
On Ubuntu/Debian with the Ondřej PPA, use update-alternatives --config php for the CLI version, and update the socket path in your Nginx/Apache config to point to the desired FPM version. You can run PHP 8.1 and 8.3 side by side and assign each site to the correct version.
Do I need to restart the web server too when I change php.ini?
If you're using PHP-FPM, you only need to reload PHP-FPM (systemctl reload php8.3-fpm). Nginx and Apache keep running without interruption. If you're using mod_php with Apache, you'll also need to reload Apache (systemctl reload apache2).
Useful resources
Other providers and guides worth comparing: