How to configure PHP with Nginx webserver on Mac OS X

Published by Igor Khrupin on

Before starting installation PHP-FPM and configurations you should have Nginx installed and configured. If you don’t have it ready please follow my previous post about installing and configuring Nginx on Mac OS X. Here is the link https://www.hrupin.com/2017/11/how-to-install-nginx-webserver-on-mac-os-x

So, here we have installed and configured NGinx. Let’s install and configure PHP-FPM. Here is step by step instructions

  • Install php71 using Homebrew
    brew tap homebrew/dupes
    brew tap homebrew/php
    brew search php
    brew install php71 --without-apache --with-fpm
  • Setup automatic launch php after mac start
    mkdir -p ~/Library/LaunchAgents
    cp /usr/local/opt/php71/homebrew.mxcl.php71.plist ~/Library/LaunchAgents/
    launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php71.plist
  • Edit /usr/local/etc/php/7.1/php.ini and replace
    ;cgi.fix_pathinfo=1

    with next string

    cgi.fix_pathinfo=0
  • Edit /usr/local/etc/php/7.1/php-fpm.d/www.conf file and put there your username and group. You need find next lines and edit them like next. Please replace YOUR_USERNAME with your username and YOUR_GROUP with name of your group
    user = YOUR_USERNAME
    group = YOUR_GROUP
  • Next we need edit default virtualhost and add make it look like this
    server {
        listen   80; ## listen for ipv4; this line is default and implied
        listen   [::]:80 default_server ipv6only=on; ## listen for ipv6
    
        server_name  localhost;
        
        root       /var/www/localhost;
    
        index index.php index.html index.htm;
     
        access_log  /usr/local/etc/nginx/logs/default.access.log;
        error_log  /usr/local/etc/nginx/logs/default.error.log;
     
        location ~ \.php$ {
            try_files      $uri = 404;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    
        error_page  404     /404.html;
        error_page  403     /403.html;
    }
  • Install mcrypt
    sudo brew install php71-mcrypt
  • Prepare test web content. Create file  /var/www/localhost/info.php with next content
    <?php
        phpinfo();
    ?>
  • Save all files and restart PHP
    sudo brew services stop php71
    sudo brew services start php71
  • Restart Nginx
    sudo nginx -s stop
    sudo nginx
  • Check how Nginx works
    curl -IL http://localhost/info.php
  • Output should be like this
    HTTP/1.1 200 OK
    Server: nginx/1.12.1
    Date: Sat, 11 Nov 2017 22:01:37 GMT
    Content-Type: text/html; charset=UTF-8
    Connection: keep-alive
    X-Powered-By: PHP/7.1.11
  • Also you can run http://localhost/info.php in your browser and you should see phpinfo table like this

Now Nginx able to work with PHP


0 Comments

Leave a Reply

Avatar placeholder

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.