RESOURCES
most of the resources from here, thanks to the blogger frdmn
http://blog.frd.mn/install-nginx-php-fpm-mysql-and-phpmyadmin-on-os-x-mavericks-using-homebrew/
other resources for mariadb instead of mysql
https://mariadb.com/kb/en/mariadb/building-mariadb-on-mac-os-x-using-homebrew/
https://mariadb.com/blog/installing-mariadb-10010-mac-os-x-homebrew
PREREQUISITES
The purpose is to run LEMP on MAC localhost for fast, robust and reliable local development environment
first get the latest Xcode, update from App Store
download Homebrew then check for the conflicts and update brew
$ ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)”
$ brew doctor
$ brew update && brew upgrade
PHP-FPM
Homebrew doesn’t have a default formula for PHP-FPM, so add this
$ brew tap homebrew/dupes
$ brew tap homebrew/php
install php-fpm source code
$ brew install –without-apache –with-fpm –with-mysql php56
Setup PHP CLI binary
$ echo ‘export PATH=”/usr/local/sbin:$PATH”‘ >> ~/.bash_profile
$ . ~/.bash_profile
setup auto start
Create a folder for LaunchAgents and symlink the start/stop service
$ mkdir -p ~/Library/LaunchAgents
$ ln -sfv /usr/local/opt/php56/homebrew.mxcl.php56.plist ~/Library/LaunchAgents/
start PHP-FPM
$ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php56.plist
make sure PHP-FPM is listening on port 9000
$ lsof -Pni4 | grep LISTEN | grep php
php-fpm 50144 msen 6u IPv4 0xacd828b2437ca351 0t0 TCP 127.0.0.1:9000 (LISTEN)
php-fpm 50145 msen 0u IPv4 0xacd828b2437ca351 0t0 TCP 127.0.0.1:9000 (LISTEN)
php-fpm 50146 msen 0u IPv4 0xacd828b2437ca351 0t0 TCP 127.0.0.1:9000 (LISTEN)
php-fpm 50147 msen 0u IPv4 0xacd828b2437ca351 0t0 TCP 127.0.0.1:9000 (LISTEN)
MARIADB
let’s look first what mariadb we can install, we can have 10.1.8 version, then install
$ brew info married
$ brew install mariadb
run the database installer
$ unset TMPDIR
$ cd /usr/local/Cellar/mariadb/10.1.8/
$ mysql_install_db
start MariaDB
$ mysql.server start
Starting MySQL
. SUCCESS!
secure the mariadb installation
$ mysql_secure_installation
root password: blabla
connect to mariadb
$ mysql -u root -p
MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ 3 rows in set (0.00 sec) verify version MariaDB [(none)]> select @@version; or MariaDB [(none)]> select version(); +----------------+ | @@version | +----------------+ | 10.1.8-MariaDB | +----------------+ 1 row in set (0.00 sec) list engines MariaDB [(none)]> show engines; MariaDB [(none)]> exit;
if you need upgrade mariadb (ignore the warning messages such as Xcode is outdated)
$ brew update & brew upgrade
$ brew upgrade mariadb
NGINX
install
$ brew install nginx
setup auto start
since port 80 is used, start nginx as root
$ sudo cp -v /usr/local/opt/nginx/*.plist /Library/LaunchDaemons/
/usr/local/opt/nginx/homebrew.mxcl.nginx.plist -> /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
$ sudo chown root:wheel /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
test web server, start nginx for the first
$ sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
default port 8080 instead of 80, go with 8080 for now
$ curl -IL http://127.0.0.1:8080
HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Thu, 05 Nov 2015 15:36:39 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Sat, 08 Aug 2015 16:10:17 GMT
Connection: keep-alive
ETag: “55c629e9-264”
Accept-Ranges: bytes
stop nginx again
$ sudo launchctl unload /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
NGINX CONFIGURATION
let’s do more nginx configuration
nginx folder is located at /usr/local/etc/nginx/ and nginx.conf file is also there
let’s delete the current nginx.conf, don’t worry the same file is there as nginx.conf.default in case you want to look at
$ rm /usr/local/etc/nginx/nginx.conf
we’ll create a custom lightweight one
$ sudo vi /usr/local/etc/nginx/nginx.conf
worker_processes 1; error_log /usr/local/etc/nginx/logs/error.log debug; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /usr/local/etc/nginx/logs/access.log main; server_names_hash_bucket_size 64; sendfile on; keepalive_timeout 65; index index.php index.htm index.html; include /usr/local/etc/nginx/sites-enabled/*; }
create the following folders which are going to be used in further configuration files
$ mkdir -p /usr/local/etc/nginx/logs
$ 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
CREATE THE FIRST SITE
first site is considered as default folder
$ sudo mkdir -p /var/www/default
$ sudo chown :staff /var/www/default
$ sudo chmod 775 /var/www/default
create .info file
$ sudo vi /var/www/default/.info.php
create index.php file
$ sudo vi /var/www/default/index.php
you can create 403.html, 404.html or even index.html files by yourself
Let’s add first site’s config file
$ sudo vi /usr/local/etc/nginx/sites-available/default
you see that we also added phpmyadmin, we’ll install phpmyadmin very soon!
server { listen 80; server_name localhost; root /var/www/default; access_log /usr/local/etc/nginx/logs/default.access.log main; location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location = /info { allow 127.0.0.1; deny all; rewrite (.*) /.info.php; } # phpmyadmin location ~ ^/phpmyadmin/(.+\.php)$ { try_files $uri =404; fastcgi_intercept_errors on; root /usr/local/share/; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi_params; } error_page 404 /404.html; error_page 403 /403.html; }
Let’s symlink the virtual default host into the sites-enabled folder
$ ln -sfv /usr/local/etc/nginx/sites-available/default /usr/local/etc/nginx/sites-enabled/default
start nginx
$ sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
http://localhost
PHPMYADMIN
before testing the site
first install autoconf
$ brew install autoconf
set $PHP_AUTOCONF
$ echo ‘PHP_AUTOCONF=”‘$(which autoconf)'”‘ >> ~/.bash_profile && . ~/.bash_profile
finally install phpmyadmin
$ brew install phpmyadmin
to get rid off blowfish secret passphrase warning
$ sudo vi /usr/local/etc/phpmyadmin.config.inc.php
$cfg[‘blowfish_secret’] = ‘qtdRoGmbc9{8IZr323xUbSN]0s)r$9b_JUnb{~Xz’; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */
symlink phpmyadmin to the default folder
$ sudo ln -s /usr/local/share/phpmyadmin /var/www/default
TEST SITES
let’s show both the site and phpmyadmin, restart nginx
$ sudo launchctl unload /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
$ sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
http://localhost
http://localhost/phpmyadmin
create index.php file
$ sudo vi /var/www/api/index.php
CREATE ADDITIONAL SITE
It’s now very easy to add additional site because the infrastructure is ready!
create a site called api
$ sudo mkdir -p /var/www/api
$ sudo chown :staff /var/www/api
$ sudo chmod 775 /var/www/api
create the config from the existing default config
$ cp /usr/local/etc/nginx/sites-available/default /usr/local/etc/nginx/sites-available/api
$ sudo vi /usr/local/etc/nginx/sites-available/api
remove phpmyadmin and info because we already have them under default and change the root
server { listen 80; server_name api; root /var/www/api; index index.php inde.html index.htm; access_log /usr/local/etc/nginx/logs/default.access.log main; location ~ \.php$ { 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; }
symlink api folder to sites-enabled
$ sudo ln -sfv /usr/local/etc/nginx/sites-available/api /usr/local/etc/nginx/sites-enabled/api
now edit hosts file
$ sudo vi /etc/hosts
127.0.0.1 api
restart nginx
$ sudo launchctl unload /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
$ sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
YEEYYYY!!!
CONTROL SERVICES
let’s setup some aliases to simplify the service commands
$ curl -L https://gist.github.com/frdmn/7853158/raw/bash_aliases -o /tmp/.bash_aliases
$ cat /tmp/.bash_aliases >> ~/.bash_aliases
$ echo “source ~/.bash_aliases” >> ~/.bash_profile && . ~/.bash_profile
reload the shell
$ source ~/.bash_profile
the https://gist.github.com/frdmn/7853158/raw/bash_aliases looks like:
alias nginx.start='sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.nginx.plist' alias nginx.stop='sudo launchctl unload /Library/LaunchDaemons/homebrew.mxcl.nginx.plist' alias nginx.restart='nginx.stop && nginx.start' alias php-fpm.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php56.plist" alias php-fpm.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.php56.plist" alias php-fpm.restart='php-fpm.stop && php-fpm.start' alias mysql.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist" alias mysql.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist" alias mysql.restart='mysql.stop && mysql.start' alias nginx.logs.error='tail -250f /usr/local/etc/nginx/logs/error.log' alias nginx.logs.access='tail -250f /usr/local/etc/nginx/logs/access.log' alias nginx.logs.default.access='tail -250f /usr/local/etc/nginx/logs/default.access.log' alias nginx.logs.default-ssl.access='tail -250f /usr/local/etc/nginx/logs/default-ssl.access.log' alias nginx.logs.phpmyadmin.error='tail -250f /usr/local/etc/nginx/logs/phpmyadmin.error.log' alias nginx.logs.phpmyadmin.access='tail -250f /usr/local/etc/nginx/logs/phpmyadmin.access.log'
Now you can use short aliases instead of long launchtl arguments
$ nginx.start
$ nginx.stop
$ nginx.restart
to quickly tail the latest error or access logs:
$ nginx.logs.access
$ nginx.logs.default.access
$ nginx.logs.phpmyadmin.access
$ nginx.logs.default-ssl.access
$ nginx.logs.error
$ nginx.logs.phpmyadmin.error
check nginx config
$ sudo nginx -t
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: [warn] 1024 worker_connections exceed open file resource limit: 256
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
so we decrease the resource limit to 256
$ sudo vi /usr/local/etc/nginx/nginx.conf
events { worker_connections 256; }
recheck nginx config
$ sudo nginx -t
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
php-fpm
$ php-fpm.start
$ php-fpm.stop
$ php-fpm.restart
check php-fpm config
$ php-fpm -t
[05-Nov-2015 12:28:55] NOTICE: configuration file /usr/local/etc/php/5.6/php-fpm.conf test is successful
PHP.INI
in case you need to update php.ini file
$ sudo vi /usr/local/etc/php/5.6/php.ini
brew install –without-apache –with-fpm –with-mysql php56
Error: No available formula with the name “–without-apache”
==> Searching for similarly named formulae…
Error: No similarly named formulae found.
==> Searching taps…
Error: No formulae found in taps.
LikeLike
brew install –without-apache –with-fpm –with-mysql php56
LikeLike
It should be with 2 dashes.. sorry for the syntax errors..
brew install –without-apache –with-fpm –with-mysql php56
LikeLike
brew install –-without-apache -–with-fpm -–with-mysql php56
Error: No available formula with the name “–-without-apache”
==> Searching for similarly named formulae…
Error: No similarly named formulae found.
==> Searching taps…
Error: No formulae found in taps.
LikeLike
What does it say when u remove it?
LikeLike
I’m getting a 404 Not Found on my site. It seems like yii doesn’t resolve the url correct. http://localhost/site/login
I tried localhost?r=site/login. this rewrites it to the first url.
any suggestions?
LikeLike