Nginx configuration for Laravel in a different port on CentOS 7

Nginx configuration for Laravel in a different port on CentOS 7

I have a DigitalOcean droplet for testing purposes and I usually deploy static websites, Node.js APIs, and Laravel projects. So in order to get Laravel and the Node.js servers working in the same droplet, I need to set a different port for each one using Nginx and FirewallD to open the needed port.

The following Nginx server block works with any Laravel version and it’s been used under CentOS 6.x and 7.x

server {
  listen 2500;
  listen [::]:2500;

  server_name my_project.com;
  client_max_body_size 4m;

  root /var/www/my_project/public;
    index  index.php;

  access_log /var/log/nginx/my_project.access.log;
  error_log /var/log/nginx/my_project.error.log;

  location / {

    root  /var/www/my_project/public;
    try_files $uri $uri/ /index.php$is_args$args;
    index  index.html index.htm index.php;
    
  }
  
  location ~ \.php$ {

    root  /var/www/my_project/public;
    fastcgi_index index.php;
    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    try_files $uri $uri/ index.php /index.php$is_args$args;
    include /etc/nginx/fastcgi_params;
    fastcgi_pass 127.0.0.1:9000;

    fastcgi_param SCRIPT_FILENAME $request_filename;
    fastcgi_param APP_ENV dev;

  }
  
  sendfile off;
}
my_project.conf
  • Create a new .conf file.
nano /etc/nginx/conf.d/my_project.conf
  • Paste the above server block code and save it with Ctrl + X
  • Reload and restart the Nginx server.
service nginx reload && service nginx restart