Using PHP scripts from html on mac's default web server - php

I'm developing a site with PHP and I need to test it using my Web Server on Mac. I installed the server and made sure it's working. Localhost is seeing and serving whatever I put in /Library/WebServer/Documents.
Now I put a test script in the CGI-Executables directory, and tried calling it from an html file inside the Documents directory. It results in this error:
The requested URL /CGI-Executables/SM.php was not found on this server.
The file is there, so I'm assuming it's a configuration issue, and looking around I played with the /etc/apache2/extra/httpd-vhosts.conf file but it seems like an overkill. I don't need a virtual host, and I'm not going to serve anything from my development machine. I just want to develop my site.

You should either put your scripts in /Library/WebServer/Documents/ or call them like /cgi-bin/SM.php Also, are you sure you want cgi scripts or do you just need your php to work? If its the latter, then putting them up in the Document root is a better option than using it as cgi script.

Related

Give a client working PHP file that is executable in Windows that also outputs directories and files

I created a PHP script that takes an input of a CSV file through the web browser and outputs a bunch of directories and files according to the CSV file. Now I want to give this to my client as a standalone application without the need to host their own LAMP stack as my client is not technical at all. He just wants it to live natively on their desktop where he can easily click to open it.
I think you can use Docker.
You can make the php script run from docker by creating a folder structure like this:
docker-compose.yml # This file should contain the docker configurations
run-application.bat # This file should run the docker application
app/ # Insert the script and map it inside the docker configuration
once run-application.bat is clicked the script should run docker and create a LAMP server mapped to the client computer local port.
You can also decide to automaticaly open the browser.
Two steps only, 5 minutes. Put EasyPHP (https://www.easyphp.org) on a USB key and put your file in "www". Your client will able to run those file by launching EasyPHP. He can even copy the whole folder on his hard drive. EasyPHP is portable.

Testing my php scripts and forms

I have two html forms and two php scripts. The first one is a form which is supposed to submit a users email to a .txt file, and the other is a Stripe payment form which uses php code to charge the customer.
Now my problem is that there are some issues with the two php scripts, that I can't figure out how to fix, because I am not really sure how to test the scripts. Normally when testing the html scripts I would just open the html files in my browser, but that doesn't work on my php scripts as the site just shows what is written in the scripts when called/submitted.
So my question is how do I test my scripts, without having to use a hosting account and can I even test it like this?
You need to run a web server on your local computer to test it out. I would suggest looking into something called Vagrant, which allows you to fairly easily create virtual machines on your computer in which you can install anything you like without fear of messing up anything else. If you go here you can even find a "box" to create a virtual machine that already has apache and php installed.
Depending on your version of PHP there's a webserver builtin right into PHP:
http://php.net/manual/en/features.commandline.webserver.php
You probably have to install a web server locally with php enabled. Since php is a server side language it needs a server to run on, in order to send you back the html, after execution. If you need to see the result on a browser.
http://en.wikipedia.org/wiki/LAMP_%28software_bundle%29.
Or run your script from terminal (you must have php installed) if you don't need to see the result in the browser and just want to run your script.
$ php myscript.php

PHP won't open files in Ubuntu LAMP web application

I am working a LAMP web app running on Ubuntu 11.10.
I followed instructions on the web to harden my apache, php and mysql.
I have a PHP script which work fine when I run from the command line under my own id. But when I put the scripts into the web app framework, it can't not even open a log file to write (in /tmp) and it can't read other files in /var/www/myapp/html as well.
I used Ajax to retrieve file contents on the server and then serve those files to the browser. So my url will look like: "php/myphpscript.php?arg=.......".
My directory structure is
/var/www/myapp/html|php|js|cfg.
I know this problem has something to do with permission, security but I am quite at loss.
Can someone describe what I need to do here?
Thanks,
I suspect you copied the files with your username, but apache executes as user www-data an thus has no access to your files. Either change them to belong to the apache user, or if you are the only develper on this machine, cahnge apache to run as you.

php dev/testing servers

I need a simple "development" server for php, e.g. not apache.
In a modern environment, such as node.js, I can run node server.js inside any folder, and it will run as a server running the site specified by server.js. I can then run another node process from a different folder, and the two servers will never interfere or get in each other's way.
Is there a similar setup for php?
With apache, it seems to me that I need to "configure" the server ahead of time; I can't just drop into some folder and serve its content on some arbitrary port.
I want a command that I can use to run a php server from inside some folder, with minimum amount of configuration, for the purpose of being a development/testing only server.
For instance, suppose this server is called sps, then, I should be able to:
cd ~/code/proj1
sps
Perhaps it could require a simple config file, sps.conf that specifies the port number the server should listen to, plus maybe information about the database connection; but nothing more.
Does such a tool exist for php?
With the current version of PHP (< 5.4), you indeed have to configure a webserver (Apache, nginx, ...) to serve the directory in which you'll have your website -- the directory in which you'll work.
Generally, though, you'll only have a couple of websites, which means you won't have to re-configure your webserver too often.
And if you often have to create / test some small scripts, just create an Apache VirtualHost that points to some tests directory, in which you'll put all your test scripts (I have exactly that on my computer).
With PHP 5.4 (currently in alpha -- so not to be used on a production server just yet ^^), you'll have a built-in web server, which should pretty much answer your question.
try XAMPP Lite version http://www.apachefriends.org/en/xampp-windows.html

How do I run a file on localhost?

How do I actually run a file on localhost?
I know it is working, but how do I run a file on it, and how do I verify that the file is in fact running on localhost?
Server newbie here, additional questions (I have xampp running Apache 2.2):
From your responses it sounds like I have to type in the path in the browser in order to open the file on the localhost. So, if I want to have the file in a subdirectory, I would have to type the full path including the directory?
Is there a way to have the browser point to the localhost while I am working on my html, instead of having to type in the path all the time?
I am working in Dreamweaver CS3, and want to use include commands (either SSI or PHP), but not have to keep putting my files up on HostGator in order to see if they are working.
What is the best way to do this (please be specific, I know nothing). If there is a detailed tutorial anywhere, really appreciate a link.
Thank you
Ok, thanks for the more specific info, ppl may remove their downvotes now...
What you are proposing is a very common thing to do! You want to run your web application locally without uploading it to your host yet. That's totally fine and that's what your Apache is there for. Your Apache is a web server meaning its main purpose is to serve HTML, PHP, ASP, etc. files. Some like PHP; it first sends to the interpreter and then sends the rendered file to the browser. All in all: it's just serving pages to your browser (the client).
Your web server has a root directory which is wwwroot (IIS) or htdocs (apache, xampp) or something else like public_html, www or html, etc. It depends on your OS and web server.
Now if you type http://localhost into your browser, your browser will be directed to this webroot and the server will serve any index.html, index.php, etc. it can find there (in a customizable order).
If you have a project called "mytutorial" you can enter http://localhost/mytutorial and the server will show you the index-file of your tutorial, etc. If you look at the absolute path of this tutorial folder then it's just a subfolder of your webroot, which is itself located somewhere on your harddrive, but that doesn't matter for your localhost.
So the relative path is
http://localhost/mytutorial
while the absolute path may be
c:/webservices/apache/www
or
c:/xampp/htdocs
If you're working with Dreamweaver you can simplify the testing process by setting up your local server as a testing server in your project settings. Try it! It's easy. Once it's done, you can just press the browser icon with any of your files and it will open on localhost.
You can do it by running with following command.
php -S localhost:8888
I'm not really sure what you mean, so I'll start simply:
If the file you're trying to "run" is static content, like HTML or even Javascript, you don't need to run it on "localhost"... you should just be able to open it from wherever it is on your machine in your browser.
If it is a piece of server-side code (ASP[.NET], php, whatever else, uou need to be running either a web server, or if you're using Visual Studio, start the development server for your application (F5 to debug, or CTRL+F5 to start without debugging).
If you're using a web server, you'll need to have a web site configured with the home directory set to the directory the file is in (or, just put the file in whatever home directory is configured).
If you're using Visual Studio, the file just needs to be in your project.
Localhost is the computer you're using right now. You run things by typing commands at the command prompt and pressing Enter. If you're asking how to run things from your programming environment, then the answer depends on which environment you're using. Most languages have commands with names like system or exec for running external programs. You need to be more specific about what you're actually looking to do, and what obstacles you've encountered while trying to achieve it.
Looking at your other question I assume you are trying to run a php or asp file or something on your webserver and this is your first attempt in webdesign.
Once you have installed php correctly (which you probably did when you got XAMPP) just place whatever file you want under your localhost (/www/var/html perhaps?) and it should run. You can check this of course at localhost/file.php in your browser.
Think of it this way.
Anything that you type after localhost/ is the path inside the root directory of your server(www or htdocs).
You don't need to specify the complete path of the file you want to run but just the path after the root folder because putting localhost/ takes you inside the root folder itself.
i am working in VScode currently. i was wanting to run my html page just to see all my main elements.
1) first, in vs, right click desired html file and choose "copy path". do not choose relative.
2) finally, paste html path in address bar (i used chrome) and hit enter. your html page should display. hope this helps someone out.

Categories