fbpx
  • Win a Fitbit Inspire HR when you move your hosting to us. Competition ends 30 November 2019

How to configure Virtualmin NGINX for a Laravel application

About Virtualmin NGINX and Laravel

Laravel and NGINX works really nicely together, and if you’re using Virtualmin, you’re in even better hands since you’ll be able to configure multiple sites to be served with ease. To get going you have to make at couple of changes the the default configuration file that is generated by Virtualmin. This is easy if you follow the steps below. The key changes that you will make is that the server root, and fastcgi_param SCRIPT_FILENAME and fastcgi_param DOCUMENT_ROOT locations must be updated to specify your application location.

Additionally you can add all the additional directives as specified on the Laravel website here which handles headers, 404s, and a couple of other things. Our complete example below shows you a fully configured configuration file with additional comments embedded to show what we have done.

In this example, let’s say our domain is called mydomain.com and our application is called helloworld. We’ve deployed helloworld in the following directory on the Virtualmin server:

/home/mydomain.com/helloworld

For this example, the following configuration file listed below should work. It’s a blend between the information on the Laravel documentation website, and the pre-existing configuration file created by Virtualmin. See the screenshot at the end of this example if you simply want to change the three important directory locations.

Example configuration file based on mydomain.com and helloworld:

server {

   # Start Laravel Additions
   add_header X-Frame-Options "SAMEORIGIN";
   add_header X-XSS-Protection "1; mode=block";
   add_header X-Content-Type-Options "nosniff";
   charset utf-8;
   location / {
      try_files $uri $uri/ /index.php?$query_string;
   }
   location = /favicon.ico { access_log off; log_not_found off; }
   location = /robots.txt { access_log off; log_not_found off; }
   error_page 404 /index.php;
   location ~ \.php$ {
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
      include fastcgi_params;
      try_files $uri =404;
      fastcgi_pass unix:/var/php-nginx/15574097604771.sock/socket;
   }
   location ~ /\.(?!well-known).* {
      deny all;
   }
   # End Laravel Additions
   # Now:
   # 1. Comment out the Virtualmin location block
   # 2. Copy the NGINX unique socket ID line in the commented block below try_files $uri = 404;
   # 3. There are three references to public_html, change all three to /home/yourdomain/yourapp/public
   # 4. Replace ip_address with the IP address in the existing Virtualmin configuration

   server_name mydomain.com www.mydomain.com
   listen ip_address;
   root /home/mydomain.com/helloworld/public;
   index index.html index.htm index.php;
   access_log /var/log/virtualmin/mydomain.com_access_log;
   error_log /var/log/virtualmin/mydomain.com_error_log;
   fastcgi_param GATEWAY_INTERFACE CGI/1.1;
   fastcgi_param SERVER_SOFTWARE nginx;
   fastcgi_param QUERY_STRING $query_string;
   fastcgi_param REQUEST_METHOD $request_method;
   fastcgi_param CONTENT_TYPE $content_type;
   fastcgi_param CONTENT_LENGTH $content_length;
   fastcgi_param SCRIPT_FILENAME /home/mydomain.com/helloworld/public$fastcgi_script_name;
   fastcgi_param SCRIPT_NAME $fastcgi_script_name;
   fastcgi_param REQUEST_URI $request_uri;
   fastcgi_param DOCUMENT_URI $document_uri;
   fastcgi_param DOCUMENT_ROOT /home/mydomain.com/helloworld/public;
   fastcgi_param SERVER_PROTOCOL $server_protocol;
   fastcgi_param REMOTE_ADDR $remote_addr;
   fastcgi_param REMOTE_PORT $remote_port;
   fastcgi_param SERVER_ADDR $server_addr;
   fastcgi_param SERVER_PORT $server_port;
   fastcgi_param SERVER_NAME $server_name;
   fastcgi_param HTTPS $https;
   # This section commented out intentionally because it's been moved up to the Laravel specific section
   #location ~ \.php$ {
   # try_files $uri =404;
   # fastcgi_pass unix:/var/php-nginx/15574097604771.sock/socket;
   #}
   listen ip_address:443 ssl;
   ssl_certificate /home/mydomain.com/ssl.combined;
   ssl_certificate_key /home/mydomain.com/ssl.key;
}

If you additionally want all HTTP pages to be served as HTTPS, add another server section below or above this one. The contents of which should be:

server {
   server_name mydomain.com www.mydomain.com
   listen ip_address:80;
   rewrite ^/(.*) https://mydomain.com/$1 permanent;
}

References:
https://laravel.com/docs/5.8/deployment#nginx
https://vander.host/knowledgebase/operating-systems/how-to-automatically-redirect-all-port-80-requests-on-a-nginx-server-to-port-443-ssl/

 

Share this article

Share on facebook
Share on twitter
Share on linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top