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:
This will install a new directory my_project and the default Drupal structure, including the web directory.
After installation, change into the project directory:
2. You then have the following directory structure:
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
- 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
Nginx Configuration
- 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:
Step 5: Set Up Drupal’s Settings File
Finally, you’ll need to configure Drupal’s settings file to reflect the new directory structure.
- Navigate to the sites/default directory in your Drupal folder.
- 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.