You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Ubuntu 18.4下Nginx网站根目录迁移至其他分区求助

解决Ubuntu 18.04下Nginx网站根目录迁移到其他分区的问题

Hey there! Let's work through this together—since you're on Ubuntu 18.04, the core idea is similar to 16.04, but we need to make sure we handle the mounted partition correctly (that's probably where the issue is right now). Here's a step-by-step fix:

1. 确认分区挂载状态

First, let's make sure /dev/sda7 is actually mounted to /mnt/7282B48E0BE626E0. Run this command to check:

mount | grep sda7

If you don't see a line linking /dev/sda7 to your /mnt/... path, you need to mount it first:

sudo mount /dev/sda7 /mnt/7282B48E0BE626E0

To make this mount permanent (so it survives reboots), add a line to /etc/fstab:

sudo nano /etc/fstab

Add this line at the end (adjust if your filesystem is not NTFS—your mount point name suggests it might be a Windows partition):

/dev/sda7 /mnt/7282B48E0BE626E0 ntfs-3g defaults,uid=33,gid=33,umask=022 0 0

注:uid=33gid=33对应www-data用户(Nginx默认运行用户),这对NTFS分区很重要,因为NTFS不原生支持Linux权限系统。

2. 迁移网站文件到目标目录

Since your target directory is empty, copy all files from the original root while preserving permissions and attributes:

sudo cp -a /var/www/html/* /mnt/7282B48E0BE626E0/www/html/

The -a flag ensures file ownership, permissions, and timestamps are kept intact—this is crucial for Nginx to access the files properly.

3. 修改Nginx站点配置

Open the default site config file just like you did in 16.04:

sudo nano /etc/nginx/sites-available/default

Find the line that looks like root /var/www/html; and replace it with your new path:

root /mnt/7282B48E0BE626E0/www/html;

Double-check that the index line (e.g., index index.html index.htm;) is still correct for your site.

4. 修复目录权限

Even if you copied files with -a, we need to make sure www-data has access to the new directory. Run these commands:

# Set ownership to www-data user/group
sudo chown -R www-data:www-data /mnt/7282B48E0BE626E0/www/html
# Set read/write/execute permissions appropriately
sudo chmod -R 755 /mnt/7282B48E0BE626E0/www/html

如果你用的是NTFS分区,刚才在fstab里设置的uid/gid已经处理了权限,这一步可能不需要,但运行一下也没坏处。

5. 测试配置并重启Nginx

Always test your Nginx config for syntax errors before restarting:

sudo nginx -t

If you see test is successful, restart the service to apply changes:

sudo systemctl restart nginx

6. 验证迁移成功

You can test this by accessing your server's IP/domain in a browser, or running:

curl localhost

You should see the content from your new root directory.

为什么直接用/dev/sda7/www/html不行?

/dev/sda7 is the raw device file for your partition, not a directory you can access directly. You have to mount the partition to a directory (like /mnt/7282B48E0BE626E0) first, then use the path inside that mount point.


内容的提问来源于stack exchange,提问作者M7Shapan

火山引擎 最新活动