0

I'm in the process of building a LEMP stack server and local development environment, largely following the tutorials that both Linode and DigitalOcean provide. I have installed all the disparate components, but I'm a bit confused about configuring NGINX to handle PHP. The tutorials describe editing a server {} block found in sites-available/default. The thing is, my default configuration file has two server blocks, and I'm not sure which one I should edit.

The first contains:

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;

   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 the PHP scripts to FastCGI server listening on 127.0.0.1:9000
   #
   #location ~ \.php$ {
   #   include snippets/fastcgi-php.conf;
   #
   #   # With php7.0-cgi alone:
   #   fastcgi_pass 127.0.0.1:9000;
   #   # With php7.0-fpm:
   #   fastcgi_pass unix:/run/php/php7.0-fpm.sock;
   #}

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

While the second contains:

# 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;
#   }
#}

Which should I edit? I will be working on/hosting multiple virtual hosted sites, if that makes a difference.

  • You should create a new `server` block in a new file, as the comment instructed. – Michael Hampton Mar 02 '17 at 21:52
  • 1
    You might find this Nginx tutorial I wrote helpful, it has downloadable config files which you might learn from : https://www.photographerstechsupport.com/tutorials/hosting-wordpress-on-aws-tutorial-pt1-introduction-configuration-downloads/ – Tim Mar 02 '17 at 22:01

1 Answers1

1

That are just both examples

First one is for setup default server, whit that you can handle all domains poitent to your server, so is a default vhost for domains whitout vhost.

Second one is for setup one domain "example.com"

You need create new file in sites-available/yourdomain.com

Maybe this can help you to start, this is basic configuration.

You need create user "yourdomain" for php-fpm config ( or any order user )

server {
        listen 80;
        server_name yourdomain.com;
        root /home/yourdomain;
        index index.html index.htm;
        autoindex off;

###
        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
                fastcgi_pass unix:/var/run/yourdomain.sock;
                fastcgi_index index.php;
               fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include fastcgi_params;
        }

}

And for php-fpm

[yourdomain]

listen = /var/run/yourdomain.sock
listen.owner = yourdomain
listen.group = yourdomain
listen.mode = 0660

user = yourdomain
group = yourdomain

pm = dynamic
pm.max_children = 10
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 5
pm.max_requests = 0

chdir = /

You also can satart whit official docs

https://www.nginx.com/resources/wiki/start/topics/examples/phpfcgi/

Skamasle
  • 422
  • 2
  • 10