Question

Lavarel project with nginx server return 404|Not Found but works fine with default /var/www/html root

Posted June 15, 2020 340 views
NginxLEMPLaravel

I am using Digital Ocean VPS with a LEMP stack.
I am struggling in the problem that return 404|not Found when accessing to my website. My lavarel project is stored as /var/www/html/api. It works fine with default /var/www/html root. My current config file is:

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    root /var/www/html/api/public;
    #root /var/www/html; //ok

    # Add index.php to the list if you are using PHP
    index index.php index.html index.htm index.nginx-debian.html;

    server_name ipaddress;
    client_max_body_size 64M;
    location / {

            try_files $uri $uri/ /index.php$is_args$args;
    }
    location ~ \.php$ {

            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    }
}

Any help would be appreciative. Thanks a lot.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
3 answers

Hey,

Question, have you checked the log files and can you also provide us with it ?
Although I also suspect as @denislavgavrilov mentioned that this is a permissions problem.

Regards,
Ivan

  • Dear @YoghurtPower :
    At the first time I checked the log file, it was said that I had a file permission problem with ‘Storage’ folder. I did change this folder permission to 775 using :

    sudo chmod -R 775 /var/www/html/api/storage
    

    And now when I checked the error.log file, there was nothing inside. Access.log listed that my local computer accessing to web server as below:

    - - [17/Jun/2020:03:29:13 +0000] "GET / HTTP/1.1" 404 622 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"
    - - [17/Jun/2020:03:29:14 +0000] "GET /favicon.ico HTTP/1.1" 200 0 "http://IP_Address
    

    Thanks a lot for your help.
    Best
    Ny

Hey, @py1210 ,

From what I can see, the nginx configuration seems perfectly fine. Could you please try and readjust the permissions for your Laravel projects with these:

sudo find /path/to/your/laravel/root/directory -type f -exec chmod 664 {} \;    
sudo find /path/to/your/laravel/root/directory -type d -exec chmod 775 {} \;

Also, please make sure to make the www-data owner of the directory:

sudo chown www-data:www-data /path/to/your/laravel/root/directory

I hope this helps and feel free to keep the conversation going!

Best,

  • Dear @denislavgavrilov ,
    Thanks a lot for your suggestion. I tried to execute the linux command but nothing appear.

    root@customize:/var/www/html/api# sudo find /var/www/html/api/ -type f -exec chmod 664 {} \;
    root@customize:/var/www/html/api# sudo find /var/www/html/api/ -type f -exec chmod 775 {} \;
    

    The www-data owner of the directory was set before. And the storage folder has the permission with 775 (I use sudo chmod -R 775 /var/www/html/laravel/storage).
    If you have any other suggestion, please kindly let me know. I greatly appreciate your help.
    Best,

    • Hey there,

      I can see you tried to execute ’-type f’ 2 times, instead of -d 755(for example) for the directories.

      Could you try and set the directories’ permissions as well?

      Best,

      • Hi @denislavgavrilov :
        I can not understand your last question, replacing ’-type f’ by ’-d 755’. Please can you help me clarify it? I understand that we will list all the files in folder /api/ and check for the permission 664/775.
        here is the command line result I did.

        root@customize:/var/www/html# sudo find /var/www/html/api -type f -exec chmod 775 {} \;
        root@customize:/var/www/html# 
        root@customize:/var/www/html# 
        root@customize:/var/www/html# 
        root@customize:/var/www/html# cd api
        root@customize:/var/www/html/api# ls -lrt
        total 424
        drwxrwxr-x 10 www-data www-data   4096 Jun 15 01:35 Modules
        -rwxrwxr-x  1 www-data www-data   1686 Jun 15 01:35 artisan
        drwxrwxr-x  6 www-data www-data   4096 Jun 15 01:35 app
        drwxrwxr-x  4 www-data www-data   4096 Jun 15 01:35 tests
        drwxrwxr-x  5 www-data www-data   4096 Jun 15 01:35 storage
        drwxrwxr-x  2 www-data www-data   4096 Jun 15 01:35 routes
        .....
        -rwxrwxr-x  1 www-data www-data   1125 Jun 15 01:35 package.json
        drwxrwxr-x  3 www-data www-data   4096 Jun 15 01:35 database
        drwxrwxr-x  2 www-data www-data   4096 Jun 15 01:35 config
        drwxrwxr-x  3 www-data www-data   4096 Jun 15 04:16 bootstrap
        drwxrwxr-x 71 www-data www-data   4096 Jun 15 04:42 vendor
        -rwxrwxr-x  1 www-data www-data 355447 Jun 15 04:42 composer.lock
        drwxrwxr-x  2 www-data www-data   4096 Jun 16 01:04 public
        root@customize:/var/www/html/api#
        

        Thanks a lot for your help and I look forward to your reply. I am sorry for late reply because maybe we are in different time zone.

        • Hey there,

          The -d and -f flags are for -directory and -files.I will give you some possible fixes but I will need more information before that.

          Could you try and enter the following commands I am going to list below:

          • sudo find /var/www/html/api -type f -exec chmod 664 {} \;
          • sudo find /var/www/html/api -type d -exec chmod 775 {} \;
          • cd /var/www/html/api
          • sudo chgrp -R www-data storage bootstrap/cache
          • sudo chmod -R ug+rwx storage bootstrap/cache

          I will propose a new Nginx config but as I told you, I will need some more info. Could you do tail -n 20 /var/log/nginx/error.log and post what is printed in the terminal?

          I hope the answer is not too messy and please let me of your progress!

          Best,

Hi @denislavgavrilov ,
Thanks a lot for your detailed instruction. I followed your comment and below is the terminal output.

root@customize:/var/log/nginx# sudo find /var/www/html/api -type f -exec chmod 664 {} \;
root@customize:/var/log/nginx# sudo find /var/www/html/api -type d -exec chmod 775 {} \;
root@customize:/var/log/nginx# cd /var/www/html/api
root@customize:/var/www/html/api# sudo chgrp -R www-data storage bootstrap/cache
root@customize:/var/www/html/api# sudo chmod -R ug+rwx storage bootstrap/cache
root@customize:/var/www/html/api# 
root@customize:/var/www/html/api# 
root@customize:/var/www/html/api# ls -lrt
total 424
drwxrwxr-x 10 www-data www-data   4096 Jun 15 01:35 Modules
-rw-rw-r--  1 www-data www-data   1686 Jun 15 01:35 artisan
drwxrwxr-x  6 www-data www-data   4096 Jun 15 01:35 app
-rw-rw-r--  1 www-data www-data    537 Jun 15 01:35 webpack.mix.js
drwxrwxr-x  4 www-data www-data   4096 Jun 15 01:35 tests
drwxrwxr-x  5 www-data www-data   4096 Jun 15 01:35 storage
-rw-rw-r--  1 www-data www-data    563 Jun 15 01:35 server.php
drwxrwxr-x  2 www-data www-data   4096 Jun 15 01:35 routes
.....
-rw-rw-r--  1 www-data www-data   1125 Jun 15 01:35 package.json
drwxrwxr-x  3 www-data www-data   4096 Jun 15 01:35 database
drwxrwxr-x  2 www-data www-data   4096 Jun 15 01:35 config
-rw-rw-r--  1 www-data www-data   2451 Jun 15 03:19 composer.json
drwxrwxr-x  3 www-data www-data   4096 Jun 15 04:16 bootstrap
drwxrwxr-x 71 www-data www-data   4096 Jun 15 04:42 vendor
-rw-rw-r--  1 www-data www-data 355447 Jun 15 04:42 composer.lock
drwxrwxr-x  2 www-data www-data   4096 Jun 16 01:04 public
root@customize:/var/www/html/api# tail -n 20 /var/log/nginx/error.log

Nothing appeared in last tail command.And the 404|Not Found stayed the same when accessing to the website.
Thanks.
Ny

  • Hey @py1210 ,

    Okay, now could you type php -v and tell me what your PHP version is?

    Best,

    • Hi @denislavgavrilov :
      My Php version is 7.3.19.

      Thanks,
      ny

      • Okay @py1210 ,

        Now let’s try and change a line in your Nginx config:

        fastcgi_pass unix:/run/php/php7.2-fpm.sock; to fastcgi_pass unix:/run/php/php7.3-fpm.sock; .

        Best,

        • Hi @denislavgavrilov ,
          Sorry from the beginning I made a mistake. I had modified the nginx config file to php7.3-fpm already. I will edit my first post.

          • Hey @py1210 ,

            I just created a quick LEMP stack and everything was working fine on my end with the configuration you posted.

            One last thing before my final proposition: You don’t forget to restart your nginx service (systemctl restart nginx) when you made changes to your config file, right? You can also try and restart php7.3-fpm.service as well and see if there is anything.

            I assume you do not have anything built upon your Laravel project, so what I could suggest is to reinstall it from scratch..

            rm -rf /var/www/html/api
            cd /var/www/html
            composer create-project --prefer-dist laravel/laravel api
            chown -R www-data:www-data /var/www/html/api
            sudo find /var/www/html/api -type d -exec chmod 775 {} \;
            sudo find /var/www/html/api -type f -exec chmod 664 {} \;
            cd /var/www/html/api/
            sudo chmod -R ug+rwx storage bootstrap/cache
            sudo chgrp -R www-data storage bootstrap/cache
            
            

            After that, make sure your Nginx config file looks like this:

            server {
                listen 80 default_server;
                listen [::]:80 default_server;
                root /var/www/html/api/public;
                #root /var/www/html; //ok
            
                # Add index.php to the list if you are using PHP
                index index.php index.html index.htm index.nginx-debian.html;
            
                server_name your_ip;
                client_max_body_size 64M;
                location / {
            
                        try_files $uri $uri/ /index.php$is_args$args;
                }
                location ~ \.php$ {
            
                        include snippets/fastcgi-php.conf;
                        fastcgi_pass unix:/run/php/php7.3-fpm.sock;
                }
            }
            
            

            After you are sure you have the correct Nginx config, do the following:

            systemctl restart nginx.service
            systemctl restart php7.3-fpm.service
            
            

            I hope this finally helps. Apologies for suggesting a fresh-install of Laravel.. I am out of options as everything is working on my side. :(

            Best regards,

          • Hi @denislavgavrilov,
            I will try your solution later because I forgot mysql root password and tried to recover it now. I will post the result soon.
            Thanks a lot for your help and effort. I really appreciated.
            Best,
            Ny

          • Hey @py1210 ,

            I just thought of another possible reason this is not working for you. Do you have any other server blocks in your Nginx? Also, do you have any other directory named ‘app’ in your /var/www/* tree?

            Best regards,

          • Dear @denislavgavrilov ,
            Yes, you are totally correct. I tested with the laravel new and it worked fine. And the problem was the laravel project I used. My friend split the font end and backend to 2 separate folder and I pointed to backend folder only. Thanks a lot for your support.

  • Hey @py1210 ,

    I’m happy this worked for you!

    Best regards,
    Denislav

Submit an Answer