When building a website using Laravel, or downloading a script from GitHub, there is usually a script that needs to use a symlink so it can run smoothly. But, before discussing further how to create a symlink in cPanel, let us first discuss what a symlink is, or what is commonly called symbolic link.
Symlink? What is that?
Symlink is an abbreviation for Symbolic Link. Okay, some of us might already know this. But what is the actual meaning of a symlink?
Based on information from Wikipedia, a symlink is a file or folder that functions to point to another file or folder called a Target. So, for example, if we have 2 websites, A and B, and they use the same file in folder Z, we can just upload this one folder Z to the hosting, then create a symlink on website A and website B without having to upload the file and folder again.
Still confused?
For an easier example, you might be more familiar with the Add Shortcut feature in Windows OS, where we can point a folder or application to the desktop. When the shortcut on the desktop is opened, Windows will display the content inside that shortcut. It is more or less like that.
How to Create a Symlink in cPanel Without SSH
Usually, creating a symlink is done using SSH or the Terminal feature in cPanel. However, the method is certainly different if our web hosting does not support SSH or terminal.
So what do we do?
If our web hosting does not yet support the SSH feature, we can use the Cron Job feature (recommended), or use a script (less recommended).
1. Creating a Symlink in cPanel Using a Cron Job
To create a symlink using a cron job, first, we obviously need to know the location of the folder that stores the file (Target), and the location of the folder used to display the folder.
For example, here I have a target folder named content-files where this folder is located inside the symlink-wpgan/target-filesfolder, and then I want to add a symlink to the symlink-wpgan/public-filesfolder, so the method used is like this.
First, log in to cPanel, then find and open the Cron Jobs page.
After being on the Cron Jobs page, create a new cron with the Once per Minute setting.
If it is set to run once every minute, then in the next column, fill it in with this format.
ln -s /lokasi/folder/target /lokasi/folder/shortcutBecause I previously used cPanel and the folder location is inside symlink-wpgan/target-files/content-files and will create a shortcut in symlink-wpgan/public-files, the result will be like this.
ln -s /home/usercpanel/symlink-wpgan/target-files/content-files /home/usercpanel/symlink-wpgan/public-filesIf it has been added, of course, we just need to save the new cron job and wait 1 minute for the cron to run.
After running for one minute, we can check the /symlink-wpgan/public-files folder again through File Manager, and there will automatically be a shortcut pointing to the /symlink-wpgan/target-files/content-files
After the symlink shortcut is successfully created, make sure to delete the cron job so the process does not run repeatedly, which might actually cause a symlink error.
2. Creating a Symlink in cPanel Using a Script
This method is actually less recommended because the script might not be executable, or it could cause security vulnerabilities.
However, if you still want to use a script, we can create a new PHP file in the folder where we will create the symlink, for example with the filename sym.php
Next, here I will create a shortcut in the /home/usercpanel/sp.nasikin.my.id folder, then use the /home/usercpanel/symlink-wpgan/target-files/content-files folder as the target, so the script that needs to be used is like this.
<?php$target = '/home/usercpanel/symlink-wpgan/target-files/content-files';$shortcut = '/home/usercpanel/sp.nasikin.my.id/content-files';symlink($target, $shortcut);?>If the sym.php file has been saved, we can access that file to create the symlink. For example, in the example above, the php file can be accessed through the page. https://sp.nasikin.my.id/sym.php
After the symlink file is accessed, check the folder used to display the shortcut again, and a new shortcut folder will automatically appear.
After it is successful, make sure to delete the php file that was created so it does not cause security vulnerabilities on the website later.
Conclusion
Actually, the way to create a symlink in cPanel is easier using SSH or terminal. However, if the hosting used does not support that feature, we can use a different method that has the same function.
Besides that, make sure to delete the command or symlink addition file if the shortcut has been successfully created.
COMMENTS
Loading...