Setting up SFTP with Nginx
Nginx Directory Permissions and Symbolic Link Configuration Guide
Environment Overview
- Using SFTP to upload files to personal directory
- Personal directory linked to website root via symbolic link
- Files accessible through public web URL after upload
Example SFTP Upload Process
- Upload file to personal directory:
/home/your_user/public_folder/ - File automatically reflects in website directory:
/usr/share/nginx/html/folder/ - Access via URL: http://your_domain/folder/file.png
Configuration Steps
- Verify Symbolic Link
<syntaxhighlight lang="bash"># Check if symbolic link is correct ls -l /home/your_user/public_folder</syntaxhighlight> Expected output:
lrwxrwxrwx 1 your_user your_user 26 Jan 20 15:00 /home/your_user/public_folder -> /usr/share/nginx/html/folder
- Check Directory Structure and Permissions
<syntaxhighlight lang="bash"># Check website root directory sudo ls -lht /usr/share/nginx/html/
- Check target directory
sudo ls -lht /usr/share/nginx/html/folder/</syntaxhighlight>
- Check Nginx Configuration
<syntaxhighlight lang="bash"># Check nginx config sudo nano /etc/nginx/nginx.conf
- Or check site config
sudo nano /etc/nginx/sites-available/default</syntaxhighlight> Basic nginx configuration should include:
<syntaxhighlight lang="nginx">server {
listen 80; server_name your_domain;
root /usr/share/nginx/html; index index.html;
location / {
try_files $uri $uri/ =404;
}
}</syntaxhighlight>
Common Issues:
- Specific directories return 404 (files exist)
- Files uploaded via SFTP not accessible via web
- Nginx error log shows permission issues
Solution: Adjust Permission Settings
- Check Nginx Error Log
<syntaxhighlight lang="bash">sudo tail /var/log/nginx/error.log</syntaxhighlight> Possible error message:
[crit] stat() "[path]/file.png" failed (13: Permission denied)
- Set User Directory Permissions
<syntaxhighlight lang="bash"># Ensure user directory is accessible sudo chmod 755 /home/your_user</syntaxhighlight>
- Set Website Directory Permissions
<syntaxhighlight lang="bash"># Ensure website directory is accessible sudo chmod 755 /home/your_user/public_folder</syntaxhighlight>
- (optional) Set File Permissions
<syntaxhighlight lang="bash"># Set file read permissions chmod 644 /usr/share/nginx/html/folder/your_file.png</syntaxhighlight>
Permission Details
chmod 755(directories):- Owner: read, write, execute (rwx)
- Group: read and execute (r-x)
- Others: read and execute (r-x)
chmod 644(files):- Owner: read, write (rw-)
- Group: read (r–)
- Others: read (r–)
Testing and Verification
Public URL format: http://your_domain/folder/file.png
If still inaccessible after configuration:
<syntaxhighlight lang="bash"># Reload nginx configuration
sudo systemctl reload nginx</syntaxhighlight>
Important Notes
- Permission changes take effect immediately, no nginx restart needed
- Entire path needs proper execute permissions
- New files need correct permissions set
- Ensure symbolic links remain intact
- Verify files are uploaded to correct location