You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel部署至虚拟服务器后图片无法加载问题求助

Fixing Image Loading Issues on Virtual Server (Works Locally)

Hey there! Let’s sort out that image loading problem you’re dealing with—everything works great locally, but the virtual server won’t show your post images. Based on the code snippet you shared and common deployment pitfalls, here are the most likely fixes to try:

Since your code uses $post->images()->get(), I’m guessing this is a Laravel project. Locally, the public/storage symlink is often created automatically, but on servers you have to do it manually:

  • SSH into your virtual server, navigate to your project root, and run:
    php artisan storage:link
    

This connects your storage/app/public folder (where images are probably stored) to the public/storage directory that the web server can access.

2. Verify File Permissions

Virtual servers often have strict permission settings that block the web server from reading image files. Fix this by setting the right permissions for your storage directory:

  • Run these commands (adjust www-data to your web server user if it’s different, like apache):
    chmod -R 755 storage/
    chown -R www-data:www-data storage/
    

This ensures the web server can read and access your image files.

3. Fix Image URL Generation in Your Code

Your current <img> tag might be using a local path that doesn’t translate to the server. Instead of hardcoding paths, use your framework’s asset helper to generate absolute URLs:

  • Update your blade code to something like this:
    @foreach($post->images()->get() as $image)
        <!-- 遍历所有以当前帖子ID为外键的图片 -->
        <div class="row">
            <div class="col-md-12">
                <img id="event-image" style="width: 600px; height: auto;" 
                     src="{{ asset('storage/' . $image->path) }}" 
                     alt="Post image">
            </div>
        </div>
    @endforeach
    

Assuming $image->path is the relative path stored in your database (e.g., images/post_123.jpg), asset() will generate the correct URL for your server.

4. Double-Check Web Server Configuration

If your web server (Nginx/Apache) isn’t pointing to the right root directory, static assets like images won’t load:

  • For Nginx, open your site’s config file and make sure the root directive points to your project’s public folder:
    server {
        listen 80;
        server_name your-domain.com;
        root /var/www/your-project/public; # Update this path
    
        index index.php index.html;
    
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }
    }
    
  • After updating, restart Nginx:
    sudo systemctl restart nginx
    

Start with the symlink and permission checks first—those are the most common culprits for this exact issue!

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:35:37