Setting Up a Web Development Environment on Mac
Many developers use Macs for web development. I myself have seen several tutorials where the trainer used an Apple system for training, but when I bought a MacBook, I found it a bit difficult to set up the environment. I believe that many new developers face such issues, so I decided to write this tutorial to help them. I had been using Linux (mostly Ubuntu/Linux Mint) for quite some time prior to switching to Mac.
Set Up Web Server (Apache)
Apache is the most popular web server, and the good news is that macOS comes pre-installed with Apache and you just need to start the web services. Run the following command to start Apache:
sudo apachectl start
To test if Apache is working, just open your favorite web browser and type "localhost" on its address bar. If you see the page as follows, your web server is working perfectly.
For those who are familiar with Apache, I would like to inform you that Apache2 is installed at /etc/apache2
and the default document root is /Library/WebServer/Documents
.
The document root is the location where you need to save all your files that you want to share through the web server. Please be noted that when you save any file at the default document root, you may need to give additional permissions so that they can be served through the 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 in 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 the "etc" folder. If the 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 comfortable with these tools or like working with the terminal, that will also do).
/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 the Apache web server to access this folder, so run the following command on your home directory:
chmod +a "_www allow execute" ~
...followed by the command:
sudo apachectl restart
Create an HTML file named index.html
in your selected folder and test by opening "localhost" in your favorite web browser. The output in my case is as follows:
We have successfully configured Apache on Mac. Wish you all the success!
Important Notes
The in-built Apache server works perfectly 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 deprecated PHP and stopped supporting it implicitly since macOS Monterey. So if you are a PHP lover like me, you must stop the in-built Apache.
Note:
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.Show Macintosh HD: It's really easy to show the 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" section, ensure "Hard disks" is selected.
No comments:
Post a Comment