How to install Apache in macOS M1 - Montery 12.x.x
Tags: apache, macos, macm1
Published: 2022-02-28 14:44:26

> 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.

```bash
sudo apachectl stop
sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist 2>/dev/null
```
---
## 2. Install `homebrew` tool to download and setup package referece.

```bash
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
```
---
## 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.

```bash
brew install httpd
```
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.
```bash
brew services start httpd
``` 
---
## 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`.

```bash
brew install php@7.4
```
Start the httpd service when you installed successfully.
```bash
brew services start php
``` 
Then you run command `php -v` in the terminal if you see the messeage same same like that. You are installed `php` success.

```bash
❯ 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
```

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.

```bash
❯ ~/.zshrc
export PATH="/opt/homebrew/opt/php@7.4/bin:$PATH"
export PATH="/opt/homebrew/opt/php@7.4/sbin:$PATH"
```
---
## 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:

```bash
mv ~/Download/moodle /opt/homebrew/var/www
```

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:
```bash
nano /opt/homebrew/etc/httpd/httpd.conf
```

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`.

```bash
LoadModule php7_module /opt/homebrew/opt/php@7.4/lib/httpd/modules/libphp7.so
```
---
## 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`.

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

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 😀