Friday, January 5, 2024

Setup inbuilt Apache in MacOS Sonoma

Many developer use mac for web development. I myself has seen several tutorials where trainer used apple system for training, but when I baught macbook, I found it bit difficult to set up the environment. I believe that many new developer face such issues so i decided to write this tutorial to help them. I have been using Linux (mostly Ubuntu/Linux Mint) for quite a time prior to switching to Mac.

Set up Web Server (Apache)

Apache is the most popular webserver. and the good news is that Mac OS (macOS 10.13 onwards) comes preinstalled with Apache and you just need to start the webservices. run following command to start apache 

sudo apachectl start 


To test if apache is working, just open your favorite webbrowser and type localhost on its address if you see page as follows, your webserver is working perfectly.


For those who are familier with apache I would like to inform that Apache2 is installed at /etc/apache2 and default document root is '/Library/WebServer/Documents' 

Document root is the location where you need to save all your files that you want to share through webserver.  Please be noted that when you save any file at default document root you may need to give additional permissions so that they can be served through web server. The easier method is to set up a folder in your home directory (i.e. /users/<yourUserName>) I have created a folder named Web for this purpose on my home directory.
So if you want to change the default document root you need to edit /etc/apache/httpd.conf (you will require to open Macintosh HD and navigate to etc folder. If folder is not visible, press Command+Shift+.(period) to display hidden files.) then open httpd.conf in your favorite text editor (many blogs recommend VI or nano so if you are confortable with these tools or like working with terminal that will also do)
locate DocumentRoot (in my case it was on line No 255) and change the folder from /Library/WebServer/Documents to the directory you created in your home directory (in my case it was /Users/isthakur/Web) 

Now you need to give permission to apache web server to access this folder so run following command on your home directory.

chmod +a "_www allow execute" ~

followed by the command 

sudo apachectl restart


Create html file named index.htm on your selected folder and test by opening localhost in your favorite web browser output in my case is as follows


We have successfully configured the apache on Mac. Wish you all the success.

Inbuilt Apache server works perfeclty fine if you do not intend to use php. But If you plan to develop using PHP as a server side language you must know that MacOS depricated PHP and stopped supporting it implicitely since MacOS Montery.  So if you are PHP lover like me you must stop the inbuilt apache  

Note:
  1. Show Hidden Files  :Click the Finder icon in your dock to open Finder. Navigate to the folder where you want to find hidden files. Press Command + Shift + . (the period key). This will show hidden files in the folder.
  2. Show Macintosh HD :It's really easy to show hard drive on your Desktop Mac. Open the Finder and select the main Finder menu. Then, click Settings. Under the General tab, under the Show these items on the Desktop, ensure Hard disks is selected.

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