David
David

Reputation: 528

Is there a way to add an existing wordpress installation to nginx?

I initialized an Ubuntu server with Wordpress on Vult. I had to uninstall Nginx and remove Certbot due to some issues I encountered. I used

$ sudo apt purge nginx
$ sudo apt purge certbot

After removing all of these, I reinstalled and set secured my domains. Now when I navigate to my URL, I see the Nginx welcome page. I do not have any files in my /etc/nginx/conf.d folder except for default.conf

server {
    server_name  my.domain;

    #access_log  /var/log/nginx/host.access.log  main;

   
    location / {
      root /usr/share/nginx/html;
      index index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/choice.radio/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/choice.radio/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

server {
    if ($host = my.domain) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = my.domain) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen       80;
    server_name  my.domain;
    return 404; # managed by Certbot
}

My wordpress is installed on /var/www/html

I have tried to change the location to:

        location / {
           root /var/www/html;
           index index.php;
         }

But when I visit my domain it downloads the php file instead of serving the website.

I have tried various other file options but Nginx throws an error.

Please help.

Upvotes: 0

Views: 77

Answers (1)

Anant V
Anant V

Reputation: 335

To interpret PHP files with nginx you need to tell nginx to use php-fpm to interprit PHP files.

    #to interprit php files you have to pass it to php. Your can replace php8.2-fpm with your version of php in single decimal place as given
  location ~ \.php$ {
                         include snippets/fastcgi-php.conf;
                         fastcgi_pass unix:/run/php/php8.2-fpm.sock;
            }
   

I have edited your conf. file as per the discussion. Please change the domain name as per your requirement.

server {
    listen 80;

     #http2 - to upgrade http 1.1 to http2 protol for faster connections and load. comment if ssl cert not enabled. 
    listen 443 ssl http2;

    #enter you domain name below. 
    server_name my.domain;
    index index.php ;

    #root folder/public folder of the site.
    root /var/www/html;

   
    

    #access_log  /var/log/nginx/host.access.log  main;

    # Forbidden files or directories.- enhance security
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
    {
        return 404;
    }

     # Directory verification related settings for one-click application for SSL certificate
    location ~ \.well-known{
        allow all;
    }

          # Uncomment lines below to enable browser caching for the following files
          # location ~* ^.+\.(?:css|cur|js|jpe?g|gif|htc|ico|png|html|xml|otf|ttf|eot|woff|woff2|svg)$ {
          #  access_log off;
          #  expires 365d;
          #  add_header Cache-Control public;

        #to interprit php files.
  location ~ \.php$ {
                         include snippets/fastcgi-php.conf;
                         fastcgi_pass unix:/run/php/php8.2-fpm.sock;
            }
   

    #to make SEO friendly url in wordpress
    location /
    {
         try_files $uri $uri/ /index.php?$args;
    }

   



    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
       deny  all;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/choice.radio/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/choice.radio/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

server {
    if ($host = my.domain) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = my.domain) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen       80;
    server_name  my.domain;
    return 404; # managed by Certbot
}

After change please make sure to do sudo service nginx restart && sudo service php8.2-fpm restart .


Please check the comments in conf. Try it. It should solve your problem. In case of issue please leave a commment. :)

Upvotes: 0

Related Questions