Posted :
This tutorial , will show how to install and configure , nginx and php , under macOS . It will also show , how to configure a local website , using nginx and php .
pkgsrc contains a set of tools for package management . Like for example , tools for compiling and installing a binary package from source .
pkgsrc also provides pkgin , which is a tool used for managing precompiled binary packages . pkgin can be used for example , to search download and install , a precompiled binary package .
To install pkgsrc for a specific version of macOS , instructions can be found here . So , for example , to install pkgsrc for macOS Mojave and higher, this what must be done :
# Copy and paste the lines below to install the 64-bit Mojave set.
#
# These packages are suitable for anyone running Mojave (10.14.6) or newer,
# and are updated from pkgsrc trunk every few days.
#
BOOTSTRAP_TAR="bootstrap-trunk-x86_64-20200219.tar.gz"
BOOTSTRAP_SHA="92992f79188a677f09cfa543499beef3f902017a"
# Download the bootstrap kit to the current directory.
curl -O https://pkgsrc.joyent.com/packages/Darwin/bootstrap/${BOOTSTRAP_TAR}
# Verify the SHA1 checksum.
echo "${BOOTSTRAP_SHA} ${BOOTSTRAP_TAR}" >check-shasum
shasum -c check-shasum
# Install bootstrap kit to /opt/pkg
sudo tar -zxpf ${BOOTSTRAP_TAR} -C /
# Reload PATH/MANPATH (pkgsrc installs /etc/paths.d/10-pkgsrc for new sessions)
eval $(/usr/libexec/path_helper)
# Refresh the pkgin database with the latest version
$ sudo pkgin -y update
Under macOS ,
pkgin
will
install everything under the
/opt/pkg/ directory .
To install nginx , issue the following command :
@difyel:~$ sudo pkgin install nginx
Next , to configure nginx , start by creating the following directories :
@difyel:~$ sudo mkdir -p /opt/pkg/var/htdocs /opt/pkg/var/log /opt/pkg/var/log/nginx @difyel:~$ sudo mkdir -p /opt/pkg/etc/nginx/sites-available /opt/pkg/etc/nginx/sites-enabled
After that , backup the nginx configuration file :
@difyel:~$ sudo mv /opt/pkg/etc/nginx/nginx.conf /opt/pkg/etc/nginx/nginx.conf.bk
And replace it with this one :
# Use nano to create an nginx.conf file
sudo nano /opt/pkg/etc/nginx/nginx.conf
# Paste the following configuration ,
# and save it by pressing
# crl-x , and choosing save .
user nginx nginx;
error_log /opt/pkg/var/log/nginx/error.log;
worker_processes 2;
events {
worker_connections 1024;
}
http {
include /opt/pkg/etc/nginx/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 /opt/pkg/var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
include /opt/pkg/etc/nginx/conf.d/*.conf;
include /opt/pkg/etc/nginx/sites-enabled/*;
}
Next , create a website configuration :
# use nano to create a website configuration
@difyel:~$ sudo nano /opt/pkg/etc/nginx/sites-available/difyel.com
# Paste the website configuration :
server {
root /opt/pkg/var/htdocs/difyel.com;
index index.php index.html index.htm;
server_name difyel.com www.difyel.com;
client_max_body_size 100M;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include fastcgi.conf;
fastcgi_pass unix://var/run/php-7.4.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
# Save the website configuration file ,
# by pressing crl-x , and choosing save .
# difyel.com can be everywhere replaced , by your
# website address
Next , create a symbolic link for the created website , in the sites-enabled
directory , so that it will be enabled :
@difyel:~$ sudo ln -s /opt/pkg/etc/nginx/sites-available/difyel.com /opt/pkg/etc/nginx/sites-enabled # replace difyel.com , with your website address .
Finally add the website address to hosts , so that it can be resolved locally :
@difyel:~$ sudo nano /etc/hosts # add the line 127.0.0.1 difyel.com www.difyel.com # replace difyel.com with the website address # ctrl-x to exit and save .
To install php , issue the following command :
@difyel:~$ sudo pkgin install php-7.4.4 php74-fpm-7.4.4nb3 php74-mbstring-7.4.4 php74-gd-7.4.4 php74-gettext-7.4.4 php74-curl-7.4.4nb7 php74-zip-7.4.4nb3 php74-xmlrpc-7.4.4 php74-pecl-mcrypt-1.0.3 php74-intl-7.4.4nb3 php74-soap-7.4.4 php74-json-7.4.4
Next , to configure php :
# open www.conf using nano @difyel:~$ sudo nano /opt/pkg/etc/php-fpm.d/www.conf # search using ctrl-w for listen = 127.0.0.1:9000 # and replace it with listen = /var/run/php-7.4.4-fpm.sock # search using ctrl-w for ;listen.group = www # and replace it with listen.group = nginx
To start and stop nginx and php , create the following script :
# issue
@difyel:~$ sudo nano /opt/pkg/sbin/control-nginx-php
# paste the following content :
#!/bin/zsh
PHP_FPM_PID="/var/run/php-fpm.pid"
PHP_FPM="/opt/pkg/sbin/php-fpm"
NGINX="/opt/pkg/sbin/nginx"
if [ $# -eq 0 ]
then
echo "Valid commands: "
echo "start | stop | restart"
echo "----------------------------------------"
echo "nginx (start | stop | restart)"
echo "php | php-fpm (start | stop | restart)"
elif [ $1 = "start" ]; then
echo "Starting nginx"
sudo $NGINX
echo "Starting php-fpm"
sudo $PHP_FPM
echo "Done!"
elif [ $1 = "stop" ]; then
echo "Stopping nginx ..."
sudo $NGINX -s stop
echo "Stopping php-fpm ..."
sudo kill `cat $PHP_FPM_PID`
echo "Done!"
elif [ $1 = "restart" ]; then
echo "Stopping nginx ..."
sudo $NGINX -s stop
echo "Stopping php-fpm ..."
sudo kill `cat $PHP_FPM_PID`
echo "Starting nginx"
sudo $NGINX
echo "Starting php-fpm"
sudo $PHP_FPM
echo "Done!"
elif [ $1 = "nginx" ]; then
if [ $2 = "start" ]; then
echo "Starting nginx"
sudo $NGINX
echo "Done!"
elif [ $2 = "stop" ]; then
echo "Stopping nginx ..."
sudo $NGINX -s stop
echo "Done!"
elif [ $2 = "restart" ]; then
echo "Stopping nginx ..."
sudo $NGINX -s stop
echo "Starting nginx"
sudo $NGINX
echo "Done!"
else
echo "Valid commands for nginx: start | stop | restart"
fi
elif [ $1 = "php" ] || [ $1 = "php-fpm" ]; then
if [ $2 = "start" ]; then
echo "Starting php-fpm"
sudo $PHP_FPM
echo "Done!"
elif [ $2 = "stop" ]; then
echo "Stopping php-fpm ..."
sudo kill `cat $PHP_FPM_PID`
echo "Done!"
elif [ $2 = "restart" ]; then
echo "Stopping php-fpm ..."
sudo kill `cat $PHP_FPM_PID`
echo "Starting php-fpm"
sudo $PHP_FPM
echo "Done!"
else
echo "Valid commands for php-fpm: start | stop | restart"
fi
else
echo "Valid commands: "
echo "start | stop | restart"
echo "----------------------------------------"
echo "nginx (start | stop | restart)"
echo "php | php-fpm (start | stop | restart)"
fi
# end of content
# Press ctrl-x to save the content , and
# to exit nano .
# make the script executable , using :
@difyel:~$ sudo chmod +x /opt/pkg/sbin/control-nginx-php
Now , to start and stop nginx and php , the following command can be issued :
# start nginx and php @difyel:~$ control-nginx-php start # stop nginx and php @difyel:~$ control-nginx-php stop # restart nginx and php @difyel:~$ control-nginx-php restart # start nginx @difyel:~$ control-nginx-php nginx start # stop nginx @difyel:~$ control-nginx-php nginx stop # restart nginx @difyel:~$ control-nginx-php nginx restart # start php @difyel:~$ control-nginx-php php start # stop php @difyel:~$ control-nginx-php php stop # restart php @difyel:~$ control-nginx-php php restart
A launch daemon is used to automatically start nginx and php when macOS starts .
To create a launch daemon for nginx :
# issue
@difyel:~$ sudo nano /Library/LaunchDaemons/com.nginx.launchdaemon.plist
# paste the following content :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>nginx</string>
<key>Program</key>
<string>/opt/pkg/sbin/nginx</string>
<key>KeepAlive</key>
<true/>
<key>NetworkState</key>
<true/>
<key>LaunchOnlyOnce</key>
<true/>
</dict>
</plist>
# end of content
# Press ctrl-x , to save the content , and ,
# to exit nano .
To create a launch daemon for php :
# issue
@difyel:~$ sudo nano /Library/LaunchDaemons/net.php.php_fpm_launchdaemon.plist
# paste the following content :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>php-fpm</string>
<key>Program</key>
<string>/opt/pkg/sbin/php-fpm</string>
<key>KeepAlive</key>
<true/>
<key>LaunchOnlyOnce</key>
<true/>
</dict>
</plist>
# end of content
# Press ctrl-x to save the content , and
# to exit nano .
Execute the following commands , to load the launch daemons when macOS starts , and to start nginx and php .
@difyel:~$ sudo launchctl load -w /Library/LaunchDaemons/com.nginx.launchdaemon.plist @difyel:~$ sudo launchctl load -w /Library/LaunchDaemons/net.php.php_fpm_launchdaemon.plist
Create a test file , under the root of your domain :
# Create the root directory for the website , # if it does not exist . @difyel:~$ sudo mkdir -p /opt/pkg/var/htdocs/difyel.com/ # issue @difyel:~$ sudo nano /opt/pkg/var/htdocs/difyel.com/index.php # paste the following content : <?php phpinfo(); ?> # end of content . # replace difyel.com , with your # website root directory . # Press ctrl-x to save the content , and # to exit nano .
Restart nginx and php
using the control-nginx-php restart command , and visit your website.