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-mysql
Verify the installation:
nginx -v
php -v
2. Configure Virtual Host for Yii2 Basic Template
Assume our project is located at:
/var/www/dynamic-duniya-basic
Step 1: Create an Nginx Configuration File
Create a new configuration file for the Yii2 Basic template:
sudo nano /etc/nginx/sites-available/dynamic-duniya-basic
Add 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 nginx
Step 4: Update /etc/hosts
File
Edit your hosts file:
sudo nano /etc/hosts
Add:
127.0.0.1 yii2basic.local
Now, 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-advanced
Step 1: Create an Nginx Configuration File
sudo nano /etc/nginx/sites-available/dynamic-duniya-advanced
Add 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 nginx
Step 4: Update /etc/hosts
File
Edit your hosts file:
sudo nano /etc/hosts
Add:
127.0.0.1 yii2frontend.local
127.0.0.1 yii2backend.local
Now, you can access your Yii2 Advanced application at:
- Frontend: http://yii2frontend.local
- Backend: http://yii2backend.local
Random Blogs
- Top 10 Blogs of Digital Marketing you Must Follow
- Top 15 Recommended SEO Tools
- Convert RBG Image to Gray Scale Image Using CV2
- Understanding OLTP vs OLAP Databases: How SQL Handles Query Optimization
- Datasets for Exploratory Data Analysis for Beginners
- Generative AI - The Future of Artificial Intelligence
- Best Platform to Learn Digital Marketing in Free
- Role of Digital Marketing Services to Uplift Online business of Company and Beat Its Competitors
- The Ultimate Guide to Starting a Career in Computer Vision
- What is YII? and How to Install it?
Prepare for Interview
Datasets for Machine Learning
- 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