If you want to configure a PHP Web development environment on your Mac, here is a guide...

This article seems to be not compatible with MacOS Catalina. Here is a solution.

I switch to Mac in order to develop my applications for iPhone, and it is very laborious. XC

In this guide, I explain how to configure an environment for PHP development, e.g.:

  • set-up the Apache server
  • create virtual hosts
  • configure the PHP module
  • configure the MySQL module
  • and configure phpMyAdmin

The aim is to display the content of the link http://local.myserver.com generated from your local server on Mac.

Tools installation pre-requisites

To develop on Mac, you shall mastered the command lines. Some tools are useful like Sublime Text which simplify the code development.

  1. Download Sublime Text 3 from https://www.sublimetext.com/
  2. Add the app to the Mac
  3. To open Sublime Text by command lines,execute the following commands in console:
> sudo ln -s /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl /usr/local/bin/subl
> subl .

The commands above make a symbolic link to the application.

PHP and Apache installation

Normally, the Apache server and PHP is already installed on the Mac. To check that, execute the following command:

php --version

If nothing is displyed, you shall install the Apache server and PHP and configure these for your system.

Apache configuration

To execute the server correctly, you shall activate at least two modules:

  • The Virtual Hosts management.
  • The URL rewritting management.

In the console, execute:

> subl /etc/apache2/httpd.conf

Uncomment the following lines:

LoadModule vhost_alias_module lib/httpd/modules/mod_vhost_alias.so
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
LoadModule php7_module libexec/apache2/libphp7.so

# Virtual hosts
Include /private/etc/apache2/extra/httpd-vhosts.conf

Modify / Add the following lines in the part <IfModule unixd_module>:

User <your user account> 
Group staff

Restart the Apache server:

> sudo apachectl restart

MySQL installation

To install MySQL, download the version "MySQL Server Community 5.7.24" from the MySQL website.

I have not managed to run version 8 on my Mac :'(

Install MySQL from installer, and note down the password given at the end of installation.

Set-up MySQL for console

To use MySQL by command line, enter mysql in the console is more simple that /usr/local/mysql/bin/mysql.

For that, you shall indicate to the console where it can found mysql launcher.

Execute:

> cd
> subl .bash_profile

Add the following lines in the file .bash_profile:

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

Restart the console before using mysql or execute the following line:

source ~/.bash_profile

Modify the MySQL password

The default password provided by MySQL can be complicated to tap-on for each session. To make easy access to MySQL, execute the following lines:

mysql -u root -p

Enter the obtained password.

Then:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';

MySQL is now configured with your new password.

phpMyAdmin installation

Download phpMyAdmin from https://www.phpmyadmin.net/downloads/ and unzip the content in the folder /Library/WebServer/Documents/phpmyadmin.

Go to the configuration with the following link: http://localhost/phpmyadmin/setup.

Click on New server.

In the tabbed panel "Base configuration", add "127.0.0.1" in Host server name.

In the tabbed panel "Authentification type", enter the MySQL password in Password for configuration method.

Click on Apply.

In the setup page, choose the default language, then click on Download.

Copy the downloaded file in the folder /Library/WebServer/Documents/phpmyadmin.

You can now access to phpMyAdmin with http://localhost/phpmyadmin.

Virtual Hosts configuration

You have Apache, PHP and MYSQL configured, you can now create virtual hosts to access to the site http://local.myserver.com.

Execute subl /etc/apache2/extra/httpd-vhosts.conf

Add the following lines:

<VirtualHost *:80>
    ServerName local.myserver.com
    DocumentRoot "/Library/WebServer/Documents/phpmyadmin"

    <Directory "/Library/WebServer/Documents/phpmyadmin">
        Options +Indexes +Includes +FollowSymLinks +MultiViews
        AllowOverride All
        Require local
    </Directory>

    ErrorLog "/private/var/log/apache2/local.myserver.com-error_log"
    CustomLog "/private/var/log/apache2/local.myserver.com-access_log" common
</VirtualHost>

Execute subl /etc/hosts

Add the following line:

127.0.0.1           local.myserver.com

So, the virtual host is now configured and is linked to phpMyAdmin. If you click on local.myserver.com, you get access to phpMyAdmin.

See you in the next article. xD

Previous Post Next Post


Add a comment