Page 1 of 1

[SOLVED] How to run PHP scripts in a browser?

PostPosted: Jul 23rd, '16, 02:11
by tarazed
Not sure if this should be posted under networking subforum or not or maybe Mageia Discuss.

Trying to learn something about PHP and php programming. There are a number of tutorials on the web but none say much, if anything, about how to submit php scripts to Firefox.
I wrote a basic 'hello world' script in php and tried to run it in the browser;

Code: Select all
$ firefox localhost/sample.php

This triggers a 404 error in the browser.
Code: Select all
$ firefox file:/home/lcl/sample.php

Firefox says "You have chosen to open a PHP script - what to do with it?"
Well I wanted Firefox to deal with it directly, so there is something I am not understanding obviously.

I had tried earlier to wrap the script in html but learned from the web that that is not possible without editing webserver files. There was also something about .htaccess if apache was the webserver.

It appears that nginx is the webserver for mageia now - always thought it was apache. Had a quick look at some of the nginx documentation but could not really follow it but tried hacking the nginx.conf file but even here I am unsure which one to edit, the /etc/nginx/nginx.conf or /etc/nginx/conf.d/default.conf. I tried both and also tried copying the edited default file to /etc/nginx/nginx.conf.

All I did was insert
Code: Select all
    location / {
        root /home/lcl/;
    }

at the end of the server stanza.

Typing localhost/sample.php in the address bar triggers a 404 error.

The reason I am pursuing this is because there are so many references to PHP in QA testing that I feel it is my duty to learn something about it and it has been suggested that I look at php-gd for some particular tests, but first things first. I have been coding for over 50 years but the only web programming I have ever done is to develop an index.html for a local home page when the WWW first opened its doors. Things have moved on since then.

Re: How to run PHP scripts in a browser?

PostPosted: Jul 23rd, '16, 03:03
by doktor5000
tarazed wrote:It appears that nginx is the webserver for mageia now - always thought it was apache.

Well, there's no real default but most people use apache. Simply stop nginx and install apache, and make sure the actual php runtime itself is installed.

See also https://forums.opensuse.org/showthread. ... en-it-with
https://ubuntuforums.org/showthread.php?t=1234887 or http://askubuntu.com/questions/96306/ho ... -a-browser

Re: How to run PHP scripts in a browser?

PostPosted: Jul 23rd, '16, 08:21
by tarazed
Thanks doktor5000. Did not know there was a php runtime. Shall look for it.

Re: How to run PHP scripts in a browser?

PostPosted: Jul 23rd, '16, 09:00
by filip
doktor5000 wrote:Simply stop nginx and install apache, and make sure the actual php runtime itself is installed.

A nice option is to install task-lamp-php package instead of only apache.

Re: How to run PHP scripts in a browser?

PostPosted: Jul 23rd, '16, 09:14
by tarazed
Yes /bin/php exists.
Tried stopping nginx and starting apache but failed.
Code: Select all
$ sudo systemctl stop nginx
$ systemctl status nginx
● nginx.service - A high performance web server and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled)
   Active: inactive (dead)
$ sudo systemctl start httpd.service
Job for httpd.service failed. See "systemctl status httpd.service" and "journalctl -xe" for details.
$ sudo systemctl -l status httpd.service
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled)
   Active: failed (Result: exit-code) since Sat 2016-07-23 08:02:41 BST; 22s ago
  Process: 19616 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=1/FAILURE)
 Main PID: 19616 (code=exited, status=1/FAILURE)

Jul 23 08:02:41 vega httpd[19616]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 192.168.1.3. Set the 'ServerName' directive globally to suppress this message
Jul 23 08:02:41 vega systemd[1]: httpd.service: main process exited, code=exited, status=1/FAILURE
Jul 23 08:02:41 vega systemd[1]: Failed to start The Apache HTTP Server.
Jul 23 08:02:41 vega systemd[1]: Unit httpd.service entered failed state.
Jul 23 08:02:41 vega systemd[1]: httpd.service failed.

Re: How to run PHP scripts in a browser?

PostPosted: Jul 23rd, '16, 09:37
by tarazed
@filip Thanks
Looks like task-lamp-php is already installed. Another avenue of research. No manual entry for lamp or lamp-php.

Re: How to run PHP scripts in a browser?

PostPosted: Aug 12th, '16, 18:32
by tarazed
After a hiatus I posted a message on the Discuss mailing list and discovered that the scripts must be located in /var/www/html. Trying to persuade the webserver to run them from user directories and modifying the browser address to localhost/home/<user>/<whatever> does not appear to work. So this is solved.

Re: How to run PHP scripts in a browser?

PostPosted: Aug 12th, '16, 23:12
by filip
tarazed wrote:After a hiatus I posted a message on the Discuss mailing list and discovered that the scripts must be located in /var/www/html.

They can be not must be.

tarazed wrote:Trying to persuade the webserver to run them from user directories and modifying the browser address to localhost/home/<user>/<whatever> does not appear to work.

Of course it works.
Instead of localhost you can use many local domains registered in hosts file (/etc/hosts).
Mine /etc/hosts goes:
Code: Select all
###########################################
127.0.0.1 localhost

#Mageia
127.0.0.1 mageia
127.0.0.1 mageia-doc
###########################################


For local domains you also need to add/modify your file
/etc/httpd/conf/vhosts.d/00_default_vhosts.conf.
Here's relevant part of mine:
Code: Select all
###########################################
# for default domain use:
# <VirtualHost _default_:80>
# instead of
# <VirtualHost *:80>

# this one is needed for apache 2.4 used in mga5
<Directory "/home/my_username/Documents/Web development">
    AllowOverride None
    # Allow open access:
    Require all granted
</Directory>

# one domain
<VirtualHost *:80>
    DocumentRoot "/home/my_username/Documents/Web development/Mageia"
    ServerName mageia
</VirtualHost>

# another domain (you can have as much as you want but beside here
they need to be defined in hosts file also)
<VirtualHost *:80>
    DocumentRoot "/home/my_username/Documents/Web development/Mageia-doc"
    ServerName mageia-doc
</VirtualHost>
###########################################


After those changes you need to restart httpd deamon of course. One option is to use as root:
Code: Select all
systemctl restart httpd

There should be no errors or warnings.

After that you would only type your chosen ServerName (in my case mageia) in your browser and it should work if the file is there.

Re: [SOLVED] How to run PHP scripts in a browser?

PostPosted: Aug 13th, '16, 09:49
by tarazed
Thanks Filip. I had tinkered with one of the config scripts but had no real idea what was needed.