DEV Community

Deepak Singh Kushwah
Deepak Singh Kushwah

Posted on

Setup nginx + multiple php-fpm on Q4OS(Debian10)

Hello Developers,

Recentely I have used multiple php versions on single installation. Today we are going to see how we can setup full linux + nginx + php on Q4OS(Debian 10) (I am using Q4OS 3, it's based on debian).

First of all we need to setup php repo which provide php verions...

wget https://packages.sury.org/php/apt.gpg
sudo apt-key add apt.gpg
Enter fullscreen mode Exit fullscreen mode

Now add repo to your source list...

echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php7.list
Enter fullscreen mode Exit fullscreen mode

So first we need to install nginx...
sudo apt install nginx-full -y

Now nignx is installed, we need to setup multiple PHP-FPM verions...

sudo apt install php7.3-fpm php7.3-cli php7.3-curl php7.3-mysql php7.3-xml php7.3-zip php7.3-mbstring php7.3-zip -y
Enter fullscreen mode Exit fullscreen mode
sudo apt install php7.0 php7.0-fpm php7.0-mysql libapache2-mod-php7.0 libapache2-mod-fcgid -y
Enter fullscreen mode Exit fullscreen mode
sudo apt install php7.4-fpm php7.4-cli php7.4-json php7.4-common php7.4-mysql php7.4-zip php7.4-gd php7.4-mbstring php7.4-curl php7.4-xml php7.4-bcmath -y
Enter fullscreen mode Exit fullscreen mode
sudo apt install php8.0-fpm php8.0-cli php8.0-json php8.0-common php8.0-mysql php8.0-zip php8.0-gd php8.0-mbstring php8.0-curl php8.0-xml php8.0-bcmath -y

Enter fullscreen mode Exit fullscreen mode
sudo apt install php8.1-fpm php8.1-cli php8.1-json php8.1-common php8.1-mysql php8.1-zip php8.1-gd php8.1-mbstring php8.1-curl php8.1-xml php8.1-bcmath -y

Enter fullscreen mode Exit fullscreen mode

You can choose other modules for selected php verions as mention above.

Now execute follwing command to check and enable nginx status...

sudo systemctl enable nginx
sudo sustemctl status nginx
Enter fullscreen mode Exit fullscreen mode

Now configure nginx to create new virtual host for your site, I am using test.lcoal as new vhost

First create new directory

mkdir -p /var/www/test.local
Enter fullscreen mode Exit fullscreen mode

Now create index.php in /var/www/test.local dir and add following content in it.

<?php
phpinfo();

Enter fullscreen mode Exit fullscreen mode

Next we create new file /etc/nginx/sites-available/test.local.conf and add following content to it.

server {
    listen 80;
    listen [::]:80;

    server_name mydomain.com www.mydomain.com;
    root /var/www/mydomain.com;
    index index.html index.php;
    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    }
}
Enter fullscreen mode Exit fullscreen mode

Similarly, you can choose any installed php version instead of php7.0-fpm

Next create symlink

sudo ln -s /etc/nginx/sites-available/test.local.conf /etc/nginx/sites-enabled/
Enter fullscreen mode Exit fullscreen mode

Add entry in /etc/hosts file.

127.0.0.1    test.local
Enter fullscreen mode Exit fullscreen mode

Save file and test it

sudo nginx -t
Enter fullscreen mode Exit fullscreen mode

It will show you ok if everything correct in nginx config.

Restart nginx to take effect.

sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

Now it's time to check our virtual host "test.local" in browser.

http://test.local
Enter fullscreen mode Exit fullscreen mode

Something you need to switch PHP on CLI if you are using multiple PHP versions and want to install dependencies (using composer). So to switch PHP, use following command.

sudo update-alternatives --config php
Enter fullscreen mode Exit fullscreen mode

It will show you list of installed php versions, you can pick and press enter to choose default one.

Hope you like this post.

Top comments (1)

Collapse
 
leslieeeee profile image
Leslie

If you are macos user, ServBay.dev is worth to try. You don't need to spend some time or couple of days to setup anything. Just download it and you can use it immediately. You can run multiple PHP versions simultaneously and switch between them effortlessly.
Honestly, this tool has greatly simplified my PHP development and is definitely worth trying!