In this article, we are going to install apache2 and PHP on our Mac. Apache and PHP come packaged with OS X. To create a local web server, all we need to do is configure Apache
Following are the steps to install Apache and PHP.
- Open terminal
- Execute “sudo su -” to logged in as a root user. provide your password.
- Enable Apache “apachectl start”
- Open safari and visit “http://localhost”. Browser output “It Works!”
- Now we are going to enable PHP for apache. For this, we need to modify Apache Configuration file. But before modifying configuration file we are going to create its backup. Execute “cp /etc/apache2/httpd.conf /etc/apache2/httpd.conf.bkp”
- Now we are ready to edit httpd.conf or Apache Configuration file. Open it in nano editor. Execute “
- Now search for “# LoadModule php7_module libexec/apache2/libphp7.so” and remove “#”. It will enable PHP. Also, we need to change the document root to “/Users/UserName/Documents”. UserName is based on your mac username. Now press Ctrl+O to save the changes and Ctrl + X to quit the nano editor.
- Restart apache by executing “apachectl restart”
- Change permission for /Users/UserName/Documents folder using “sudo chmod -R 747 /Users/admin/Documents”
- Now open your favorite text editor and create an index.php file with content <?php echo “Hello, vLemonn!”; ?> in “/Users/admin/Documents”
- Browse it using Safari.