Remove Yii2 Frontend Web in Apache and Nginx

Advertisement

Frontend Configuration

server {
    listen 80;
    server_name frontend.local;
    root /var/www/yii2-test/frontend/web;

    index index.php;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/run/php/php8.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location ~* /\.(htaccess|svn|git) {
        deny all;
    }
}

Backend Configuration

server {
    listen 80;
    server_name backend.local;
    root /var/www/yii2-test/backend/web;

    index index.php;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/run/php/php8.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location ~* /\.(htaccess|svn|git) {
        deny all;
    }
}

Finally, add the domains to your system hosts file:

Advertisement
127.0.0.1 frontend.local
127.0.0.1 backend.local

Once everything is configured, access the frontend via http://frontend.local/ and the backend via http://backend.local/. This method is reliable for production and can be combined with HTTPS, caching, and additional security optimization.

Latest Articles