How to setup Drupal 8 Multisite on nginx webserver with different domain.

How to setup Drupal 8 Multi site on nginx web server with different domain. In this example we are going to use three different domain for our drupal-8 multi site ex: drupal-main.com, drupal-first.com, drupal-second.com Here drupal-main.com will be main site pointing to our drupal-8 installation, while two others will be pointing directory site1 and site2 respectively.

Directory structure will be as follows:


  var/www/drupal-main.com/public - will contain drupal-8 installation
  var/www/drupal-main.com/public/sites/site1
  var/www/drupal-main.com/public/sites/site2
  

Create above directory structure on your server. Now create a copy of sites/default/default.settings.php to sites/default/settings.php and also make a copy of example.sites.php to sites.php available in sites folder and put the following site alias at the end of file:


  $sites['drupal-first.com'] = 'site1';
  $sites['drupal-second.com'] = 'site2';
  

After that also make copy of default.service.yml and default.settings.php as follows:


  cp default/default.settings.php site1/settings.php
  cp default/default.settings.php site2/settings.php
  cp default/default.services.yml site1/services.yml
  cp default/default.services.yml site2/services.yml 
  

Now, create sever block for above three separate sites as follows:


  cd /etc/nginx.conf.d
  

sudo vi drupal-main.conf and add the below text to it


  server {
    listen   80;
    server_name  www.drupal-main.com;
    rewrite ^/(.*) http://drupal-main.com/$1 permanent;
  }
  server {
    listen   80;
    server_name drupal-main.com;
    root   /var/www/drupal-main.com/public/;
    index  index.php;
    # For Drupal >= 7 clean url
    location / {
      try_files $uri /index.php?$query_string; 
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
      include snippets/fastcgi-php.conf;
      fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
  }
  

sudo vi drupal-first.conf and add the below text to it


  server {
    listen   80;
    server_name  www.drupal-first.com;
    # rewrite ^/(.*) http://drupal-first.com/$1 permanent;
  }

  server {
    listen   80;
    server_name drupal-first.com;

    root   /var/www/drupal-main.com/public/;
    index  index.php;

    # For Drupal >= 7 clean url
    location / {
      try_files $uri /index.php?$query_string; 
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
      include snippets/fastcgi-php.conf;
      fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
  }
  

sudo vi drupal-second.conf and add the below text to it

  
  server {
    listen   80;
    server_name  www.drupal-second.com;
    rewrite ^/(.*) http://drupal-second.com/$1 permanent;
  }
  server {
    listen   80;
    server_name drupal-second.com;

    root   /var/www/drupal-main.com/public/;
    index  index.php;

    # For Drupal >= 7 clean url
    location / {
      try_files $uri /index.php?$query_string; 
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
      include snippets/fastcgi-php.conf;
      fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
  }
  

Finally, add entry to /etc/hosts file as follows:


  # drupal sites
  127.0.1.1    drupal-main.com www.drupal-main.com
  127.0.1.1    drupal-first.com www.drupal-first.com
  127.0.1.1    drupal-second.com www.drupal-second.com
  

and restart your nginx server

-:done:-

Comments

Popular posts from this blog

Install drush globally using composer on WSL

Drupal views create exposed filter programmatically