Nginx For Mac Os

Posted onby

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. Dec 04, 2017 My MacBook Pro installed Mac OS High Sierra and it was shipped with pre-installed PHP package. So, depending on your web development environment setup, you may need to upgrade default PHP version to the latest version. Normally, Mac OS X doesn’t ship with its own copy of MySQL. So, you will have to install MySQL on your local machine.

Seeking a satisfactory solution to create a local web server for programming in macOS with PHP and MySQL, I was disappointed that the turnkey solutions were far from equaling the WAMP that may exist on Windows.

Nginx For Mac Os

But I forgot macOS is a Unix system, and unlike Windows, it’s perfectly possible to create a customized local server with some packages.

We will see how to install Nginx, PHP-FPM and MariaDB (MySQL) on macOS High Sierra thanks to Homebrew package manager.

Homebrew

HomeBrew is a package manager for macOS, that allows to easily install various Unix applications.

To install, simply execute the command shown on the official website:

If you do not already have them, macOS will prompt you to first install Xcode Command Line Tools.

Nginx

Although Apache is natively included with macOS, we propose here to install Nginx, particularly lightweight and easily configurable.

Installation

To install and launch Nginx on startup, we use:

Although we musn’t use sudo with brew install, it is necessary to use it to launch Nginx if we want to use the the default port 80.

Configuration

We want to store our web site in the folder of our choice, and access to the URL http://localhost/. To do this, edit the configuration file:

To begin, we will have to give to Nginx the permission to access our files and avoid a nasty 403 Forbidden error. To do so, we change the first line, where <user> is your username:

Nginx For Mac Os High Sierra

Then, to add a new website, we will add a new section inside the http directive:

We then restart Nginx in order to take this changes into account:

PHP

In order to use PHP with Nginx we will use PHP-FPM.Here, we will use PHP 7.2, but you can easily choose any other version:

Then, we re-edit the configuration file:

Mac Os Download

We modify the line starting with index by:

Finally, add in the section server the following lines to run PHP for all files with the extension .php:

Nginx For Mac Os 10.13

To avoid a File not found. error, we also need to give the right permissions to PHP. In the following file:

Mac

Change the following parameter to:

At last, we restart Nginx to activate the changes, and we don’t forget to launch PHP, to avoid a 502 Bad Gateway:

MySQL

Nginx For Mac Os Versions

We will now install and launch MariaDB:

Finally, complete the installation by choosing a root password for MySQL:

Here is the perfect MAMP installation !

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 with each new version of Mac OS X. Yosemite marked the worst debacle of Mac OS X to date, but in term of compiling C native code, El Capitan and macSierra are not much better either. It looks like Tim Cook tries to alienate all professional developers from using Mac.

I really have hard time when I compile nginx with openssl on Mac OS X, both El Capitan and macSierra. So I document it here.

Download the latest file from nginx site: http://nginx.org/en/download.html

wget http://nginx.org/download/nginx-1.11.10.tar.gz

Extract the file:

tar -xzvf nginx-1.11.10.tar.gz

After you down load nginx, you also need to download openssl source from https://www.openssl.org/source/, extract the file, build openssl, and keep the source directory for further use, when you build nginx.

Nginx For Mac Os

The nginx compiling process expect that there will be 'include' and 'lib' with include files and lib files in <OPENSSL SOURCE>/.openssl

You have to manually create the directory '.openssl' there, and copy the needed files from '/opt/openssl' that you already build above.

Go into the directory where you just extract the file, run the following command:

Then you are ready to run nginx from /opt/nginx/sbin

The command 'make' with the option '-stdlib=libstdc++' compiles and links nginx to correct platform target.