If you are looking to host multiple laravel projects with different versions of PHP on a single Nginx DigitalOcean Droplet, then in this article I’ll guide you step-by-step on how to install multiple PHP version with LEMP Stack on Ubuntu.
Step 1: the official Ubuntu repositories may not have the old version of PHP or more up-to-date versions of PHP, therefore you will first need to add the ondrej/php
repository to your system.
sudo apt update sudo add-apt-repository ppa:ondrej/php
Step 2: Update the repository
sudo apt update
Step 3: Next Install Different versions of PHP
To Install PHP 5.7 sudo apt-get install php5.7-fpm php5.7-mysql php5.7-mbstring To Install PHP 7.0 sudo apt-get install php7.0-fpm php7.0-mysql php7.0-mbstring To Install PHP 7.1 sudo apt-get install php7.1-fpm php7.1-mysql php7.1-mbstring To Install PHP 7.2 sudo apt-get install php7.2-fpm php7.2-mysql php7.2-mbstring
Step 4: After Installing Different PHP version, start the PHP FPM service for specific version
To Start PHP 5.7 sudo systemctl start php5.7-fpm To Start PHP 7.0 sudo systemctl start php7.0-fpm To Start PHP 7.1 sudo systemctl start php7.1-fpm
Step 5: Next, verify the status of the PHP FPM service:
sudo systemctl status php5.7-fpm sudo systemctl status php7.0-fpm sudo systemctl status php7.1-fpm
Step 6: Important configuration changes that must be applied for specific version of PHP
here is how to open specific version of PHP configuration file for PHP-FPM
sudo nano /etc/php/5.7/fpm/php.ini sudo nano /etc/php/7.0/fpm/php.ini sudo nano /etc/php/7.1/fpm/php.ini
here is how to open specific version of PHP configuration file for PHP-CLI
sudo nano /etc/php/5.7/cli/php.ini sudo nano /etc/php/7.0/cli/php.ini sudo nano /etc/php/7.1/cli/php.ini
You must first open the php.ini
configuration file, first decide whether you want to edit the PHP-FPM ini file or PHP-CLI ini file depending on whether you want to make configuration changes to web server PHP or command line PHP.
Once it is open, use Ctrl+W
and now type in cgi.fix_pathinfo=
and click enter, This will take you to the right line. You will see a semicolon the left of this line. Delete the semi colon and then change the 1 into a 0 and save the file.To save something in Nano, just press Ctrl+X
and type Y
and then press Enter
.
You may also adjust the value in your php.ini file for configurations such asupload_max_filesize
,post_max_size
,max_execution_time
,max_input_time
andmemory_limit
according to your project requirement.
Step 7: To see the changes take effect we need to restart php-fpm
by typing following command in your terminal.
sudo systemctl restart php5.7-fpm sudo systemctl restart php7.0-fpm sudo systemctl restart php7.1-fpm
if you have made changes to PHP-CLI configuration file, you don't need to restart it, since the configuration is only loaded when it is used and then destroyed in Command Line PHP.
At this point you have installed different PHP versions on your server. With most of the configuration completed, it is time to setup the server engine and define which version of the PHP will be used by specific Laravel projects.
Step 8: Nginx server block file is where it comes to play around with PHP Versions.
Nginx Server Blocks allows you to run more than one website on a single machine with different version of PHP and different configurations. With Server Blocks, you can specify the site document root (the directory which contains the website files), create a separate security policy for each site, use different SSL certificates for each site and much more.
A typical Nginx server block file for Laravel projects should looks like this: I have highlighted the code block where you have to play with PHP version.
server { listen 80 default_server; listen [::]:80 default_server; root /var/www/techalyst/public; index index.php index.html index.htm index.nginx-debian.html; server_name techalyst.com www.techalyst.com; location / { try_files $uri $uri/ /index.php?$query_string; } //this is the code block where you will define the version of the PHP to use, in this example we use PHP 7.2 location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.2-fpm.sock; } location ~ /\.ht { deny all; } }
This article is not meant for introducing on How to create multiple nginx server block file for hosting multiple laravel project in same droplet on DigitalOcean. You can read my following useful articles to do that.
- Host Multiple Laravel Project in Same Droplet on Digital Ocean | Deploy Multiple Laravel Apps
- Laravel Hosting with Digital Ocean Droplet Step By Step Tutorial
So far everything is good, if you output phpinfo()
in different project root and access it via browser, you will see the exact PHP version you have configured in the server block file. But you will see strange result if you try to check the PHP version in the command line. If you try to execute composer command, such as installing laravel projects, it may fail with PHP version error, don't worry !.
If you want to execute php in command line or execute composer commands based on specific php versions, then you have to do like this.
Example 1: Let's install Laravel 5.7 which has minimum PHP Version requirement of php 7.1
/usr/bin/php7.1 /usr/local/bin/composer install --optimize-autoloader --no-dev
Example 2: Let's install Laravel 8 which has minimum PHP version requirement of PHP 7.3
/usr/bin/php7.3 /usr/local/bin/composer install --optimize-autoloader --no-dev
If you want to execute Laravel artisan commands on different PHP versions, here is how to do it:
sudo /usr/bin/php7.1 artisan config:cache sudo /usr/bin/php7.1 artisan route:cache sudo /usr/bin/php7.1 artisan key:generate sudo /usr/bin/php7.1 artisan migrate sudo /usr/bin/php7.1 artisan storage:link sudo /usr/bin/php7.3 artisan config:cache sudo /usr/bin/php7.3 artisan route:cache sudo /usr/bin/php7.3 artisan key:generate sudo /usr/bin/php7.3 artisan migrate sudo /usr/bin/php7.3 artisan storage:link
If you have any other questions, experience or insights on "Running Multiple PHP Version with Composer Command line, Nginx on DigitalOcean Droplet" please feel free to leave your thoughts in the comments bellow, Don't forget to share the posts.
Be the first one to write a response :(
{{ reply.member.name }} - {{ reply.created_at_human_readable }}