Installing Nginx + PHP-FPM + MySQL on OS X

Setting up working environment is the first step you need to do. This will be your local web server for programming in OS X with PHP, Nginx and MySQL (also known as LEMP).

Let’s make it as ease as possible 😉

 

What we will do

  1. Install xcode (for license) and brew package manager (as apt-get or yum in linux)
  2. Install Nginx
  3. Install PHP-FPM
  4. Install MySQL
  5. Setup alias to control services

 

 

XCode

First step is to install xcode. It’s mainly need to agree to the license.

Download it from developer.apple.com or iTunes (App Store).

After you finished install, agree to the license.

If this not happen, open terminal (I use iterm2) and install the Xcode Command Line Tools:

 

 

Homebrew

Homebrew is package manager for OS X.

To install homebrew, type this in terminal:

Then we need to check any conflicts:

Updates all packages (formulas) with:

 

 

Nginx

Install default nginx with:

Makes it autostart:

 

Setup virtuals

Create following folders for nginx conf files:

Use this to start nginx:

And this to stop:

 

Changes to nginx.conf file

By default, nginx expects you to define all virtuals in nginx.conf.
But we change it to our structure so we can easily manage virtuals:

Default nginx.conf available as /usr/local/etc/nginx/nginx.conf.default in case you want to restore the default settings.

 

Setup example virtual hosts

 

Control the services

it’s better and ease to use aliases to start/stop/restart our services.

Now we can user short commands to control our services instead of typing long launchctl commands.

Nginx

 

 

Installing PHP-FPM

We need to tap (register) PHP repository first because homebrew doesn’t come with with formula for PHP-FPM by default:

Now we can search all available PHP formulas:

It will return very long list of packages.

 

Next we install PHP:

 

Start, Stop and Restart php-fpm

 

Show running processes

 

 

Installing MySQL

The next step is to install MySQL:

This will install the latest version of MySQL and the command-line tools to connect.

Then we set up the start/stop service, so the MySQL server gets automatically started and stopped when the Mac is shutdown/powered on

 

By default MySQL server installs with no root password but locks it to localhost (so only local users can access it anyway)

To secure our MySQL server, we need to change the root password, remove anonymous users and disable remote root logins:

> Enter current password for root (enter for none):

Press ENTER since you don’t have one set.

> Change the root password? [Y/n]
Confirm using ENTER to accept the suggested default answer (Y) and enter it here in the prompt.

> Remove anonymous users? [Y/n]

Press again ENTER. They are not necessary.

> Disallow root login remotely? [Y/n]

ENTER — No need to log in as root from any other IP than 127.0.0.1.

> Remove test database and access to it? [Y/n]

ENTER — You don’t need the testing tables.

> Reload privilege tables now? [Y/n]

ENTER — Reload the privilege tables to ensure all of the changes made so far will take effect immediately.

Test connection

To ensure that all works fine we can connect to mysql with following

Enter your root password and you should see the MySQL console:

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

mysql>

Since you now know that this works, you can log out and quit the session using \q:

mysql> \q
Bye

Leave a Reply

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

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">