Sunday, January 7, 2024

Setup MAMP (Mac Apache MySQL/MySQL DB PHP) on Mac OS Sonoma on Macbook (Silicon Chip)

Introduction

Apple MacBook has gained popularity among developers in recent past. Many young developers are switching to Mac from Windows and Linux Environment. This article is an guide for developers who recently switched from differnet environment (specially Linux) to help them set up local MAMP (MacOS Apache MySQL PHP/Perl/Python) Environment

Installing Apache

MacOS Comes preinstalled with Apache and just needed to activate it. to activate Apache in MacOS Sonama (MacOS Version 14.2.1), open terminal from launchpad and issue following command:

sudo apachectl start

Inbuilt Apache works perfectly if you just need a webserver, but if you intend to work with PHP this may not be very useful because PHP has been depricated after MacOS 10 and removed after MacOS 11. Therefore if you want to setup development environment for PHP development on MacOS and you happen to have MacOS Version 12 or higher (like Sonoma in this case) you will need to disable internal Apache and install apache manually. following commands will help you to disable internal Apache and stop it autoloading each time you boot your machine.

sudo apachectl stop

sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist 2>/dev/null

Install Homebrew

In order to install apache on MacOS, homebrew is the easiest method I have found. In order to use homebrew you need install it first, to install homebrew(in case it is not already installed) run following command on terminal

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

run following commands to add path of homebrew to system path so that you can run homebrew from any directory(Directory is another name of folder, generally used when we interact using terminal)

echo "export PATH=/opt/homebrew/bin:$PATH" >> ~/.zshrc

source ~/.zshrc

Test your installation with following command by issuing following command

brew help

if you see some help on terminal this means that homebrew is working perfectly

Installing httpd (formally httpd)

Since home brew is working you can run following command to install apache

brew install brew



run brew services restart httpd to start Apache web server in terminal and open browser and type localhost:8080 (homebrew install apache on port 8080) in address bar if you see following 'It Works", this means your webserver without any problem.

Installing PHP

Since your apache is working now you can install php using homebrew, run following command on terminal to proceed with installation of PHP

brew install PHP



run php --version on terminal to confirm if php is installed propertly


Configure apache to run php

Installtion of Apacahe as well as PHP is complete. Now we need to configure apache to process php. We will also configure document root (location where we need to save webpages as well as php file to server through webserver) to custom location. For this purpose we need to create a folder which will serve as document root. I have created a folder named 'Web' under my home directory. You need to know absolute path of this folder, in my case it is '/Users/isthakur/Web' (/Users/<MacUsername>/Web). Now open httpd.conf in your text editor do following modifications

Add following codes to httpd.conf (location of this file was displayed when you installed apache/httpd in this case the location is /opt/homebrew/etc/httpd/) search for LoadModule and add following codes (these codes are displayed when you installed php) at the bottom of LoadModule section (somewhere line No 160's):

LoadModule php_module /opt/homebrew/opt/php/lib/httpd/modules/libphp.so



Also look for User and group and take a note of User

Now change the location of document root from default value (somewhere near line No 250)

DocumentRoot "/opt/homebrew/var/www"
<Directory "/opt/homebrew/var/www">


I also recommend to change the AllowOverride None to AllowOverride All

Add codes to <FileMatch> as follows (as shown on screen after php installation) and index.php to <IfModule dir_module> section


you may also change the value of ErrorLog and CustomLog if you want to save these logs to any directory other then default (you may need this if you need to access these logs frequently)

Search for AddType (somewhere around Line No 424) and add followng codes to associate .php with php engine for server side processing by apache.

AddType application/x-httpd-php .php



Save this file (httpd.conf) and restart apache with following command

Grant Permssion to webserver to access document root

You need to give permission to this user (_www) to access your custom document root, issue following command to give access to home directory (~)

chmod +a "_www allow execute" ~ 

Finalize and Test

brew services restart httpd

to test create a simple php file with following codes in your document root (I have created file test.php)

<?php phpinfo(); ?>

open localhost/test.php on your browser



If you see screen as above this means that you have successfully installed and configured Apache and PHP for local development and also made a specific folder as document root where you can save your project files and access through web server.

Installing MySQL

To complete the environment, now just mysql is required to be installed I think this is the easiest thing to do, because you just need to install mysql like any other software on mac i.e. download MySQL (Community Edition) from official site (https://cdn.mysql.com//Downloads/MySQL-8.2/mysql-8.2.0-macos13-arm64.dmg).

The installtion process of MySQL is smooth ( you but you may not be able to access mysql directly becasue you need to add the location of mysql executables to path. First confirm that MySQL is installed propertly and is running click on apple icon>System Settings and search for Mysql

Confirm that MySQL Service is running 


 you can see installed instance as well as active instance is green which means that these services are running without any problem. click on configuration tab and locate the Base Directory of MySQL (/usr/local/mysql is default location)

Issue following command to add the path of MySQL binaries to system path

echo "export PATH=/usr/local/mysql/bin:$PATH" >> ~/.zshrc

source ~/.zshrc

launch terminal and confirm if mysql is working from terminal by running following command (you need to enter password that you entered while installing MySQL when system prompts)

mysql -u root -p


MySQL> prompt confirms that MySQL is also working properly.


This is simple how to guide to setup development environment on MacOS Sonoma (14.2.1) on MacBook (SiliconChip)



No comments:

Post a Comment

Setup MAMP (Mac Apache MySQL/MySQL DB PHP) on Mac OS Sonoma on Macbook (Silicon Chip)

Introduction Apple MacBook has gained popularity among developers in recent past. Many young developers are switching to Mac from Windows an...