Setup nginx + multiple php-fpm on Q4OS(Debian10)
Published: 2022-02-25 12:33:55

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
```

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
```


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
```

```
sudo apt install php7.0 php7.0-fpm php7.0-mysql libapache2-mod-php7.0 libapache2-mod-fcgid -y
```

```
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
```

```
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

```

```
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

```
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
```

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
```

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

```
<?php
phpinfo();

```

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;
	}
}
```

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/
```

Add entry in /etc/hosts file. 

```
127.0.0.1    test.local
```

Save file and test it

```
sudo nginx -t
```

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

Restart nginx to take effect.

```
sudo systemctl restart nginx
```

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

```
http://test.local
```

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
```

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


Hope you like this post.