
Learn how to set up a virtual host for Nginx on Ubuntu to efficiently manage multiple Yii2 projects (Basic & Advanced templates). This guide covers configuration, domain setup, and best practices for seamless development.
When working on multiple Yii2 projects, setting up Nginx virtual hosts helps manage different projects efficiently. Below, we will configure virtual hosts for both Basic and Advanced Yii2 templates.
Ensure Nginx and PHP are installed on your Ubuntu system. If not, install them using:
sudo apt update
sudo apt install nginx php-fpm php-mysqlVerify the installation:
nginx -v
php -vAssume our project is located at:
/var/www/dynamic-duniya-basicCreate a new configuration file for the Yii2 Basic template:
sudo nano /etc/nginx/sites-available/dynamic-duniya-basicAdd the following content:
server {
listen 80;
server_name yii2basic.local;
root /var/www/dynamic-duniya-basic/web;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}sudo ln -s /etc/nginx/sites-available/dynamic-duniya-basic /etc/nginx/sites-enabled/sudo systemctl restart nginx/etc/hosts FileEdit your hosts file:
sudo nano /etc/hostsAdd:
127.0.0.1 yii2basic.localNow, visit http://yii2basic.local in your browser.
Assume our Advanced Template is located at:
/var/www/dynamic-duniya-advancedsudo nano /etc/nginx/sites-available/dynamic-duniya-advancedAdd the following content:
server {
listen 80;
server_name yii2frontend.local;
root /var/www/dynamic-duniya-advanced/frontend/web;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
server {
listen 80;
server_name yii2backend.local;
root /var/www/dynamic-duniya-advanced/backend/web;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}sudo ln -s /etc/nginx/sites-available/dynamic-duniya-advanced /etc/nginx/sites-enabled/sudo systemctl restart nginx/etc/hosts FileEdit your hosts file:
sudo nano /etc/hostsAdd:
127.0.0.1 yii2frontend.local
127.0.0.1 yii2backend.localNow, you can access your Yii2 Advanced application at:
Sign in to join the discussion and post comments.
Sign in