I hope someone can help me here. I just want to change the index.html to index.php, but when I do so I get the 403 forbidden error. I followed a bunch of tutorials on permission and installing php etc. to no avail. I am a complete newbie, so most of the time I don’t really know what I am doing but I am trying to follow the tutorials.

If someone could give me complete newbie intructions on how to solve this, I’d be very grateful. I learned how to access files and other simple stuff but not much more.

I am using nginx on ubuntu. With the html files my website works fine, but I need it .php to run something.

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.

×
5 answers

Hi @karpred ,
I had to start new answer again :D
Thanks for the latest result, and please provide one couple more. So, it will be two files: get_me_and_remove_me_2 and get_me_and_remove_me_3

1.

For sudoer:

sudo grep -e server_name -e root -e index -e include /etc/nginx/sites-available/* > ~/get_me_and_remove_me_2 && sudo cp /home/$USER/get_me_and_remove_me_2 /var/www/

For root:

grep -e server_name -e root -e index -e include /etc/nginx/sites-available/* > /var/www/get_me_and_remove_me_2

2.

For sudoer:

sudo ls -la /etc/nginx/sites-enabled/  > ~/get_me_and_remove_me_3 && sudo cp /home/$USER/get_me_and_remove_me_3 /var/www/

For root:

sudo ls -la /etc/nginx/sites-enabled/  > /var/www/get_me_and_remove_me_3
  • Hi,

    getmeandremoveme_2:

    /etc/nginx/sites-available/auto-inc.club:        root /var/www/auto-inc.club/html;
    /etc/nginx/sites-available/auto-inc.club:        index index.html index.htm index.nginx-debian.html;
    /etc/nginx/sites-available/auto-inc.club:        server_name auto-inc.club www.auto-inc.club;
    /etc/nginx/sites-available/default: # include snippets/snakeoil.conf;
    /etc/nginx/sites-available/default: root /var/www/html/auto-inc.club;
    /etc/nginx/sites-available/default: # Add index.php to the list if you are using PHP
    /etc/nginx/sites-available/default: index index.html index.htm index.nginx-debian.html index.php;
    /etc/nginx/sites-available/default: server_name auto-inc.club www.auto-inc.club;
    /etc/nginx/sites-available/default: #   include snippets/fastcgi-php.conf;
    /etc/nginx/sites-available/default: # deny access to .htaccess files, if Apache's document root
    /etc/nginx/sites-available/default:#    server_name example.com;
    /etc/nginx/sites-available/default:#    root /var/www/example.com;
    /etc/nginx/sites-available/default:#    index index.html;
    /etc/nginx/sites-available/example.com:server {        listen 80;        root /var/www/html;        index index.php index.html index.htm index.nginx-debian.html;        server_name example.com;        location / {                try_files $uri $uri/ =404;        }        location ~ \.php$ {                include snippets/fastcgi-php.conf;                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;        }        location ~ /\.ht {   deny all;        }}
    /etc/nginx/sites-available/test.example.com.conf:index index.html index.htm index.php;
    
    

    getmeandremoveme_3:

    total 8
    drwxr-xr-x 2 root root 4096 Sep 25 13:23 .
    drwxr-xr-x 8 root root 4096 Sep 26 15:23 ..
    lrwxrwxrwx 1 root root   40 Aug 22 14:01 auto-inc.club -> /etc/nginx/sites-available/auto-inc.club
    lrwxrwxrwx 1 root root   38 Sep 25 13:23 example.com -> /etc/nginx/sites-available/example.com
    
    
    • Thank you. Apparently we must focus on /etc/nginx/sites-available/auto-inc.club. Please, follow the steps:

      1. Check what PHP version you have installed.

      php --version
      

      I highlighted the version in the exemplary output.

      Output
      PHP 7.4.3 (cli) (built: May 26 2020 12:24:22) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

      If you will get an output like below, that means you need to install php-fpm package.

      Output
      Command 'php' not found, but can be installed with: apt install php7.2-cli apt install hhvm Ask your administrator to install one of them.

      To install the php-fpm, run:

      sudo apt update && sudo apt -y install php-fpm
      

      2. Open config file /etc/nginx/sites-available/auto-inc.club in your favorite text editor, and add index.php to the list of files. It should look like:

      index index.html index.htm index.nginx-debian.html index.php;
      

      Then, look for a block looking like that

        location ~ \.php$ {                
          include snippets/fastcgi-php.conf;                
          fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;        
        }        
      

      If there is not any, then simply add it, replacing PHP version with the correct one (from step 1).

      3. Save your config file, and check if its syntax is correct

      sudo nginx -t
      

      If it is OK, you will get the result like

      Output
      nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful

      4. Ensure that your index.php file is located in document directory defined in the server block of your domain: /var/www/auto-inc.club/html

      5. Restart nginx service.

      sudo systemctl restart nginx
      

      If it is still unsuccessfully, please provide the file /etc/nginx/sites-available/auto-inc.club.

      • Hey :)

        I had already installed PHP but all the rest in /etc/nginx/sites-available/auto-inc.club was missing.

        It’s now finally working, thank you guys!!!!!

        • Hi @karpred,
          I am happy it helped :)
          What I can suggest is to remove all unnecessary files containing nginx server blocks, just to avoid any doubts in the future. In my opinion, you can safely remove following symlinks and files:

          /etc/nginx/sites-enabled/example.com
          /etc/nginx/sites-available/example.com
          /etc/nginx/sites-available/test.example.com.conf
          /etc/nginx/sites-available/default
          

          To remove them, use a command rm, e.g.

          sudo rm /etc/nginx/sites-enabled/example.com
          

Hi,

Try the following steps.

1. Restart nginx service

sudo systemctl restart nginx

2. Run the command

curl http://your_droplet_ip/

3. In your Internet browser’s address bar enter:

http://your_droplet_ip/index.php

4. Clear Internet browser’s cache.

If it is still unsuccessfully, give us a results of step 2 and 3.

Hi @karpred ,
I had to start new answer because we got to many replies to the previous one :D

@karpred wrote
here is my config file code...

I meant nginx config file of your website, or rather nginx server block of your website. Sorry, I did not specify it clearly. This file is usually located in a directory /etc/nginx/sites-available/

@karpred wrote
I don’t know how to copy the code from the console

I do not know that too :) I ssh my droplets using gnome terminal what gives me text copy-paste feature. But in your case, having an FTP access to the droplet, you can transfer the file in easy way, e.g.

1. Copy the file to the directory you can reach with an FTP client

sudo cp /etc/nginx/sites-available/your_website_config /var/www/get_me_and_remove_me

2. Change an owner of the copied file

sudo chown your_ftp_user /var/www/get_me_and_remove_me

3. Get the file through the FTP and remove it from FTP directory. Now you can open this file in a text editor, copy text, and paste it here :)

@karpred wrote
When I use http://your_droplet_ip/index.php, I get a '404 Not Found’ now.

We are just one step to solving the problem, I think so :D

  • You’re the best, thanks :D

    Here it is:

    ##
    # You should look at the following URL's in order to grasp a solid understanding
    # of Nginx configuration files in order to fully unleash the power of Nginx.
    # https://www.nginx.com/resources/wiki/start/
    # https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
    # https://wiki.debian.org/Nginx/DirectoryStructure
    #
    # In most cases, administrators will remove this file from sites-enabled/ and
    # leave it as reference inside of sites-available where it will continue to be
    # updated by the nginx packaging team.
    #
    # This file will automatically load configuration files provided by other
    # applications, such as Drupal or Wordpress. These applications will be made
    # available underneath a path with that package name, such as /drupal8.
    #
    # Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
    ##
    
    # Default server configuration
    #
    server {
        listen 80 default_server;
        listen [::]:80 default_server;
    
        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;
    
        root /var/www/html;
    
        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html index.php;
    
        server_name _;
    
        location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
        }
    
        # pass PHP scripts to FastCGI server
        #
        #location ~ \.php$ {
        #   include snippets/fastcgi-php.conf;
        #
        #   # With php-fpm (or other unix sockets):
        #   fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        #   # With php-cgi (or other tcp sockets):
        #   fastcgi_pass 127.0.0.1:9000;
        #}
    
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #   deny all;
        #}
    }
    
    
    # Virtual Host configuration for example.com
    #
    # You can move that to a different file under sites-available/ and symlink that
    # to sites-enabled/ to enable it.
    #
    #server {
    #   listen 80;
    #   listen [::]:80;
    #
    #   server_name example.com;
    #
    #   root /var/www/example.com;
    #   index index.html;
    #
    #   location / {
    #       try_files $uri $uri/ =404;
    #   }
    #}
    
    
    • Thank you for that file but, reading your conversation with @KFSys, I must say we need more information from you.

      Please perform following actions.

      1. Execute one of the commands put below, depending on the user you are logged in your droplet with.

      If you are a sudoer (a user who can act as root using sudo command), run this command:

      sudo grep -e server_name -e root -e index $(find /etc/nginx/ -name "*.conf*") > ~/get_me_and_remove_me_2 && sudo cp /home/$USER/get_me_and_remove_me_2 /var/www/
      

      If you are simply root, run this command:

      grep -e server_name -e root -e index $(find /etc/nginx/ -name "*.conf*") > /var/www/get_me_and_remove_me_2
      

      Good news is you can copy the command and paste it into console in your DO control panel (Ctrl+V) :)

      2. Now, you should have a file /var/www/get_me_and_remove_me_2, and you can get it through FTP. Change an owner of this file:

      sudo chown your_ftp_user /var/www/get_me_and_remove_me_2
      

      3. Get the file through the FTP and remove it from FTP directory. Provide its content to us. You can mask your domain name with e.g. mydomain.com, if you do not want to share it.

      • Hey,

        here is the content of that file:

        /etc/nginx/nginx.conf:index index.html index.htm index.php;
        /etc/nginx/nginx.conf:    server_names_hash_bucket_size 64;
        /etc/nginx/nginx.conf:  # server_name_in_redirect off;
        /etc/nginx/snippets/fastcgi-php.conf:fastcgi_index index.php;
        /etc/nginx/sites-available/test.example.com.conf:index index.html index.htm index.php;
        /etc/nginx/fastcgi.conf:fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        /etc/nginx/fastcgi.conf:fastcgi_param  DOCUMENT_ROOT      $document_root;
        /etc/nginx/fastcgi.conf:fastcgi_param  SERVER_NAME        $server_name;
        
        • Can you give me the console command to delete the file again please? I didn’t give my user permission to delete it via ftp and I don’t know how to change that right now :D

        • The content looks really strange. Can you run below command, which is a bit extended, comparing to the previous one, please ?

          For sudoer:

          sudo grep -e server_name -e root -e index -e include $(find /etc/nginx/ -name "*.conf*") > ~/get_me_and_remove_me_2 && sudo cp /home/$USER/get_me_and_remove_me_2 /var/www/
          

          For root:

          grep -e server_name -e root -e index -e include $(find /etc/nginx/ -name "*.conf*") > /var/www/get_me_and_remove_me_2
          

          Provide its result, please.

          • /etc/nginx/nginx.conf:include /etc/nginx/modules-enabled/*.conf;
            /etc/nginx/nginx.conf:index index.html index.htm index.php;
            /etc/nginx/nginx.conf:    server_names_hash_bucket_size 64;
            /etc/nginx/nginx.conf:  # server_name_in_redirect off;
            /etc/nginx/nginx.conf:  include /etc/nginx/mime.types;
            /etc/nginx/nginx.conf:  include /etc/nginx/conf.d/*.conf;
            /etc/nginx/nginx.conf:  include /etc/nginx/sites-enabled/*;
            /etc/nginx/snippets/fastcgi-php.conf:fastcgi_index index.php;
            /etc/nginx/snippets/fastcgi-php.conf:include fastcgi.conf;
            /etc/nginx/sites-available/test.example.com.conf:index index.html index.htm index.php;
            /etc/nginx/fastcgi.conf:fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
            /etc/nginx/fastcgi.conf:fastcgi_param  DOCUMENT_ROOT      $document_root;
            /etc/nginx/fastcgi.conf:fastcgi_param  SERVER_NAME        $server_name;
            
            

Hello @karpred,

You need to check 2 or 3 things In your domain’s Nginx configuration file.

The first is your root or referred to as DocumentRoot directive. In your case it’s /var/www/html but are you sure that’s where your files are located? Enter that directory and see if the files are there or located in another folder named after your domain. If it’s the second option just add the domain in your root directive :

root /var/www/html

to be

root /var/www/html/domain.com

That’s only if the files are in the said folder.

The next thing to change would be the server_name

server_name _;

You should change it to include your domain

server_name domain.com www.domain.com;

After you make those changes, restart nginx and see if your website works properly now.

Regards,
KFSys

Hi @karpred,

It seems like you might not have PHP installed.

Please follow the last reply from @Yannek, they provided you with steps on how to install PHP on your droplet. See if that works.

Submit an Answer