How to setup Nginx on Mac - Install Nginx, Setup nginx virtualhost, Nginx php server

0

This installation we will use Homebrew to install nginx

brew install nginx
After done you may have 2 options to use Nginx server :

1. Localhost

just run sudo nginx and you will get default local server with port 8080 http://localhost:8080

- To change port

nano /usr/local/etc/nginx/nginx.conf

You will see the content inside and it's about content below 

server {
listen 8080;
server_name localhost;

#access_log logs/host.access.log main;

location / {
root html;
index index.html index.htm;
}


So just change Listen 8080 -> listen 80

- To change default directory 

Change root html; -> root {your directory that content index file};
Change index index.html index.htm; -> index {it's depend on your file name or extension Example : index.html or index.php};

- To setup PHP server

Check how to install PHP first Install PHP on Mac
Follow example below

server {

        listen       80;

        server_name  localhost;


        root /Users/myname/myphp;

        index index.php;


         location / {

                # First attempt to serve request as file, then

                # as directory, then fall back to displaying a 404.

                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_pass   127.0.0.1:9000;

            fastcgi_index  index.php;

            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

            fastcgi_param  SCRIPT_FILENAME $request_filename;

            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;

            include        fastcgi_params;


        }


        location ~ /\.(?!well-known).* {

             deny all;

        }

     }


After all done restart Nginx 
brew services restart nginx

2. Virtualhost

Add host name to hosts file 
nano /etc/hosts
add your domain name
127.0.0.1      your_domain.local

Refresh hosts file
OSX 10.4 and below: lookupd -flushcache
OSX 10.5 + 10.6: dscacheutil -flushcache
OSX 10.7 + 10.8: sudo killall -HUP mDNSResponder
OSX 10.9 and above: sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
OSX 10.12 and above: sudo dscacheutil -flushcache

nano /usr/local/etc/nginx/nginx.conf and add another server {} if you have many virtual host just add it inside 1 server{} equal 1 server or host.

Check how to install PHP first Install PHP on Mac

Follow example below for PHP if no PHP you can follow example 1. Localhost

server {

        listen       80;

        server_name  your_domain.local;


        root /Users/myname/myphp;

        index index.php;


         location / {

                # First attempt to serve request as file, then

                # as directory, then fall back to displaying a 404.

                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_pass   127.0.0.1:9000;

            fastcgi_index  index.php;

            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

            fastcgi_param  SCRIPT_FILENAME $request_filename;

            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;

            include        fastcgi_params;


        }


        location ~ /\.(?!well-known).* {

             deny all;

        }

     }


Notice : fastcgi_pass   127.0.0.1:9000; Is the way to use which PHP version if you have another port/version should check it properly (Install PHP on Mac) unless you will face error.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)

Random Posts