Shuvankar Paul Software Engineer, Strativ AB Magento eCommerce Website Development, Website Development Using Php and HTML and Cross-Platform application development using Appcelerator.

A Full Guide to Make Mac Environment for any PHP framework Developemnt

6 min read

Mac is Shipped with some predefined tools for US

  • PHP – will Depreciated in next MacOS release
  • Apache

You can check with

php -v
httpd -v

Install Http (Apache Version)

Brew Official Package : https://formulae.brew.sh/formula/httpd

You can use shipped apache version that come with MacOS, but in this full article we will use our brew apache.

Check running Apache service : ps -aef | grep hatted

# stop the current running apache service
sudo apachectl stop
# unload the system apache version
sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist 2>/dev/null
# install apache
brew install httpd

After completing installation, a screen appear showing some info about.

Brew information

Those information is helpful, but don’t worry, you don’t need to save the screen. You can get this info by

brew info httpd

Now apache is installed. If you now type which httpd
you will found
/usr/local/bin/httpd
now in coming time which package we installed all will be in local directory.

Apache Necessary Command :

  • httpd  -v (check version install)
  • brew services start httpd – To have launchd start httpd now and restart at login
  • sudo apachectl start – if you don’t want/need a background service you can just run
  • sudo apachectl stop – stop the apache service
  • sudo apachectl restart – restart the apache service
  • brew info httpd – getting information about the apache

Apache Necessary Path :

  • Path of project directory : /usr/local/var/www
  • Path on http.conf /usr/local/etc/httpd/httpd.conf
  • Path on Virtual hosts /usr/local/etc/httpd/extra/httpd-vhosts.conf

Necessary Changes on Apache Configuration File :

Apache Configuration path locate at :
/usr/local/etc/httpd/httpd.conf

TypeDescriptionFromTo
ServerName Changeto avoid this error : AH00558: httpd: Could not reliably determine the server’s fully qualified domain name, using Shuvos-MacBook-Pro.local. Set the ‘ServerName’ directive globally to suppress this message#ServerName www.example.com:8080ServerName localhost
Url RewriteUncomment line :
#LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so
#LoadModule rewrite_module lib/httpd/modules/mod_rewrite.soLoadModule rewrite_module lib/httpd/modules/mod_rewrite.so
Virtual Host EnableUncomment Line :
#Include /usr/local/etc/httpd/extra/httpd-vhosts.conf
#Include /usr/local/etc/httpd/extra/httpd-vhosts.confInclude /usr/local/etc/httpd/extra/httpd-vhosts.conf
http port changeBy Default the http request port is set to 8080, we will change it to 80Listen 8080Listen 80
https (ssl) port changeBy Default the http request port is set to 8443, we will change it to 443
File :/usr/local/etc/httpd/extra/httpd-ssl.conf
Listen 8443Listen 443
SSL Engine EnableTo Enable Self Signature SSL : Uncomment line : #LoadModule ssl_module lib/httpd/modules/mod_ssl.so#LoadModule ssl_module lib/httpd/modules/mod_ssl.soLoadModule ssl_module lib/httpd/modules/mod_ssl.so
Virtual Host EnableUncommemt :
#Include /usr/local/etc/httpd/extra/httpd-vhosts.conf
#Include /usr/local/etc/httpd/extra/httpd-vhosts.confInclude /usr/local/etc/httpd/extra/httpd-vhosts.conf
SSL config EnableUncomment :
#Include /usr/local/etc/httpd/extra/httpd-ssl.conf
#Include /usr/local/etc/httpd/extra/httpd-ssl.confInclude /usr/local/etc/httpd/extra/httpd-ssl.conf
Change Port in SSL File alsoChange 8443 port in ssl file<VirtualHost _default_:8443><VirtualHost _default_:443>
Shmcb extension enableUncomment :
#LoadModule socache_shmcb_module lib/httpd/modules/mod_socache_shmcb.so
#LoadModule socache_shmcb_module lib/httpd/modules/mod_socache_shmcb.so#LoadModule socache_shmcb_module lib/httpd/modules/mod_socache_shmcb.so
Change Web Server Group (***)Web server Group — by default it is _www. You need to change it, otherwise permission issue will happen. in the next two column it description from -> changes
you need to change
username from User username to your username.
You can found your username by,
whoami
in my case it is shuvo
so my full command
User shuvo
Group staff
User _www
Group _www
User username
Group staff

Now restart the apache server & browse the localhost, you will found this look. But still just start. We need to configure custom site, https, php, mysql & others

Create Custom Site both http & https

Summary : By default with localhost you can continue your work, but why not work with custom domain in localhost.
So I will configure equaltrue.devs (as .dev is publicly used) with https & http

Set youdomain.devs (equaltrue.devs) in .hosts file

Open /etc/hosts with sudo access

sudo open -a vscode /etc/hosts

You can open any file with any IDE or text editor
please give a look on this video for details :
https://youtu.be/-KNJm98WdN4

How to open file in application in Mac

Add following line :
127.0.0.1       yourdomain.devs

Generate SSL for the site yourdomain.devs : (Self Signature SSL Certificate)

openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /usr/local/etc/httpd/yourdomain-devs-server.key -out /usr/local/etc/httpd/yourdomain-devs-server.crt

It will prompt you just give the asked answer and for
Common Name (eg, fully qualified host name) give yourdomain.devs

Configure Custom yourdomain.devs in Apache :

Open file : /usr/local/etc/httpd/extra/httpd-vhosts.conf
Remove all lines from the file
Add code following, which will set http version of the site and you need to upload the files on DocumentRoot Directory

<VirtualHost *:80>
    ServerAdmin webmaster@yourdomain.devs
    DocumentRoot "/Users/suvankar/WebServer/yourdomain.devs"
    <Directory "/Users/suvankar/WebServer/yourdomain.devs">
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Require all granted
    </Directory>
    ServerName mymall.devs
    ErrorLog "/var/log/apache2/yourdomain.devs-error_log"
    CustomLog "/var/log/apache2/yourdomain.devs-access_log" common
</VirtualHost>

Now http is ready.

Now for https:

We will also generate a default certification for default ssl

openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /usr/local/etc/httpd/server.key -out /usr/local/etc/httpd/server.crt

You don’t need to provide the Common Name Just keep it empty.

Now yourdomain.devs in https part :

add following line in : /usr/local/etc/httpd/extra/httpd-vhosts.conf
you could also can add this on /usr/local/etc/httpd/extra/httpd-ssl.conf this file but to keep all in one place, we place it here.

<VirtualHost *:443>
    ServerAdmin webmaster@yourdomain.devs
    DocumentRoot "/Users/suvankar/WebServer/yourdomain.devs"
    <Directory "/Users/suvankar/WebServer/yourdomain.devs">
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Require all granted
    </Directory>
    ServerName mymall.devs
    SSLEngine on
    SSLCertificateFile "/usr/local/etc/httpd/yourdomain-devs-server.crt"
    SSLCertificateKeyFile "/usr/local/etc/httpd/yourdomain-devs-server.key"
    ErrorLog "/var/log/apache2/yourdomain.devs-error_log"
    CustomLog "/var/log/apache2/yourdomain.devs-access_log" common
</VirtualHost>

You will found this warning, as because it’s is self certificate ssl, just visit this website.

Now Apache & Virtual is Configured.

PHP Installing

  • for latest version – brew install php (this will installed all necessary library)
  • for specific version install – brew install php@7.2

After complete install a list of instruction will show, you need to complete that.

For better understand let’s breakdown here

Now we will configure php with Apache

Introduce php module to Apache

To enable PHP in Apache add the following to httpd.conf and restart Apache:

LoadModule php7_module /usr/local/opt/php/lib/httpd/modules/libphp7.so

Now the below line will make sure apache understand .php file

<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>

Finally, check DirectoryIndex includes index.php

On <IfModule dir_module> if you found like

<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>

Change it to

<IfModule dir_module>
    DirectoryIndex index.php index.html
</IfModule>

Now on next line it will show the php.ini location

Now Create a index.php file with <?php phpinfo();?> code on /Users/suvankar/WebServer/yourdomain.devs/ directory and browse https://yourdomain.devs
It should phpinfo

Now all good but still Terminal command php (php-cli). Please restart the terminal.

If which php show path like /usr/local/bin/php this then okay if show /usr/bin/php then take default MacOS php version.


What we will do we will change that by adding PATH

echo 'export PATH="/usr/local/opt/libpq/bin:$PATH"' >> ~/.zshrc

Now all okay, change your environment php variable like max_execution_time, post_max_size and others.

Multiple PHP Version :

Now we will install another php version, suppose you need to install php version 7.3. So will follow the same step

Check Available php version

# check available php version
brew search php

Now we install php version 7.3

brew install php@7.3

Now you need to configure new php version with apache. Add the following line below
LoadModule php7_module /usr/local/opt/php/lib/httpd/modules/libphp7.so
and comment this line

LoadModule php7_module /usr/local/opt/php@7.3/lib/httpd/modules/libphp7.so

LoadModule php7_module /usr/local/opt/php@7.3/lib/httpd/modules/libphp7.so

Final Code will be

#PHP Version
#LoadModule php7_module /usr/local/opt/php/lib/httpd/modules/libphp7.so
LoadModule php7_module /usr/local/opt/php@7.3/lib/httpd/modules/libphp7.so

Switch Between PHP Version :

PHP version change :

Manual Way :

# This will unlink the latest php with php 7.3
brew unlink php && brew link --force php@7.3

Please Make sure you comment other php lib in apache after unlink on httpd.conf

# if you switch from php latest to php 7.3
#LoadModule php7_module /usr/local/opt/php/lib/httpd/modules/libphp7.so
LoadModule php7_module /usr/local/opt/php@7.3/lib/httpd/modules/libphp7.so
# if you switch from php 7.3 to php latest
LoadModule php7_module /usr/local/opt/php/lib/httpd/modules/libphp7.so
#LoadModule php7_module /usr/local/opt/php@7.3/lib/httpd/modules/libphp7.so

PHP Automate Switcher :

Install the switcher

# Download the switcher script to the proper destination
curl -L https://gist.githubusercontent.com/rhukster/f4c04f1bf59e0b74e335ee5d186a98e2/raw > /usr/local/bin/sphp
# Make the switcher script writeable
chmod +x /usr/local/bin/sphp

Now switch to version. It will ask to restart apache.

# To switch whatever version to php 7.4
sphp 7.4

# To switch whatever version to php 7.3
sphp 7.3

For Deprecated Version PHP install : (Recommend not to use, if emergency need then use Docker or Bitnami)

brew tap exolnet/homebrew-deprecated
brew upgrade
brew install php@7.1

Then same way adding the line in httpd.conf file and uncomment other php lib line

LoadModule php7_module /usr/local/opt/php@7.1/lib/httpd/modules/libphp7.so

When you try to switch php version to deprecated version it will show error like this

there will be file missing so you have error will showdyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.61.dylibReferenced from: /usr/local/bin/phpReason: image not found

Now this problem can be solved by this thread
you need to use : 

brew reinstall icu4c

If not working then look this thread

https://github.com/eXolnet/homebrew-deprecated/issues/27

You can follow this thread : https://gist.github.com/hgrimelid/703691ab48c4a4d0537cfe835b4d55a6

Shuvankar Paul Software Engineer, Strativ AB Magento eCommerce Website Development, Website Development Using Php and HTML and Cross-Platform application development using Appcelerator.

Disable Magento 2 Two Factor Authentication

In new Magento Version by Default Magento 2 Enable Two Factor Authentication, but if you work on localhost, then it is not useful for...
Shuvankar Paul
12 sec read

Install MySQL in MacOS with Brew | Run Multiple…

Home Brew MySQL install in Mac and run Multiple MySQL version with
Shuvankar Paul
5 min read

Remove Home-Brew MySQL Completely from MacOS

>> Please do take backup all DB Before Deleted MySQL service & Process Kill Check MySQL running or not brew services list if running...
Shuvankar Paul
45 sec read

Leave a Reply

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