|
| 1 | +--- |
| 2 | +title: Serve Web Files on Root Filesystem |
| 3 | +--- |
| 4 | + |
| 5 | +Web files are accessible across your cluster deployment using NFS. |
| 6 | +While this provides a convenient way to share files, it can also introduce challenges, particularly around old code bases that still use modules that follow the PSR-0 standard. |
| 7 | +On newer deployments of Magento 2, this shouldn't be an issue, but if you are working with older code bases, you may need to serve web files from the root filesystem instead of NFS. |
| 8 | + |
| 9 | +On newer deployments of Magento 2 (template version 5.2 and later), you can convert your cluster to serve web files from the root filesystem. |
| 10 | +This will resolve any slowness that you might feel when accessing the admin interface but it does come with some caveats. |
| 11 | + |
| 12 | +## Conversion walkthrough |
| 13 | + |
| 14 | +!!! Assumptions: |
| 15 | +You are logged in as the cluster user (uid 9001) and your site's primary domain name is **example.com**. |
| 16 | +This feature is also only compatible with Magento at the moment, since other applications are not structured in a way that would support this type of setup. |
| 17 | +!!! |
| 18 | + |
| 19 | +First, we will need to modify the nginx configuration for your site to change where the root directory is set. |
| 20 | +Luckily, this is a simple process that can be done with the `vhost` command. |
| 21 | + |
| 22 | +``` |
| 23 | +vhost modify default-backend enable_sync_healthcheck=yes |
| 24 | +vhost modify example.com use_www_cache_root=yes |
| 25 | +``` |
| 26 | + |
| 27 | +If you made a lot of customizations to your nginx configs and deviated from the base template, then you may want to reimplement the changes modifing the nginx configs or implement the root change manually. You can review how much your configs deviate from the base template by running the following command: |
| 28 | + |
| 29 | +``` |
| 30 | +vhost diff example.com |
| 31 | +``` |
| 32 | + |
| 33 | +Next, we will need to create a custom boot hook that will sync the web files from the NFS share to the root filesystem on boot. |
| 34 | + |
| 35 | +```bash |
| 36 | +sudo cp -p /opt/jrc/hooks-available/boot.d/93-sync-www-data-on-boot /mnt/jrc-comms/hooks/boot.d/ |
| 37 | +``` |
| 38 | + |
| 39 | +Now all that is left is to execute the custom boot hook script to sync the web files from the NFS share to the root filesystem. |
| 40 | +The script also handles reloading nginx and restarting php-fpm. |
| 41 | + |
| 42 | +```bash |
| 43 | +cluster exec --role web -- sudo /mnt/jrc-comms/hooks/boot.d/93-sync-www-data-on-boot |
| 44 | +``` |
| 45 | + |
| 46 | +## Boot process |
| 47 | + |
| 48 | +Everything is handled in the boot hook script. |
| 49 | +The script syncs over web files for every site defined in `vhost` that has the `use_www_cache_root` option set to `yes`. |
| 50 | +Here are some high level steps of what happens during the boot process: |
| 51 | + |
| 52 | +1. Remove existing cache in `/var/www-cache` (faster copy process) |
| 53 | +2. Ensure that the web node can still serve web files by serving files from the NFS share. |
| 54 | +3. Start the copy process from the NFS share to the root filesystem. |
| 55 | +4. Start serving web files from the root filesystem. |
| 56 | + |
| 57 | +## Manually syncing changes |
| 58 | + |
| 59 | +If you need to sync changes that are made in your NFS share to the root filesystem, you can run the following command: |
| 60 | + |
| 61 | +```bash |
| 62 | +cluster exec --role web -- sudo /mnt/jrc-comms/hooks/boot.d/93-sync-www-data-on-boot |
| 63 | +``` |
| 64 | + |
| 65 | +This will run the same script that is executed during the boot process and will sync the web files from the NFS share to the root filesystem while still serving files from the NFS share until the copy process is complete. |
| 66 | + |
| 67 | +If you would like to simply copy the files over without having the NFS fallback, you can run the following command: |
| 68 | + |
| 69 | +```bash |
| 70 | +cluster exec --role web -- sudo /opt/jrc/sbin/sync-www-cache example.com |
| 71 | +``` |
| 72 | + |
| 73 | +## Manually rotate web nodes |
| 74 | + |
| 75 | +If you find yourself in a situation where you need to manually rotate your web nodes one by one, then you can run the following command: |
| 76 | + |
| 77 | +```bash |
| 78 | +cluster exec --role web -- sudo bash -c 'www-health mark unhealthy && sleep infinity' |
| 79 | +``` |
| 80 | + |
| 81 | +This command will run on every web node and mark the node as unhealthy, which will cause the target group to stop sending traffic to the node. |
| 82 | +It will then fail health checks and a new web node will be started to replace it. |
| 83 | +We wait till infinity because we want to wait for that node to be fully shutdown before continuing to replace the next web node. |
0 commit comments