Skip to content

Instantly share code, notes, and snippets.

@laptrinhcomvn
Last active January 11, 2022 19:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save laptrinhcomvn/d1f45fb3263a9f8197daa6efd40f4bd5 to your computer and use it in GitHub Desktop.
Save laptrinhcomvn/d1f45fb3263a9f8197daa6efd40f4bd5 to your computer and use it in GitHub Desktop.
Set up on Mac OS X El Capitan: Nginx - PHP 5.6.x

Mac OS X El Capitan setup Nginx - PHP 5.6.x, MySQL 5.7.x, phpMyAdmin

1 Install homebrew

Run this command to install homebrew at system level

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

1.1 Homebrew libs

Run bellow command and check if have any thing need to be fixed.

sudo chown -R $(whoami) /usr/local
brew doctor

2 Install nginx

Command to install nginx via brew.

brew install nginx

3 Install PHP 5.6

3.1 Install PHP

To install PHP v 5.6.x (script is from http://php-osx.liip.ch)

curl -s http://php-osx.liip.ch/install.sh | bash -s 5.6 

3.2 Enable new PHP 5.6 at system

nano ~/.bash_profile

Enter:

export PATH="/usr/local/php5/bin:/usr/local/bin:$PATH"

Disable apple builtin old php-fpm version

sudo mv /usr/sbin/php-fpm /usr/sbin/php-fpm.old

Create symlink for new version of php-fpm

sudo ln -s /usr/local/php5/sbin/php-fpm /usr/local/bin/

3.3 Install composer

curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

Maybe need to update .bash_profile to add new line

alias composer="php /usr/local/bin/composer"

3.4 Configuration file

php.ini : /usr/local/php5/lib/php.ini php-fpm.conf: /usr/local/php5/etc/php-fpm.conf nginx : /usr/local/etc/nginx/nginx.conf

3.4.1 Config php.ini

Find and set this:

cgi.fix_pathinfo=0

3.4.2 Config php-fpm

Find and set these:

  • pid file, this file is auto created by php-fpm every start to save it's process id. We will use this pid to kill php-fpm running instance in script 3.5 bellow
pid = /path/to/.php-fpm.pid
  • if you want to change listen port for php-fpm, (default 9001), you also should check it.

3.5 Create .sh file to run webserver via command

Create file webserver.sh at your user folder: ~/webserver.sh

nano ~/webserver.sh

Add below content into webserver.sh file

sudo -S id -u

PHPFPM="/Volumes/WORK/WWW/NGINX/logs/.php-fpm.pid"
NGINX="/Volumes/WORK/WWW/NGINX/logs/.nginx.pid"

echo "Killing PHP-FPM"
if [ -e "$PHPFPM" ]; then
    sudo kill `cat "$PHPFPM"`
else
    echo "PHP-FPM is not running."
fi

echo "Killing NGINX"
if [ -e "$NGINX" ]; then
    sudo kill `cat "$NGINX"`
else
    echo "NGINX is not running."
fi

sudo /usr/local/bin/php-fpm --fpm-config /usr/local/php5/etc/php-fpm.conf -D
sudo /usr/local/bin/nginx

Save it. From now, you can start/restart server by run this command:

sh ~/webserver.sh

3.6 NGINX support OpenSSL self-signed

Command to generate SSL certificate via OpenSSL for Nginx

#!/bin/bash
echo "Generating an SSL private key to sign your certificate..."
openssl genrsa -des3 -out myssl.key 1024

echo "Generating a Certificate Signing Request..."
openssl req -new -key myssl.key -out myssl.csr

echo "Removing passphrase from key (for nginx)..."
cp myssl.key myssl.key.org
openssl rsa -in myssl.key.org -out myssl.key
rm myssl.key.org

echo "Generating certificate..."
openssl x509 -req -days 365 -in myssl.csr -signkey myssl.key -out myssl.crt

echo "Copying certificate (myssl.crt) to /etc/ssl/certs/"
mkdir -p  /etc/ssl/certs
cp myssl.crt /etc/ssl/certs/

echo "Copying key (myssl.key) to /etc/ssl/private/"
mkdir -p  /etc/ssl/private
cp myssl.key /etc/ssl/private/

Update nginx config file for ssl

server {
    listen               443;
    ssl                  on;
    ssl_certificate      /etc/ssl/certs/myssl.crt;
    ssl_certificate_key  /etc/ssl/private/myssl.key;
    server_name SERVER_NAME.com;
    location / {
    }
}

4 Upgrade Ruby

4.1 Using RVM to manage and upgrade Ruby RVM

\curl -sSL https://get.rvm.io | bash -s stable --ruby

After installed rvm

nano ~/.bash_profile

Check for rmv load script

# RVM
export PATH="$PATH:$HOME/.rvm/bin"
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*

4.2 Install Ruby and set default version

RVM will auto install Ruby newest stable version to system

5 CocoaPods

sudo gem install cocoapods

6 MySQL 5.7.x

6.1 Install MySQL

  • Download MySQL at (http://dev.mysql.com/downloads/mysql/), you can download DMG file for easy install.
  • After installed, the root/password will be shown. Note it to use later
  • Add mysql bin folder to terminal PATH env by edit file ~/.bash_profile
$> nano  ~/.bash_profile

Enter this information, then save it and restart terminal.

export PATH="/usr/local/mysql/bin:$PATH"
  • Let's change the default expired password by using this command:
mysqladmin -u root -p password
Password: (enter noted password after installed)
New password: (your new password here)
Confirm new password: (enter new password again)

6.2 Install phpMyAdmin

  • Go to: https://www.phpmyadmin.net
  • Download the lastest release and unzip to http root directory
  • Login to phpMyAdmin by user/password above in step 6.1

7 Postfix to sendmail

Mac OS X has shipped with postfix to send email via PHPSendmail. But default, postfix is not started.

Add this lines to bottom of file ~/webserver.sh. Postfix mail server will start at time of start webserver.

POSTFIX="/var/spool/postfix/pid/master.pid"
echo "Check for POSTFIX email system"
if [ -e "$POSTFIX" ]; then
#    sudo kill `cat "$POSTFIX"`
    sudo postfix stop
else
    echo "POSTFIX is not running."
fi
sudo postfix start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment