DEV Community

Cover image for How to install Apache in macOS M1 - Montery 12.x.x
Ha Tuan Em
Ha Tuan Em

Posted on

How to install Apache in macOS M1 - Montery 12.x.x

This post is usefull for Macbook M1 and macOS is Montery 12.x.x, so you need to double check what is your chip to make sure everything you do is right.

At the moment, Macbook is removed PHP in macOS Montery. That's mean we can run the application is run LAMP Stack. We need to backup this case. I found a lot case why and how we can change this.
To cover this case, I use the httpd package of homebrew. I will show you we can do now.


1. Stop Apache in macOS

I used httpd in this case, so I need to turn off apache and log errors.

sudo apachectl stop
sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist 2>/dev/null
Enter fullscreen mode Exit fullscreen mode

2. Install homebrew tool to download and setup package referece.

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

3. Install httpd

Since homebrew has installed successfully. We need to install this package that is main package to run all Apache services in macOS 12.

brew install httpd
Enter fullscreen mode Exit fullscreen mode

After installed httpd, we need to start this service and make sure your httpd has ran, we are typing http://localhost:8080 and when you see the test It's work that mean httpd is installed and ran successfully.

brew services start httpd
Enter fullscreen mode Exit fullscreen mode

4. Install PHP

PHP doesn't support in macOS M1, we to download and install PHP in local machine. In this case I use php@7.4.

brew install php@7.4
Enter fullscreen mode Exit fullscreen mode

Start the httpd service when you installed successfully.

brew services start php
Enter fullscreen mode Exit fullscreen mode

Then you run command php -v in the terminal if you see the messeage same same like that. You are installed php success.

❯ php -v
PHP 7.4.28 (cli) (built: Feb 17 2022 16:38:06) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.28, Copyright (c), by Zend Technologies
Enter fullscreen mode Exit fullscreen mode

If you command php not found, you should correct the path to PHP bin is existed. Or not you can add this command bellow .zshrc file or bash file.

❯ ~/.zshrc
export PATH="/opt/homebrew/opt/php@7.4/bin:$PATH"
export PATH="/opt/homebrew/opt/php@7.4/sbin:$PATH"
Enter fullscreen mode Exit fullscreen mode

5. Now, try to test php with database.

I use Moodle framework in this case. Because it's fast and easily to setup.

After framework downloaded, we need to move the folder of source to /opt/homebrew/var/www. You can run manually or use this command:

mv ~/Download/moodle /opt/homebrew/var/www
Enter fullscreen mode Exit fullscreen mode

with

  • /opt/homebrew/var/www is path of directory that folder is contain the resource of applications.

6. Test local folder

Open browser and go to address http://localhost:8080/moodle - moodle is a folder in httpd root directory. You will see the list of files php - tree directory. That's mean you have to success setup httpd in macOS 12. And also we need to setup the php to run php application.


7. Setup php in httpd

Use the command:

nano /opt/homebrew/etc/httpd/httpd.conf
Enter fullscreen mode Exit fullscreen mode

Press Ctrl + W and search LoadModule, then paste the path of php at last line of LoadModule section. In homebrew, this is default path to of php.

LoadModule php7_module /opt/homebrew/opt/php@7.4/lib/httpd/modules/libphp7.so
Enter fullscreen mode Exit fullscreen mode

Final. Apache server

And also, we need to tell Apache server what files we need to run php module. We paste the code bellow the line code which setup php module.

<FilesMatch \.php$>
        SetHandler application/x-httpd-php
</FilesMatch>
Enter fullscreen mode Exit fullscreen mode

Then press Ctrl + O and Enter, Ctrl + X to complete setup php in Apache.

Back to browser in section 6, go to http://localhost:8080/moodle, you will see the magic πŸ˜‰.

And we done.

Thank for reading and see you in next post.

Have a nice day πŸ˜€

Top comments (0)