Change document root on azure php webrole

1.4k Views Asked by At

I want setup Laravel framework on azure webrole and have to change document root, but so far I can't find way how to do this

My deployment project folder:

├──webrole <- now this is document root
│  ├──app
│  ├──bin
│  ├──bootstrap
│  ├──config
│  ├──database
│  ├──public <- I want make this as document root
│  ├──resources
│  ├──storage
│  ├──tests
│  ├──vendor
│  ├──.env
│  ├──.env.example
│  ├──.gitattributes
│  ├──.gitignore
│  ├──artisan
│  ├──composer.json
│  ├──composer.lock
│  ├──gulpfile.js
│  ├──package.json
│  ├──phpspec.yml
│  ├──phpunit.xml
│  ├──readme.md
│  ├──server.php
│  ├──Web.cloud.config
│  └──Web.config
├──deploymentSettings.json
├──ServiceConfiguration.Cloud.cscfg
├──ServiceConfiguration.Local.cscfg
├──ServiceDefinition.csdef

I found this Change approot path of Windows Azure WebRole but it's quite old question and there isn't approved answer

The only one workaround I've found is using rewrite rules but, I think that is not secure...

2

There are 2 best solutions below

7
On

As in Azure Cloud Service PHP WebRole directory, where are web.config and web.cloud.config files which is leveraged to configure PHP site application on IIS.

Web can find the content like the following in these files:

<system.webServer>
    <defaultDocument>
      <files>
        <clear />
        <add value="index.php" />
      </files>
    </defaultDocument>
  </system.webServer>

We can modify the setting defaultDocument to change the document root of your laravel application:

<system.webServer>
    <defaultDocument>
      <files>
        <clear />
        <add value="public/index.php" />
      </files>
    </defaultDocument>
  </system.webServer>
0
On

If someone is still searching for an answer:

You can change the root directory easily by replacing the nginx configuration file by taking these 2 steps:

1- Add the new configuration file to your project root (wwwroot), we call it default.

server {
#proxy_cache cache;
#proxy_cache_valid 200 1s;
listen 8080;
listen [::]:8080;
root /home/site/wwwroot/public; # changed for Laravel
index  index.php index.html index.htm;
server_name  example.com www.example.com;

location / {
    try_files $uri $uri/ /index.php?$args; # changed for Laravel
}

# redirect server error pages to the static page /50x.html
#
error_page   500 502 503 504  /50x.html;
location = /50x.html {
    root   /html/;
}

# Disable .git directory
location ~ /\.git {
    deny all;
    access_log off;
    log_not_found off;
}

# Add locations of phpmyadmin here.
location ~ [^/]\.php(/|$) {
    fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
    fastcgi_pass 127.0.0.1:9000;
    include fastcgi_params;
    fastcgi_param HTTP_PROXY "";
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param QUERY_STRING $query_string;
    fastcgi_intercept_errors on;
    fastcgi_connect_timeout         300;
    fastcgi_send_timeout           3600;
    fastcgi_read_timeout           3600;
    fastcgi_buffer_size 128k;
    fastcgi_buffers 4 256k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;
}

}

2- Add a startup command to replace the nginx configuration file with your new file.

  • Enter your azure resource settings -> configuration.

  • General settings tab.

  • Startup Command:

    cp /home/site/wwwroot/default /etc/nginx/sites-available/default && service nginx reload

This should change the document root each time your instance starts.

Resource: https://learn.microsoft.com/en-us/azure/mysql/flexible-server/tutorial-php-database-app