How to install and run Apache, PHP on mac OS Big Sur

Akshay Devkate
Nerd For Tech
Published in
4 min readJun 6, 2021
Photo by Christopher Gower on Unsplash

Index

Installing Apache

Installing php

Installing Apache

Apache and php comes pre installed in mac OS Big Sur, You will just need to activate them using terminal. To start apache type the following command in the terminal and run.

sudo apachectl start

You need to write “sudo” before the actual command. We use sudo, which stands for “super-user do,” to run apps with higher rights. In other words, it allows ordinary users to perform system-level tasks. Installing apps or upgrading the operating system are examples. Things that typical users aren’t supposed to do. Once you press enter to execute the command you will be asked for the system password.

to make sure that apache works perfectly type the following address in the web browser.

http://localhost

If everything works perfect you will be shwon “It works!” message in the browser indicating that the apache is working fine. Else you may have encountered any error.

output image of http://localhost

Installing PHP

After running apache server now you can start php Now open terminal and type following command and password.

sudo nano /etc/apache2/httpd.conf

Remember for each sudo command you will be asked for the password. nano is the text editor, On macOS and most Linux distributions, the Nano text editor comes pre-installed. This command will open httpd.conf file in nano text editor.

The above nano text editor will open here you need to uncomment the line which has php in it (that is removing # from the front). To save our time we can use (cmd+f) in the nano editor and copy paste the following line. After getting the line you need to uncomment it that is remove # from the beginning.

#LoadModule php7_module libexec/apache2/libphp7.so

Once you remove # from the beginning you need to press ctrl +x to exit from the nano editor and you will be asked to save the changes that you made in nano file.

Now you need to restart the apache. Use following command to restart .

sudo apachectl restart

Now we need to change the default file used by our local host. Goto terminal again and type the following command

sudo nano /etc/apache2/httpd.conf

Search for the following code

<IfModule dir_module>

DirectoryIndex index.html

</IfModule>

This indicates the default file run by our webserver when we type http://localhost. Now we want to run the file with the name index.php to do so change it to the following code

<IfModule dir_module>

DirectoryIndex index.php index.html

</IfModule>

This will change the default executable file to index.php now restart the server using the following command

sudo apachectl restart

After restarting the server you might be encountered with the error user permissions here you need to give “read and write permission” to the user to make changes. This can be done in the following way.

In finder on the upper navigation bar navigate to Finder ->Go->Go to Folder and type the location /Library/WebServer navigate to Documents and write click on ‘Document’ folder and than select ‘Get Info’ now navigate to ‘Sharing and Permissions’ You need to change every priviledge from ‘Read Only’ to ‘Read and Write’. You need to enter admin by clicking on the yellow lock on the bottom of the window click on it and enter the system password to enter into admin. Later you will be able to change the privilegdes.

Now you just have to create a php file and run the webserver. As we dont have one.

<?php

echo “<title>Finally We did it </title>”;

echo “<p>Now you can start building your own website or projects </p>”;

?>

save it as index.php in the following location /Library/WebServer/Document folder. Now go and run http://localhost in your web browser

--

--