Skip to main content

How to set up Drupal Composer Installation Work Outside of Web Folder?

How to set up Drupal Composer Installation Work Outside of Web Folder?
How to set up Drupal Composer Installation Work Outside of Web Folder?

 

Setting up Drupal Composer Installation Work Outside of Web Folder ensures your Drupal site is organized, secure, and easy to manage. By using Composer for Drupal installation and separating the web root, you ensure your Drupal site's security and scalability. In this guide, we’ll walk you through how to set up Drupal Composer Installation Work Outside of Web Folder, ensuring better scalability, performance, and security for your Drupal site.


 Related topics you may enjoy: What are the key differences between Drupal 10 and Drupal 11?


 

 

 

Step 1: How to set up Drupal Composer Installation Work Outside of Web Folder with its dependencies?

 

Example:

 

set up drupal composer installation

 

This will install a new directory my_project and the default Drupal structure, including the web directory.

 

After installation, change into the project directory:

bash
 
cd my_project 
 
 
 
bash
 
/my_project  
         /web       
         /core      
        /modules        
              /themes       
           /sites       
         /profiles    
       composer.json    
      composer.lock
 /vendor


Step 2: Move the web Directory
 

Now, this will create Drupal Composer Installation work outside of the Web Folder by relocating the Web folder to a different directory outside the document root.

  1.  Move the web folder into a new directory outside the document root of your web server.

  bash

 

mv web ../drupal
 

  • 2.  You then have the following directory structure:

    bash
     
    /my_project 
       /drupal       <-- Moved web directory here  
      composer.json    
    composer.lock    /vendor
     

 Step 3: Update Composer Configuration


 You’ll need to tell Composer to look for the web folder in its new location. This can be done by modifying the composer.json file.
 

1. Open composer.json in a text editor.

2. Include or alter the config section to reference the new Drupal directory. Add the following line:

json

 

"config": {

 "web-dir": "drupal" }

Upon this modification, your composer.json will look like this

json

 

{  "name": "drupal/recommended-project",

  "type": "project", 

 "license": "GPL-2.0-or-later",  

"config": {    "preferred-install": 

"dist",    "web-dir": "drupal"  },  ... }


Step 4: Web Server Configuration Adjustment

 

Now that you’ve moved the web directory, update your web server to point to the new location. For Apache or Nginx, make sure to adjust the DocumentRoot or root directive to the new drupal directory.

 

Apache Configuration
 

  1. Open your Apache virtual host configuration file. For example, if you're using a standard Ubuntu setup, the file might be located in /etc/apache2/sites-available/000-default.conf.

2. Modify the Document root to point to the Drupal folder:

apache
 

DocumentRoot /path/to/my_project/drupal
 
3. Make sure the following mod_rewrite and directory settings are implemented.
 
apache
 
<Directory /path/to/my_project/drupal>
    AllowOverride All
    Require all granted
 </Directory>
4. Restart Apache to apply changes:
 
bash
 
sudo systemctl restart apache2
 

Nginx Configuration
 

  1. Open your Nginx configuration file (typically located in /etc/nginx/sites-available/default or a similar file).
     

2. Modify the root directive to point to the Drupal directory:

nginx

server {

 listen 80;

 server_name example.com; 

root /path/to/my_project/drupal;

 index index.php index.html; ... }
 

3. Restart Nginx to apply changes:

bash
 
sudo systemctl restart nginx

Step 5: Set Up Drupal’s Settings File
 

Finally, you’ll need to configure Drupal’s settings file to reflect the new directory structure.

  1. Navigate to the sites/default directory in your Drupal folder.
     
  2. If it doesn’t already exist, create the settings.php file from the default.settings.php file:

bash

cp default.settings.php settings.php
 

3. Modify the $settings['file_public_path'] and $settings['file_private_path'] to reflect the correct file paths based on the new directory structure.
 

Step 6: Clear the Cache and Test the Setup
 

Run the following Composer command to clear Drupal's cache and ensure everything is working

bash

 

drush cr

Visit your website in a browser, and if everything is configured correctly, you should see your Drupal site working perfectly from the new location.
 

Conclusion
 

Setting up Drupal with Composer outside the web folder is useful for maintaining an organized, secure, and manageable structure. It is very useful in production environments where it is important to keep the public web root clean. With a little configuration, this setup lets you manage your Drupal site efficiently.

By learning how to set up Drupal Composer installation work outside the web folder, you’ll enhance your site’s security and performance while keeping everything neatly organized.

 

Tags

Related Solutions