Php Server For Mac

Posted on  by 



When starting php -S on a mac (in my case macOS Sierra) to host a local server, I had trouble with connecting from legacy Java. As it turned out, if you started the php server with 'php -S localhost:80' the server will be started with ipv6 support only! To access it via ipv4, you need to change the start up command like so: 'php -S 127.0.0.1:80'. Feb 22, 2019 Before you can enable PHP on a Mac, you first have to enable Apache. Both PHP and Apache are free open source software programs and both come installed on all Macs. PHP is server-side software, and Apache is the most widely used web server software. Enabling Apache and PHP on a Mac isn't difficult to do. MAMP is a Mac utility that helps users install Apache, Nginx, PHP and MySQL on their machine and therefore have a personal web server. The name of the program is an acronym for Macintosh, Apache, MySQL and PHP.

Mac OS X runs atop UNIX. Apache and PHP come packaged with OS X. To create a local web server, all you need to do is enable them.

Starting Appache

Open the Terminal app (command line) and type:

Starting PHP

In order to enable PHP you need to enable it on httpd.conf.
Open for editing /etc/apache2/httpd.conf

Download Php Server For Mac

and uncomment the line

Note: it may require root permission. You can use sudo or change permission to 777

Restart Apache for changes to take place

From now on .php files can be created and executed from the terminal using

Run PHP files on server

Web files located at directory: /Library/WebServer/Documents
Create a filename.php file by

Mac

Open a browser in the address http://localhost/filename.php

Changing default web files directory

The default web files directory is /Library/WebServer/Documents.
Every time you browse to your localhost you get to that directory.
The following will instruct you to change the directory to /Users/your-username/Sites.

Edit the Apache config file:

Change the Document root from the default /Library/WebServer/Documents to the new folder under home directory:
Look for these lines:

And change them to:

Mac

Of course instead of your-username insert there your real username.

I prefer to have it there, as I do not need root permissions to add content to that folder.

Enable .htaccess file in Mac OS X

In the same file (/etc/apache2/httpd.conf), under the section <Directory “/Users/your-username/Sites”> look for:

And change it to:

Also edit this file:

And look for the same line as above

Apple server software

And change it to:

Then restart apache:

How to enable access to sub-directories from the browser

Apache Php Server For Mac

Starting with OS X 10.10 (Yosemite), Apple moved from Apache 2.2 to Apache 2.4, and that means there are a few additional changes we need to make. One of them is a directive that helps secure your machine by denying access to the entire file system by default.
In order to enable access to a sub directory from the browser (e.g. http://localhost/subdirectory/filename.php ) you need to edit the Apache config file:

Comment out (place ‘#’ in front of each line) the section of code runs from line 220 through 223.





Coments are closed