Vala Khosravi
Vala Khosravi

Reputation: 2570

How can I serve Laravel project on nginx on macOS?

I created Laravel project with sudo composer create-project laravel/laravel MyProject --prefer-dist command in "/Users/vala/Documents/laravel/" directory. Then I ran these commands:

sudo chown -R _www:_www /Users/vala/Documents/laravel/MyProject/
sudo chmod -R 755 /Users/vala/Documents/laravel/MyProject/

Then I create a file called "laravel" in "/usr/local/etc/nginx/sites-available/" and write text below in it:

server {
  listen 80;
  listen [::]:80;
  root /Users/vala/Documents/laravel/MyProject/public;
  index  index.php index.html index.htm;
  server_name  mylaravelproject.com;

  location / {
    try_files $uri $uri/ /index.php?$query_string;        
  }


  location ~ \.php$ {
     try_files $uri =404;
     fastcgi_split_path_info  ^(.+\.php)(/.+)$;
     fastcgi_index            index.php;
     fastcgi_pass             unix:/var/run/php/php7.1-fpm.sock;
     include                  fastcgi_params;
     fastcgi_param   PATH_INFO       $fastcgi_path_info;
     fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }

}

Then I ran sudo ln -s /usr/local/etc/nginx/sites-available/laravel /usr/local/etc/nginx/sites-enabled/ and after that I ran these commands:

launchctl unload /usr/local/cellar/nginx/1.17.8/homebrew.mxcl.nginx.plist
launchctl load /usr/local/cellar/nginx/1.17.8/homebrew.mxcl.nginx.plist

But when I open "mylaravelproject.com" on my browser it shows This site can’t be reached.

I'm using MySQL database.

What did I miss?

Upvotes: 3

Views: 4614

Answers (2)

pradeep
pradeep

Reputation: 592

Its possible to run a laravel project in Mac NginX, but we need to write a bit of code to make the laravel's url rewrite functions. Here is the nginx conf file for your reference.

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
sendfile        on;
keepalive_timeout  65;

server {
    listen       8080;
    server_name  localhost;
    root /Users/vala/Documents/laravel/MyProject/public;

    index index.html index.htm index.php;

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

    access_log off;
    error_log  /var/log/nginx/myapp-error.log error;

    sendfile off;

    client_max_body_size 100m;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME fastcgi_script_name;
        fastcgi_intercept_errors off;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
    }

    location ~ /\.ht {
        deny all;
    }
}

include servers/*;
}

then restart the nginx server.

nginx -s reload

Upvotes: 1

AJ Xu
AJ Xu

Reputation: 1

Manually setting up a local environment can be time consuming. To help you clarify your question here is a list of considerations:

  1. Have you setup php-fpm service? Is it running?
  2. Have you added redirection in /etc/hosts file for your example.com?
  3. What database will you be running? Is it installed and running?

My suggestion is to use Docker with Laradock as it is simple to use and you can choose to use Laradock in production to sync up both development and production environments, official page is here laradock.io

Upvotes: -1

Related Questions