Creating a CageFS template to make Node.js available to all users by default is a useful task in shared hosting environments, where you want to provide a common set of tools and applications to multiple users while maintaining security and isolation. CageFS is a virtualized file system that isolates each user’s environment. In this article, we’ll guide you through the process of setting up a CageFS template to make Node.js accessible to all users. We’ll explain each step and provide the necessary commands.
Prerequisites:
Before you start, ensure you have the following prerequisites:
CageFS Installed: CageFS should be installed on your server. CageFS is typically available as part of the CloudLinux OS. If you haven’t already, you can install it using the following command:
yum install cagefs
Node.js Installed: Node.js should be installed on your server. You can use the package manager (e.g., yum or apt) to install Node.js.
Now, let’s proceed with creating a CageFS template for Node.js.
Step 1: Enable CageFS for the User
Before you create a template, you need to enable CageFS for the user you want to provide access to Node.js. Replace <username>
with the actual username of the user.
cagefsctl --enable <username>
Step 2: Create a CageFS Skeleton Directory
A CageFS skeleton directory is a template directory that will be used as a basis for the user’s environment. We’ll create a custom skeleton directory and add Node.js to it.
mkdir -p /etc/cagefs/skeleton
Step 3: Install Node.js in the Skeleton Directory
Now, let’s install Node.js and npm in the skeleton directory. This way, it will be available to all users who have CageFS enabled.
cagefsctl --addrpm=nodejs
Step 4: Create a CageFS Template
Create a CageFS template from the updated skeleton directory.
cagefsctl --update
Step 5: Set the Template for the User
Set the created template for the user.
cagefsctl --set <username>
Step 6: Verify Node.js Availability
To verify that Node.js is available to the user, you can log in as that user and check if Node.js and npm are accessible.
su - <username>
node -v
npm -v
Step 7: Repeat for Additional Users (Optional)
You can repeat steps 1 to 6 for other users who also need access to Node.js. Each user will have their own isolated environment with Node.js installed.
Updating Node.js: To update Node.js for all users, you can simply update the version in the skeleton directory (/etc/cagefs/skeleton
) and then run cagefsctl --update
again.
Node.js Version: You can install a specific version of Node.js by specifying the version in the cagefsctl --addrpm
command. For example, to install Node.js 14, you can use cagefsctl --addrpm=nodejs14
.
Adding Global NPM Packages: If you want to make certain npm packages available globally to all users, you can install them in the skeleton directory as well.