user3425344
user3425344

Reputation: 3557

Start a PHP server on Mac OS X

I am figuring out how to use PHP OpenId

I have cloned the repo to the ~/www directory. There are some examples in the php-open-id/examples directory which I wanted to run.

Specifically, I wanted to render the page php-open-id/examples/consumer/index.php in order to better understand the API. I started a server in the php-open-id/examples directory using

python -m SimpleHTTPServer 8000

and I navigated to localhost://consumer/index.php

But it didn't work. It shows a dialog box to save the file. What is the correct way to render this PHP file?

Upvotes: 57

Views: 87122

Answers (5)

Arslan Ramay
Arslan Ramay

Reputation: 1160

You can run PHP files locally on macOS without any software e.g. MAMP or XAMPP.

Example 1:

Run the following command in your Terminal and it will start a test server using the index.php file in your current working directory.

php -S localhost:8000 

After running the above command, you will see the following output on your terminal:

PHP 8.2.19 Development Server (http://localhost:8000) started

Now, open http://localhost:8000 in your browser to see the output of your PHP file.

Example 2:

If you want to run the PHP server in a specific folder/directory, then run the following commands:

cd ~/public_html
php -S localhost:8000 -t consumer/

The above command will run the PHP server in '/consumer/' folder assuming that this folder has an index.php file.

Reference: https://www.php.net/manual/en/features.commandline.webserver.php

Upvotes: 1

Carsten
Carsten

Reputation: 65

Update November 2022:

Unfortunately, PHP has been removed from MacOS. This is a brief tutorial which explains how to get PHP working in Ventura.

I personally dislike the idea to compensate the lack of PHP with the activities outlined in the tutorial. My solution was to install Docker and to run a proper NGINX+PHP container. I consider it to be cleaner, more flexible and better to maintain rather than faffing about with half-baked on-board stuff. But that's a matter of taste, I guess.

Also, as suggested in one of the answers, using the Python approach is not hassle free in Ventura, since it requires xcode to be installed.

In MacOS 10.x I have also been working with the on-board Python and PHP. Having lost those tools as standard installations is annoying.

Upvotes: -1

abhinavsinghvirsen
abhinavsinghvirsen

Reputation: 2054

Mac OS

Just Visit your project folder

open terminal on that folder

and Run php -S localhost:9000

then you can see

PHP 7.3.24-(to be removed in future macOS) Development Server started at Tue Mar 30 11:58:49 2021
Listening on http://localhost:9000

After that you are able to access your application on http://localhost:9000 if your port is not available try to change port

Upvotes: 16

user3425344
user3425344

Reputation: 3557

I have found a solution :

Run the server using

php -S localhost:9000

Upvotes: 183

Kulvinder Singh
Kulvinder Singh

Reputation: 307

You need to have a php server for serving .php files. The simple python server is not a php server and therefore it is just simply listing the files in the directory for download.

If you are on OSX 10 or above, it comes with apache which has php module present and can be setup to serve php file.

Tutorials can be found here http://php.net/manual/en/install.macosx.bundled.php and here https://discussions.apple.com/docs/DOC-3083

If you would like to setup a dedicated server instead of using apache, MAMP is a good solution. Google It and see tutorials on how to set it up.

Upvotes: 3

Related Questions