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
- Install xcode (for license) and brew package manager (as apt-get or yum in linux)
- Install Nginx
- Install PHP-FPM
- Install MySQL
- 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:
1 |
xcode-select --install |
Homebrew
Homebrew is package manager for OS X.
To install homebrew, type this in terminal:
1 |
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" |
Then we need to check any conflicts:
1 |
brew doctor |
Updates all packages (formulas) with:
1 2 |
brew update brew upgrade |
Nginx
Install default nginx with:
1 |
brew install nginx |
Makes it autostart:
1 2 |
sudo cp /usr/local/opt/nginx/*.plist /Library/LaunchDaemons/ sudo chown root:wheel /Library/LaunchDaemons/homebrew.mxcl.nginx.plist |
Setup virtuals
Create following folders for nginx conf files:
1 2 3 4 5 6 7 8 |
mkdir -p /usr/local/etc/nginx/sites-available mkdir -p /usr/local/etc/nginx/sites-enabled mkdir -p /usr/local/etc/nginx/conf.d mkdir -p /usr/local/etc/nginx/ssl mkdir -p /usr/local/etc/nginx/logs sudo mkdir -p /var/www sudo chown :staff /var/www sudo chmod 775 /var/www |
Use this to start nginx:
1 |
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist |
And this to stop:
1 |
launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist |
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:
1 2 |
rm /usr/local/etc/nginx/nginx.conf curl -L https://gist.github.com/frdmn/7853158/raw/nginx.conf -o /usr/local/etc/nginx/nginx.conf |
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
1 |
curl -L https://gist.github.com/frdmn/7853158/raw/sites-available_default -o /usr/local/etc/nginx/sites-available/default |
Control the services
it’s better and ease to use aliases to start/stop/restart our services.
1 2 |
curl -L https://gist.github.com/frdmn/7853158/raw/bash_aliases -o /tmp/.bash_aliases cat /tmp/.bash_aliases >> ~/.bash_aliases |
Now we can user short commands to control our services instead of typing long launchctl commands.
Nginx
1 2 3 |
nginx.start nginx.stop nginx.restart |
Installing PHP-FPM
We need to tap (register) PHP repository first because homebrew doesn’t come with with formula for PHP-FPM by default:
1 2 |
brew tap homebrew/dupes brew tap homebrew/php |
Now we can search all available PHP formulas:
1 |
brew search php |
It will return very long list of packages.
Next we install PHP:
1 2 |
brew install php71 --without-apache --with-fpm --with-mysql brew install php71-mcrypt |
Start, Stop and Restart php-fpm
1 2 3 |
sudo brew services start php71 sudo brew services stop php71 sudo brew services reload php71 |
Show running processes
1 |
lsof -Pni4 | grep LISTEN | grep php |
Installing MySQL
The next step is to install MySQL:
1 |
brew 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
1 |
secure mysql installation |
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:
1 |
mysql_secure_installation |
> 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
1 |
mysql -u root -p |
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