In the dynamic world of web development, managing different applications with varying PHP version requirements can be a challenge. Fortunately, using Ubuntu, Nginx, and a little know-how, you can establish a single server capable of running multiple PHP versions on single nginx server in ubuntu. This blog delves into the process of creating this versatile environment, empowering you to cater to diverse project needs seamlessly.
Ensure you have an Ubuntu server with root or sudo privileges –Â Â (Initial server setup in ubuntu 22.04 server)
Nginx: Nginx web server should be installed and configured –Â (How to install Nginx, MYSQL and PHP in Ubuntu 22.04)
Unleashing Options: Adding Ondřej's PPA
We’ll utilize Ondrej Personal Package Archive (PPA), a trusted repository offering access to various PHP versions. To add it, run these commands:Â
![host multiple websites](https://focalsoft.ae/wp-content/uploads/2024/03/text-web-hosting-writing-colored-wooden-board-with-simple-icon-blue-background.webp)
sudo add-apt-repository ppa:ondrej/php
sudo apt update
Selecting Your Tools: Installing Desired PHP Versions
Choose the PHP versions you require. Replace X.Y in the following command with the desired version:
Repeat this command for each additional version you wish to install.
sudo apt install phpX.Y-fpm phpX.Y-cli phpX.Y-common phpX.Y-mbstring phpX.Y-xml phpX.Y-json
Customizing Control: Configuring PHP-FPM for Each Version
For each installed version, we need to tailor the configuration:
Copy the configuration file:
sudo cp /etc/php/X.Y/fpm/php-fpm.conf /etc/php/X.Y/fpm/php-fpm-custom.conf
Edit the copied file:
sudo nano /etc/php/X.Y/fpm/php-fpm-custom.conf
Modify the following lines:
Change the socket path to avoid conflicts.
Ensure user and group ownership match your web server configuration.
Adjust pool settings based on your server’s resources and needs.
sudo systemctl restart phpX.Y-fpm.service
Repeat for each version you’ve configured.Â
Creating Individual Homes: Setting Up Virtual Hosts
Now, we need to configure Nginx to handle multiple domains and their respective PHP versions. Create separate virtual host (server block) configuration files:
sudo nano /etc/nginx/sites-available/your_domain_X.Y.conf
Replace your_domain with your actual domain name and X.Y with the PHP version.
Tailoring Nginx Configuration:
Add the following content to the configuration file, replacing placeholders with your details:
server {
listen 80;
server_name your_domain_X.Y;
root /var/www/html/your_domain_X.Y; # Replace with your web directory
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/phpX.Y-fpm.sock; # Update socket path if needed
}
}
Enabling Virtual Hosts and Restarting Nginx:
Finally, activate the virtual host and restart Nginx to implement the changes:
sudo ln -s /etc/nginx/sites-available/your_domain_X.Y.conf /etc/nginx/sites-enabled/your_domain_X.Y.conf
sudo systemctl restart nginx
Repeat steps 5 to 7 for each additional domain requiring a different PHP version.
Verifying Success: Testing the Setup:Â Â Â Â Â
Create a simple PHP file (info.php) with the following code and access it with the corresponding domain:
<?php phpinfo(); ?>
If you see the PHP information page for the configured version, congratulations! You’ve successfully set up your server to host multiple PHP versions.
![host multiple websites](https://focalsoft.ae/wp-content/uploads/2024/03/text-web-hosting-writing-colored-wooden-board-with-simple-icon-blue-background.webp)
In the dynamic world of web development, managing different applications with varying PHP version requirements can be a challenge. Fortunately, using Ubuntu, Nginx, and a little know-how, you can establish a single server capable of running multiple PHP versions on single nginx server in ubuntu. This blog delves into the process of creating this versatile environment, empowering you to cater to diverse project needs seamlessly.
Ensure you have an Ubuntu server with root or sudo privileges. (Initial server setup in ubuntu 22.04 server)
Nginx: Nginx web server should be installed and configured. (How to install Nginx, MYSQL and PHP in Ubuntu 22.04)
Unleashing Options: Adding Ondřej's PPA
We’ll utilize OndÅ™ej’s Personal Package Archive (PPA), a trusted repository offering access to various PHP versions. To add it, run these commands:
sudo add-apt-repository ppa:ondrej/php
sudo apt update
Selecting Your Tools: Installing Desired PHP Versions
Choose the PHP versions you require. Replace X.Y in the following command with the desired version:
Repeat this command for each additional version you wish to install.
sudo apt install phpX.Y-fpm phpX.Y-cli phpX.Y-common phpX.Y-mbstring phpX.Y-xml phpX.Y-json
Customizing Control: Configuring PHP-FPM for Each Version
For each installed version, we need to tailor the configuration:
Copy the configuration file:
sudo cp /etc/php/X.Y/fpm/php-fpm.conf /etc/php/X.Y/fpm/php-fpm-custom.conf
Edit the copied file:
sudo nano /etc/php/X.Y/fpm/php-fpm-custom.conf
Modify the following lines:
Change the socket path to avoid conflicts.
Ensure user and group ownership match your web server configuration.
Adjust pool settings based on your server’s resources and needs.
sudo systemctl restart phpX.Y-fpm.service
Repeat for each version you’ve configured.Â
Creating Individual Homes: Setting Up Virtual Hosts
Now, we need to configure Nginx to handle multiple domains and their respective PHP versions. Create separate virtual host (server block) configuration files:
sudo nano /etc/nginx/sites-available/your_domain_X.Y.conf
Replace your_domain with your actual domain name and X.Y with the PHP version.
Tailoring Nginx Configuration:
Add the following content to the configuration file, replacing placeholders with your details:
server {
listen 80;
server_name your_domain_X.Y;
root /var/www/html/your_domain_X.Y; # Replace with your web directory
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/phpX.Y-fpm.sock; # Update socket path if needed
}
}
Enabling Virtual Hosts and Restarting Nginx:
Finally, activate the virtual host and restart Nginx to implement the changes:
sudo ln -s /etc/nginx/sites-available/your_domain_X.Y.conf /etc/nginx/sites-enabled/your_domain_X.Y.conf
sudo systemctl restart nginx
Repeat steps 5 to 7 for each additional domain requiring a different PHP version.
Verifying Success: Testing the Setup:Â
Create a simple PHP file (info.php) with the following code and access it with the corresponding domain:
<?php phpinfo(); ?>
If you see the PHP information page for the configured version, congratulations! You’ve successfully set up your server to host multiple PHP versions.