Nginx For Mac Os X



If you want to start developing PHP applications, or merely work on your PHP-based site off-line, on Mac OS X you can easily do so. In this how-to we'll see how you can set up NginX, a high performance web server, with the PHP version shipped with Mac OS X itself to create a local web server. In case you're wondering, you can of course use it in parallel with MAMP, XAMPP or even the multi-PHP version server I've described in an earlier post.

Install HomeBrew

Nginx For Mac Os X

If you don't have HomeBrew already you need to install it per its instructions. HomeBrew is a package manager for Mac OS X which lets you install a lot of software without hunting around the Internet for a suitable package file.

Compile nginx with openssl on Mac OS X 64-bit Print Email Details Written by Chau Hong Linh Category: Computer Science. Mac OS X's kernel is based on Mach and Open BSD, which is a Unix-based system. It used to be very convenient to compile native C code on Mac OS X, until Snow Leopard. From Snow Leopard until now, it becomes worse and worse. Installing NGINX on Mac. There are following steps to install the Nginx on Mac OS: Step 1: Download Homebrew. To install the Nginx on Mac OS, Homebrew must be installed on the system. Homebrew is a package manager for Mac operating system that allows us to install various Unix applications easily. Heavily borrowed from: Nginx configuration for Mac OS X with Homebrew, using sites-enabled directory. Install nginx brew install nginx cd /usr/local/etc/nginx/ git clone git@bitbucket.org:fountainhead/nginx-sites-available sites-available wget ". Now let’s have a look how to install NGINX, PHP and MySQL on Mac OS local environment. Continue reading “How to Install NGINX, PHP and MySQL on Mac OS” Author Chatura Halwatura Posted on December 4, 2017 December 4, 2017 Categories Mac OS X, PHP Tags MySQL, NGINX, PHP 1 Comment on How to Install NGINX, PHP and MySQL on Mac OS.

Install NginX

Open a Terminal window and enter the following command:

After a couple of minutes it should tell you that the installation is complete. Now we need to create a few links to let us easily configure NginX and start it automatically.

You can configure NginX by editing the files inside the /usr/local/etc/nginx directory.

First, we need to edit the /etc/nginx/nginx.conf file because the default is a bit problematic. From a Terminal window type

and replace the contents of the file with

Change yourusername (shown in bold red letters) with your Mac OS X username. If you don't know what it is type the following in a Terminal window

The system will reply with your username.

At this point it should be noted that the web server root is set up to be the Sites folder inside your user's directory. If you are not sure whether you have this folder, type the following in a Terminal window

Starting, stopping and reloading NginX

We need to first tell Mac OS X to automatically start NginX whenever we boot the computer.

Please note that the 1.8.0 you see in the following instructions is the version number of NginX I installed at the time of this writing. Looks inside /usr/local/Cellar/nginx to see what is your actual version. Just open Finder, press ⇧⌘G type /usr/local/Cellar/nginx and press ⏎.

Assuming that the version is 1.8.0, you need to type the following into a Terminal window

Nginx Mac Download

The very first time you install NginX you need to start it manually. You can do that by typing the following into a Terminal window

Stopping NginX requires typing the following into a Terminal window

You can restart it using the same command you used to start it the very first time.

After editing NginX' configuration, or the configuration of one of the server blocks (sites) you need to tell NginX to reload the configuration. You can do that by typing the following into a Terminal window

Set up PHP-FPM

At this point your NginX server isn't very useful. It can only serve static content. We need to tell it how to run PHP scripts. Unlike Apache, there is no PHP module for NginX. Instead, it uses PHP-FPM (FastCGI Process Manager) which is just as fast and much more secure. We just need to enable it on your Mac. Type the following in a Terminal window

You can edit /etc/php.ini to taste. Please note that since this is a system file you need to open it for editing from a Terminal window by typing

Now we need to edit the file /private/etc/php-fpm.conf and make some changes. Start by opening the file for editing by typing this to a Terminal window

Nginx for mac os x 10.13

Find the lines starting with pid = and error_log = and change them to

Next up, find the user = and group = lines and change them to read

Remember to change yourusername to your actual username, the same way you did for nginx.conf.

Finally, paste the following line at the bottom of the file

Now let's tell Mac OS X to load PHP-FPM automatically. From a Terminal window enter

sudo open /Library/LaunchDaemons/net.php.php-fpm.plist -a TextEdit

Paste the following contents into this file:

Now launch PHP-FPM with the following Terminal command

You only need to do that the very first time. From now on PHP-FPM will auto-start on boot.

Testing it all works

Create a new file called phpinfo.php inside the Sites directory under your user directory. A simple way to do that:

and enter the following contents:

Now visit this file from your browser at http://localhost:8080/phpinfo.php If all went according to the plan you should see a page stating the PHP version installed on your Mac along with its configuration info. Swell!

Creating server blocks (virtual hosts)

Doing development on subdirectories is very problematic if you intend to move your site into the domain's root later on. My personal favorite is creating one subdomain per site I'm working on locally using the non-existent local.web domain name to make sure I don't accidentally screw up a live site.

Let me show you how simple it is to create a new server block (virtual host) in NginX. In our example we'll create a new site which will be accessible under http://example.local.web:8080

First we need to create a new file with a .conf extension inside the /usr/local/etc/nginx/servers directory. There is no requirement for the file name but it's a good convention to use the site's subdomain and domain name. It will help you 5-6 months later when you won't remember what you've set up, why and how. So let's name our file example.local.web.conf. From a Terminal window:

Paste the following contents into the file:

The things you need to change are:

  • example.local.web. This is the name of your subdomain. It can be anything you want.
  • yourusername. Replace it with your username as we already did when setting up NginX and PHP-FPM
  • example. This is the subdirectory inside the Sites folder where the site files live, i.e. the site's root. The convention I use is that the directory name must be the same as the sudomain it's serving. Don't make yourself think!

The thing is that Mac OS X currently doesn't know that the domain example.local.web should be served by our local server. We can edit the file /etc/hosts and tell it so. From a Terminal window:

Add the following line at the end of the file

Finally, we need to tell NginX to reload the configuration so it knows how to serve our site. From a Terminal window:

That's it! You can now access your dev site from a browser typing http://example.local.web:8080/

Please note that it's best to close and re-open your browser, otherwise the browser thread may have not picked up the change in the /etc/hosts file and reply that the server was not found.

Restart

What if I don't like port 8080?

No problem at all. Just change the line

in your NginX configuration files to reflect the port you want it to listen to. For example, to make it listen to the default HTTP port (which means that you can access the server as http://localhost instead of http://localhost:8080) change the line to

What about SEF URLs (Joomla!), permalinks (WordPress), routes (Drupal) and so on?

This is something you need to add to your server block files, i.e. the files inside the /usr/local/etc/nginx/servers directory. Typically it's something like this:

and MUST be placed BEFORE the location ~ .php$ line. That said it's best to consult the NginX and your site script's documentation for the 'right way' to set it up for use with NginX. Some quick and useful links:

Nginx Mac Os X Yosemite

That's all folks! Enjoy your high performance local server!

nginx

Basic HTTP server features
Other HTTP server features
Mail proxy server features
TCP/UDP proxy server features
Architecture and scalability
Tested OS and platforms

nginx [engine x] is an HTTP and reverse proxy server,a mail proxy server,and a generic TCP/UDP proxy server,originally written by Igor Sysoev.For a long time, it has been runningon many heavily loaded Russian sites includingYandex,Mail.Ru,VK, andRambler.According to Netcraft, nginx served or proxied25.76%busiest sites in September 2020.Here are some of the success stories:Dropbox,Netflix,Wordpress.com,FastMail.FM.

The sources and documentation are distributed under the2-clause BSD-like license.

Commercial support is available fromNginx, Inc.

Basic HTTP server features

  • Serving static andindexfiles,autoindexing;open file descriptor cache;
  • Acceleratedreverse proxying with caching;load balancingand fault tolerance;
  • Accelerated support with caching ofFastCGI,uwsgi,SCGI, andmemcachedservers;load balancingand fault tolerance;
  • Modular architecture.Filters includegzipping,byte ranges, chunked responses,XSLT,SSI,and imagetransformation filter.Multiple SSI inclusions within a single page can be processed inparallel if they are handled by proxied or FastCGI/uwsgi/SCGI servers;
  • SSL andTLS SNI support;
  • Support for HTTP/2with weighted and dependency-based prioritization.

Other HTTP server features

Download Nginx For Mac Os X

  • Name-based and IP-basedvirtual servers;
  • Keep-aliveand pipelined connections support;
  • Accesslog formats,bufferedlog writing,fast log rotation, andsyslog logging;
  • 3xx-5xx error codesredirection;
  • The rewrite module:URI changingusing regular expressions;
  • Executingdifferent functions depending on theclient address;
  • Access control based onclient IP address,by password (HTTPBasic authentication) and by theresult ofsubrequest;
  • Validation ofHTTP referer;
  • The PUT, DELETE, MKCOL, COPY,and MOVE methods;
  • FLVandMP4streaming;
  • Response rate limiting;
  • Limiting the number of simultaneousconnectionsorrequestscoming from one address;
  • IP-based geolocation;
  • A/B testing;
  • Request mirroring;
  • Embedded Perl;
  • njs scripting language.

Mail proxy server features

  • User redirection toIMAPorPOP3server using an external HTTPauthenticationserver;
  • User authentication using an external HTTPauthenticationserver and connection redirection to an internalSMTP server;
  • Authentication methods:
    • POP3:USER/PASS, APOP, AUTH LOGIN/PLAIN/CRAM-MD5;
    • IMAP:LOGIN, AUTH LOGIN/PLAIN/CRAM-MD5;
    • SMTP:AUTH LOGIN/PLAIN/CRAM-MD5;
  • SSL support;
  • STARTTLSand STLS support.

TCP/UDP proxy server features

Nginx For Mac Os X
  • Generic proxyingof TCP and UDP;
  • SSL andTLS SNI supportfor TCP;
  • Load balancingand fault tolerance;
  • Access control based onclient address;
  • Executing different functions depending on theclient address;
  • Limiting the number of simultaneousconnectionscoming from one address;
  • Accesslog formats,bufferedlog writing,fast log rotation, andsyslog logging;
  • IP-based geolocation;
  • A/B testing;
  • njs scripting language.

Architecture and scalability

  • One master and several worker processes;worker processes run under an unprivileged user;
  • Flexible configuration;
  • Reconfigurationand upgrade of anexecutable without interruption of the client servicing;
  • Support forkqueue (FreeBSD 4.1+),epoll (Linux 2.6+),/dev/poll (Solaris 7 11/99+), event ports (Solaris 10),select, and poll;
  • The support of the various kqueue features including EV_CLEAR, EV_DISABLE(to temporarily disable events), NOTE_LOWAT, EV_EOF, number of available data,error codes;
  • The support of various epoll features includingEPOLLRDHUP (Linux 2.6.17+, glibc 2.8+) andEPOLLEXCLUSIVE (Linux 4.5+, glibc 2.24+);
  • sendfile (FreeBSD 3.1+, Linux 2.2+, macOS 10.5+), sendfile64 (Linux 2.4.21+),and sendfilev (Solaris 8 7/01+) support;
  • File AIO(FreeBSD 4.3+, Linux 2.6.22+);
  • DIRECTIO(FreeBSD 4.4+, Linux 2.4+, Solaris 2.6+, macOS);
  • Accept-filters (FreeBSD 4.1+, NetBSD 5.0+) and TCP_DEFER_ACCEPT (Linux 2.4+)support;
  • 10,000 inactive HTTP keep-alive connections take about 2.5M memory;
  • Data copy operations are kept to a minimum.

Tested OS and platforms

  • FreeBSD 3 — 12 / i386;FreeBSD 5 — 12 / amd64;FreeBSD 11 / ppc;FreeBSD 12 / ppc64;
  • Linux 2.2 — 4 / i386;Linux 2.6 — 5 / amd64;Linux 3 — 4 / armv6l, armv7l, aarch64, ppc64le;
  • Solaris 9 / i386, sun4u;Solaris 10 / i386, amd64, sun4v;Solaris 11 / x86;
  • AIX 7.1 / powerpc;
  • HP-UX 11.31 / ia64;
  • macOS / ppc, i386, x86_64;
  • Windows XP,Windows Server 2003,Windows 7,Windows 10.