How to setup Nginx and PHP7.1 with FPM on Mac OS X without crying 
Tags: nginx, php71, fastcgi, phpfpm
Published: 2017-10-29 23:02:02

---
title: How to setup Nginx and PHP7.1 with FPM on Mac OS X without crying 
published: true
description: Setting up an Nginx server to use php7.1 with php-fpm for executing php scripts.
tags: nginx, php71, fastcgi, php-fpm
---
![alt text](http://media.rehansaeed.com/rehansaeed/2016/08/NGINX.png)

I had problems with my nginx for months. I read a ton of articles online but couldn't find a solution. Most of what I saw was for `php7.0`, and they used `php-fpm-sock file` which clearly does not exist in `php7.1`. Creating and configuring `php7.1` to use the `sock file` was a different kettle of fish on it's own. I failed at it several times. When I upgraded to High Sierra (which basically wiped my system clean due to multiple failures), I had to start setup all over again. I decided to take my time and actually tackle this nginx setup issue. There was still no tutorial on it that worked for me. I made up my mind to figure it out on my own. Did I? This article will explain how I did it. 

If you are new to http servers on your local machine, this article would make setting up nginx pretty easy for you even if you don't understand most of the stuff. If you use LAMP or WAMP or any of that, you might want to go read up on setting up nginx for your OS before coming right here. The nginx configurations would be the same. Just the folders to find the `nginx.config` file for nginx may change from OS to OS. Yes, this should work for non Mac users as well.

To make this article pretty useful to you, I'd skip a lot of stuff that are not the focus (stuff that there are probably like 1,000 articles on already). I'd speed through a lot of this. I use High Sierra on my Mac (I used Sierra before) so I cannot guarantee what would happen for other versions of macOS X. If you are following and anything is missing, do not fret. Just Google how to install it and then continue with this setup.

If you already have Nginx and PHP7.1 installed, you can skip to the installation section and head straight to configuration.

## INSTALLATION

We are going to use `Homebrew` for all our installations. It makes life easy and it is pretty straightforward. [Click here](https://brew.sh/) to read about it or just install right away.
```shell
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
```

When you are done, ensure homebrew's environment is setup correctly.
```shell
$ brew doctor
```

Next we're going to install a launchctl wrapper that will stop, start, and restart any service we install with brew. 
```shell
$ brew tap homebrew/services; brew tap homebrew/dupes
```

Let's install php7.1 with fpm and display the version after installation.
```shell
$ brew install php71 --with-fpm --without-apache; php -v; php-fpm -v
```

Start PHP right away. 
```shell
$ brew services start php71 
``` 

We would not have to setup much for PHP7.1 with FPM because it comes pretty configured right out of the box. Let's just get nginx and get the party going.
```shell
$ brew install nginx; brew services start nginx
```

We are done with installations for now, time for configurations. Nginx should be running on port 8080 if you also have apache installed and running. Either way, you should see which port it is running on after installation, so you can quickly visit `http://localhost:8080` to see the default nginx homepage (use the port displayed in your terminal after setup instead, if it is not 8080).

## THE CONFIGURATION
When installed with homebrew, Nginx’s configuration file would be found in `/usr/local/etc/nginx`. Open it with any editor you like (I use `nano` to save time).

We are going to leave everything the way it is and just edit only a few sections. The `nginx.config` file would be configured properly upon installation to serve static files. All your websites would be in `/usr/local/var/www` folder so that nginx can see them. Nginx mirrors all the files there to it's `html` folder that can be found in `/usr/local/Cellar/nginx/[version_number]/html` as of this writing. If you want to set a different part for your files, edit this block of the config file and set a different `root`.
```nginx
location / {
            root   html;
            index  index.html index.htm index.php;
        }
```
Don't forget to add `index.php` to the `index` list so that nginx can automatically load that file when it serves your site. 

The next thing is to setup how our php files are proxied for execution. Edit your block to match the following. 

```nginx 
  # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        # location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root           html;
            include        fastcgi.conf;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
        }
```
I left the `fastcgi_param` line commented out just incase you need to set something there later. All params are already included below it and the `fastcgi.conf` file should have all the settings required to use `php-fpm`. Save your file and restart nginx.
```shell
$ brew services restart nginx
```

If you are using multiple configuration files (all stored in `/sites-available`), you would have to adjust them one by one as well. You can store this setup block above in a separate file and include it in all of them if they all use the same root folder.

Time to test our beautiful setup. Create an `info.php file` in `/usr/local/var/www` folder and put any `php` code of your choice that should output stuff (I echoed `phpinfo()`). Go to `http://localhost:8080/info.php` and it should display just fine (use the port displayed in your terminal after setup if it is not 8080).

Now buddy, go enjoy your nginx and cry no more.
<hr />

NGINX is a free, open-source, high-performance HTTP server known for its high performance, stability, rich feature set, simple configuration, and low resource consumption. If you want to see how nginx performs in handling requests and heavy usage when compared to apache, read [Web server performance comparison](https://help.dreamhost.com/hc/en-us/articles/215945987-Web-server-performance-comparison). If you would like to understand better about the individual strengths of nginx and apache, checkout [this article](https://www.digitalocean.com/community/tutorials/apache-vs-nginx-practical-considerations) on Digital Ocean.