How to Install Apache + PHP 7 on MacOS Big Sur

How to Install Homebrew Apache web server localhost and php 7 macos big sur

Hi there,

In this article, we will install Apache + PHP 7 with Homebrew on macOS Big Sur.

Let’s start.

Firstly, We have to install XCode Command Line Tools. Open your Terminal and paste the code below.

xcode-select --install

Please wait for Command Line Tools to download and install. This may take some time…

How to Istall Homebrew on MacOS Big Sur

HomeBrew is our highly recommended package manager for MacOS platforms. It is perfect for installing missing packages on our Mac. Now let’s install HomeBrew. Paste the following command into the terminal.

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

You will be asked for your password for installation. Type your password and press enter. Note: You that when you start typing your password, there will be no change on the screen, but you will still type it.

The Terminal will ask you for confirmation for the installation of the HomeBrew files.

Press RETURN to continue and wait for the installation to complete.

How to install Homebrew on MacOS Big Sur Visual Guide Mersev Bilisim
How to install Homebrew on MacOS Big Sur Visual Guide Mersev Bilisim

After the installation, you can check it with the blow code. Paste it to Terminal:

brew-doctor

If you get “Your system is ready to brew” message. Congratulations!

How to Install Apache Web Server on MacOS

Normally, macOS versions come with Apache installed. But we will istall own local host with Homebrew. Let’s stop macOS’ own Apache Server first. To do this, let’s send the following commands to Terminal.

First Command: sudo apachectl stop
Second Command: sudo launchctl unload /System/Library/LaunchDaemons/org.apache.httpd.plist 2>/dev/null

After entering the commands, the system will again ask for your macOS user password and you must enter your password again and press enter to confirm.

Now, Install Own Apache Web Server with HomeBrew on our MacOS Big Sur.

Paste the following command in Terminal and wait for Apache to install.

brew install httpd

Now paste the following command in our Terminal to have Apache start automatically every time we boot our system.
sudo brew services start httpd

Notice: Note that you can enter the following code in Terminal to browse the Apache Error Log.
tail -f /usr/local/var/log/httpd/error_log

IMPORTANT NOTICE: You can use the commands below to stop, start or restart Apache Web Server.

Apache Services Terminal Commands:
Starts the Service
:brew services start httpd
Stop: brew services stop httpd
Restrart: brew services restart httpd

How to Install PHP 7 on MacOS Big Sur

I prefer to install PHP 7 on my Mac. You can change the command you give to the terminal according to the version number.

Open Terminal, type and send the following command and wait for the installation to complete.

brew install [email protected]

IMPORTANT NOTICE: You can find your PHP.ini configuration file in here:
/usr/local/etc/php/yourversion/php.ini

Now we have successfully downloaded and installed PHP as well. We will make the process of showing PHP to Apache and some settings so that Apache Web Server can recognize PHP.

MacOS Apache Web Server httpd.cof Settings

httpd.conf file is settings file of Apache Web Server. If you want to open it, you can write to Terminal:

vi /usr/local/etc/httpd/httpd.conf

But for faster processing, let’s open the httpd.conf file by entering the httpd.conf file directly into /usr/local/etc/httpd.

STEP 1- Before finding the Listen8080 line and change it to Listen 80

STEP 2- 

LoadModule socache_shmcb_module lib/httpd/modules/mod_socache_shmcb.so
LoadModule ssl_module lib/httpd/modules/mod_ssl.so
LoadModule vhost_alias_module lib/httpd/modules/mod_vhost_alias.so
LoadModule userdir_module lib/httpd/modules/mod_userdir.so
LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so

Find these module lines and remove (#) symbols.

STEP 3- At the end of the LoadModule lines, paste the following as a new line:

LoadModule php7_module /usr/local/opt/[email protected]/lib/httpd/modules/libphp7.so
<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>

STEP 4- Go to line 194, find:

User _www
Group _www

and

User Developer -> (Here is your Mac user account name)
Group staff

replace with the above lines.

STEP 5- Go to line 225, find:
#ServerName www.example.com:8080
and replace
ServerName localhost .

STEP 6- Now define where to host the localhost files. Generally, the Sites folder that macOS automatically creates is used. But I will use my MersevBilisim folder that I created on my Desktop. Now got to lines 249 and 250 and find:
DocumentRoot "/usr/local/var/www"
<Directory "/usr/local/var/www">
AllowOverride None
replace with:
DocumentRoot "/Users/Your User Account Name/Sites"
<Directory "/Users/Your User Account Name/Sites">
AllowOverride All

If you want to create a different folder on the desktop and use it. /Users/User Account Name/Desktop/FolderName

STEP 7- Go to line 283 and find: DirectoryIndex index.html and replace with: DirectoryIndex index.php index.html

STEP 8 (Optional) – I recommend active these configs,
Go to line 505 and find: #Include /usr/local/etc/httpd/extra/httpd-userdir.conf Remove the # symbol at the beginning of the line.
Go to line 511 and find: #Include /usr/local/etc/httpd/extra/httpd-vhosts.conf Remove the # symbol at the beginning of the line.

Now, save your httpd.conf file and quit.

STEP 9: Restart the Apache Web Server with this command.

brew services restart httpd

Now you can check your PHP Info.
___

PHP 7 and Apache Web Server are now successfully installed

PHP 7 and Apache Web Server are now successfully installed on your macOS. If you wish, you can read macOS’ta HomeBrew ile MySQL 8 Kurulumu guide.

You can get free support by reporting the problems you encounter in the comments.

Thanks…

 

Also,
To do the same on the Windows platform, you can read our Apache and PHP 7 Installation article on Windows 10.

—Translated by Google Translate.—