How do i run a php file on mac?

Late response, but was looking into doing this for myself, this coming up as one of the results in my searching wanted to provide 2 solutions since I ultimately came to both on my own.

Solution #1

The simple way is to go a round about way by writing a wrapper file to execute the script you're working on. Create a file with the following code:

#!/usr/bin/php

Save it as wrapper.command The name wrapper isn't important, but the command extension tells Finder that this is a shell script to open up in Terminal. The file itself just executes whatever php script is in the include.

Solution #2

The specific inquiry requires a bit of work.

First make sure that the 1st line of the php script is:

#!/usr/bin/php

This is where the preinstalled version of PHP is installed on Mac OS X. You can always verify by running this command in terminal:

whereis php

Once you've added the Shebang line to the php script you've readied it for automatic execution.

To make it double clickable executeable you have to do the following: Right click on the PHP script and click Get Info. Click where it says Open With, click the default option to see all the available options. Select Other...

Switch where it says Enable: from Recommended Applications to All Applications, and click the checkbox for Always Open With. Choose Terminal as the application. Finally, you have to click the button that says Change All...

OS X will verify you want it to set Terminal as the default application to open .php files

This will make every php file open up in terminal by default, but unless they contain the #!/usr/bin/php line they won't actually run.

How to Work with PHP programs on macOS / Mac OS X

The macOS / Mac OS X operating system comes pre-installed with the libraries needed to run PHP programs. You can interact with PHP programs via the Terminal application [Applications -> Utilities -> Terminal]. You can also use the built-in web server that comes with Mac, the Apache web server, to execute PHP programs. For more information on running PHP via the local web server, see the following:

Running PHP on Apache for Mac

PHP programs can be created using any text editor such as EditRocket. PHP programs and scripts typically end with the .php extension. EditRocket will automatically recognize files with the .php extension as PHP programs, and will color the syntax accordingly.

To create a PHP program, simply create a new file, such as hello.php. In the file, place the following:

The above program can be executed using the EditRocket Tools -> PHP -> Execute Program option, or you can execute it from the Terminal window. To execute the script in the Terminal, use the cd command to cd to the directory where the hello.php file was saved, such as

cd /Users/user/Desktop

Then type the following:

php hello.php

Hello World! should then be printed to the screen.

Running PHP on the macOS / Mac OS X Apache Web Server

PHP 5 is installed in macOS / OS X by default, but the built-in Apache web server may not be configured to automatically run PHP. To enable PHP, the apache configuration file needs to be modified. This file is located in the following location:

/etc/apache2/httpd.conf

In order to edit this file, the permissions on the file must be changed. This can be accomplished by doing the following:

1. Open a terminal windows [Applications -> Utilities -> Terminal]

2. Change to the correct directory [cd /etc/apache2]

3. Change the permissions on the file [sudo chmod 755 httpd.conf]

Once the file permissions are changed, you can use a tool like EditRocket to open the httpd.conf file and modify it. After opening the httpd.conf file, we need to look for the following line:

#LoadModule php5_module libexec/apache2/libphp5.so

This line needs to be uncommented by removing the # in front of the line. Remove the # and then save the file.

PHP is now enabled, but we need to restart the apache web server for the changes to take effect. To restart the web server, do the following:

1. Go to System Preferences -> Sharing View

2. Uncheck the Web Sharing box. Then check the Web Sharing box again. This will start and stop the apache web server.

To test PHP, create a PHP file called test.php in the following directory

/Library/WebServer/Documents/

Enter the following URL to visit the page:

//localhost/test.php

How do I run PHP on Mac terminal?

PHP Installation for Mac Users:.
Open terminal or command line window..
Goto the specified folder or directory where php files are present..
Then we can run php code using the following command: php file_name.php..

How do I run a PHP file?

php” file is placed inside the “htdocs” folder. If you want to run it, open any web browser and enter “localhost/demo. php” and press enter. Your program will run.

How do I open a PHP file in my browser?

Open PHP/HTML/JS In Browser.
Click the button Open In Browser on StatusBar..
In the editor, right click on the file and click in context menu Open PHP/HTML/JS In Browser..
Use keybindings Shift + F6 to open more faster [can be changed in menu File -> Preferences -> Keyboard Shortcuts ].

Can you run PHP from command line?

Getting PHP to run from the command line can be performed without making any changes to Windows.

Chủ Đề