How to Setup Yii2 Project with Nginx on Ubuntu
ilosrim | 2024/06/07
Pre-requisites
- PHP installed. I have created this document with PHP 7.4 installed on my server
- Yii2 project and know to set it up.
- PHP fpm installed
Steps
- Install Nginx
bash
sudo apt install nginx
sudo apt install nginx
- Clone your project in
/home/username/codes
folder
bash
git clone <repo_url>
git clone <repo_url>
- create Nginx conf files for backend and frontend
File: /etc/nginx/sites-available/backend
bash
server {
listen 80;
server_name <your_backend_url>;
root /home/username/codes/<project_name>/backend/web;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
}
server {
listen 80;
server_name <your_backend_url>;
root /home/username/codes/<project_name>/backend/web;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
}
File: /etc/nginx/sites-available/frontend
bash
server {
listen 80;
server_name <your_frontend_url>;
root /home/username/codes/<project_name>/frontend/web;
index index.php;
add_header 'Access-Control-Allow-Origin' "*";
add_header 'Access-Control-Allow-Methods' "GET,POST,PUT,DELETE,OPTIONS";
add_header 'Access-Control-Allow-Headers' "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With, api-key, auth-key";
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
}
server {
listen 80;
server_name <your_frontend_url>;
root /home/username/codes/<project_name>/frontend/web;
index index.php;
add_header 'Access-Control-Allow-Origin' "*";
add_header 'Access-Control-Allow-Methods' "GET,POST,PUT,DELETE,OPTIONS";
add_header 'Access-Control-Allow-Headers' "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With, api-key, auth-key";
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
}
- Create following symlinks
bash
sudo ln -s /etc/nginx/sites-available/backend /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/frontend /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/backend /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/frontend /etc/nginx/sites-enabled/
- Add ubuntu to www-data group
bash
sudo usermod -a -G www-data username
sudo usermod -a -G www-data username
- Give permissions to backend/assets and frontend/assets folder
bash
sudo chmod -R 777 /home/username/codes/<project_name>/frontend/web/assets/
sudo chmod -R 777 /home/username/codes/<project_name>/backend/web/assets/
sudo chmod -R 777 /home/username/codes/<project_name>/frontend/web/assets/
sudo chmod -R 777 /home/username/codes/<project_name>/backend/web/assets/
- Restart Nginx
bash
sudo service nginx restart
sudo service nginx restart