Create Virtual Host for Nginx on Ubuntu (For Yii2 Basic & Advanced Templates)
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.
1. Install Nginx and PHP (If Not Installed)
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 -v2. Configure Virtual Host for Yii2 Basic Template
Assume our project is located at:
/var/www/dynamic-duniya-basicStep 1: Create an Nginx Configuration File
Create 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;
}
}Step 2: Enable the Virtual Host
sudo ln -s /etc/nginx/sites-available/dynamic-duniya-basic /etc/nginx/sites-enabled/Step 3: Restart Nginx
sudo systemctl restart nginxStep 4: Update /etc/hosts File
Edit your hosts file:
sudo nano /etc/hostsAdd:
127.0.0.1 yii2basic.localNow, visit http://yii2basic.local in your browser.
3. Configure Virtual Host for Yii2 Advanced Template
Assume our Advanced Template is located at:
/var/www/dynamic-duniya-advancedStep 1: Create an Nginx Configuration File
sudo 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;
}
}Step 2: Enable the Virtual Host
sudo ln -s /etc/nginx/sites-available/dynamic-duniya-advanced /etc/nginx/sites-enabled/Step 3: Restart Nginx
sudo systemctl restart nginxStep 4: Update /etc/hosts File
Edit 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:
- Frontend: http://yii2frontend.local
- Backend: http://yii2backend.local
Random Blogs
- Big Data: The Future of Data-Driven Decision Making
- Understanding LLMs (Large Language Models): The Ultimate Guide for 2025
- The Ultimate Guide to Data Science: Everything You Need to Know
- Understanding Data Lake, Data Warehouse, Data Mart, and Data Lakehouse – And Why We Need Them
- String Operations in Python
- How to Become a Good Data Scientist ?
- Top 10 Blogs of Digital Marketing you Must Follow
- Government Datasets from 50 Countries for Machine Learning Training
- Top 15 Recommended SEO Tools
- What is YII? and How to Install it?
Prepare for Interview
- JavaScript Interview Questions for 5+ Years Experience
- JavaScript Interview Questions for 2–5 Years Experience
- JavaScript Interview Questions for 1–2 Years Experience
- JavaScript Interview Questions for 0–1 Year Experience
- JavaScript Interview Questions For Fresher
- SQL Interview Questions for 5+ Years Experience
- SQL Interview Questions for 2–5 Years Experience
- SQL Interview Questions for 1–2 Years Experience
- SQL Interview Questions for 0–1 Year Experience
- SQL Interview Questions for Freshers
- Design Patterns in Python
Datasets for Machine Learning
- Awesome-ChatGPT-Prompts
- Amazon Product Reviews Dataset
- Ozone Level Detection Dataset
- Bank Transaction Fraud Detection
- YouTube Trending Video Dataset (updated daily)
- Covid-19 Case Surveillance Public Use Dataset
- US Election 2020
- Forest Fires Dataset
- Mobile Robots Dataset
- Safety Helmet Detection
- All Space Missions from 1957
- OSIC Pulmonary Fibrosis Progression Dataset
- Wine Quality Dataset
- Google Audio Dataset
- Iris flower dataset
- Artificial Characters Dataset
- Bitcoin Heist Ransomware Address Dataset

