How to host .php files on apache2 - ubuntu? - php

I am hosting my web application for my maturita exam on google cloud. So I created VM (running on ubuntu) and installed apache2 on it. Then I installed PHP successfully. The problem is, it is hosting only .html files even though I installed PHP properly. When i try to host .php file an HTTP ERROR 500 will ocur.
The phpinfo.php is the only file thats working.
<?php phpinfo(); ?>
My question is. How do I change configuration of apache on Google clouds ubuntu?

PHP is the component of your setup that will process code to display dynamic content. It can run scripts, connect to your MySQL databases to get information, and hand the processed content over to your web server to display.
Once again, leverage the apt system to install PHP. In addition, include some helper packages this time so that PHP code can run under the Apache server and talk to your MySQL database:
sudo apt install php libapache2-mod-php php-mysql
This should install PHP without any problems. We’ll test this in a moment.
In most cases, you will want to modify the way that Apache serves files when a directory is requested. Currently, if a user requests a directory from the server, Apache will first look for a file called index.html. We want to tell the web server to prefer PHP files over others, so make Apache look for an index.php file first.
To do this, type this command to open the dir.conf file in a text editor with root privileges:
sudo nano /etc/apache2/mods-enabled/dir.conf
It will look like this:
/etc/apache2/mods-enabled/dir.conf
<IfModule mod_dir.c>
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>
Move the PHP index file (highlighted above) to the first position after the DirectoryIndex specification, like this:
/etc/apache2/mods-enabled/dir.conf
<IfModule mod_dir.c>
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>
When you are finished, save and close the file by pressing CTRL+X. Confirm the save by typing Y and then hit ENTER to verify the file save location.
After this, restart the Apache webserver in order for your changes to be recognized. Do this by typing this:
sudo systemctl restart apache2
You can also check on the status of the apache2 service using systemctl:
sudo systemctl status apache2

An HTTP 500 error can be caused by a problem with your Apache configurations. If your code generate a 500 error, check to make sure they have permission to run in the directory where they are located. Apache will not allow programs to execute by default. So try to give permission to your virtual host directory with CHMOD.
And also for more details about the error you should check Apache error log like:
tail -f /var/log/apache2/error.log

Related

PHP downloading instead of opening in browser - MacOS Sierra

I'm having issues related to loading php using Apache. I use a iMac running Sierra 10.12.6. Server version: Apache/2.4.25 (Unix). PHP 5.6.30 (cli) (built: Feb 7 2017 (16:18:37). Loaded module: php7_module (shared). I have been making all changes using Terminal. So far I can access my localhost, getting the "It Works!" page. I can access my user directory, for html documents only.
When I load the php5 module localhost will not connect at all. Server localhost not found. When I comment the php5 load module out again and restart apache I can gain access html documents in both localhost and localhost/~. When php5 is not loaded it downloads the document instead of opening it. It will go to the downloads folder or open in a text editor like Dreamweaver automatically. I downloaded MAMP and can access the phpinfo page and localhost on port 8888 so I think that is Ok, although I'm having issues connecting MySQL. That's a separate issue. Right now I just want to access php in my localhost/~.
My user directory info is (using actual username)
<Directory "Users/username/Sites/">
Options Indexes MultiViews
AllowOverride All
Require all granted
</Directory>
I followed tutorials making the recommended changes to the httpd.conf and other files and creating the index.html and php info documents in ~/Sites.
LoadModule userdir_mod
AddHandler php5-script php
display_errors = On
Include /private/etc/apache2/extra/httpd-userdir.conf
Include /private/etc/apache2/other/*.conf
I also changed the ServerName to localhost:80.
I also changed permissions on my WebServer/Documents folder to read & write for system, wheel and everyone.
I also made the following changes after reading through message boards on this site making changes to the Directory index.
I made other changes to the bash file (following the tutorial) and eventually reloaded the original httpd but I still can't access php. How do i access php5 in my user directory? Thanks.
Either some modules are missing, or a misconfiguration in your httpd.conf file. The correct AddType for php is:
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
And make sure your module is loaded. Depending on the PHP version you are using, this is something like:
LoadModule php5_module modules/mod_php56.so
Restart your Apache and clear your browser cache before testing your php pages. Or try to upgrade to PHP 7. Unlink the old PHP and add the new:
brew tap homebrew/dupes
brew tap homebrew/versions
brew tap homebrew/homebrew-php
brew unlink php56
brew install php71
I had the same issue and it turned out to be a permissions error. Try creating a new folder and copy across all the files.

How do I access phpMyAdmin?

I installed phpMyAdmin on my computer. I used Apache as my http server. However, every time I go to http://localhost/phpMyAdmin/, this screen appears:
How do I make it so that this login screen appears instead:
You may be better off to install an integrated suite, such as:
XAMPP - Linux/Windows/Apple
* store web pages in htdocs
WAMP - Windows
MAMP - Apple
Then, just going to the address localhost will give you a menu, with all components (apache, phpmyadmin, tomcat, etc etc)
They are all free, so why not?
Add this
DirectoryIndex index.php
to /etc/phpmyadmin/apache.conf (or wherever your apache.conf or httpd.conf or whatever is).
If you happen to be running on Windows, look for the Apache webserver directory.
XAMPP Users: If you have installed it as a service, changes to configuration files seem to take effect only when you restart the service through the Services panel and not via the XAMPP control panel.
Use http://localhost/phpMyAdmin/index.php
or
Check your host config, check the DirectoryIndex param.
I found that when I entered root in the username and left the password blank I got in
You need to edit your apache2 config file to include PHPmyadmin.
Do this:
gedit /etc/apache2/apache2.conf
OR nano /etc/apache2/apache2.conf
Add this to the end of your config file:
Include /etc/phpmyadmin/apache.conf
Save then restart Apache:
/etc/init.d/apache2 restart

.htaccess not working in amazon ec2 ubuntu instance

I have a server from amazon's ec2 service running on Linux Ubuntu (
Ubuntu Server 13.04 64 bit) and I have installed apache, php, and mysql. I have added a .htaccess file in my document root (i.e /var/www/).
Here is the code in .htaccess file as follows:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
If I remove .php from url like "index1" instead of "index1.php", it returns 404 browser error. It works properly in my previous server.
I have .htaccess enabled in server. I did it using command "sudo vim /etc/apache2/sites-available/default" and changed "AllowOverride None" to "AllowOverride All".
I have also checked .htaccess working by passing invalid value in htaccess file and it returns "Internal server error - 500" in browser.
Here is the link of my server information : http://54.200.58.45/mytest.php
Any help in this regard will be highly appreciated.
This is wat worked for me on a fresh EC2 instance with Ubuntu 13.10:
a2enmod rewrite
vim /etc/apache2/sites-enabled/000-default.conf
add the following to the VirtualHost
<Directory "/var/www">
AllowOverride All
</Directory>
service apache2 restart
This happens because the rewrite module doesn’t come enabled by default for security reasons.
Create a new file called rewrite.conf in /etc/apache2/mods-enabled
in the file put this line LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so
Now reload server sudo service apache2 restart
This worked for me and hopefully for you, but I don’t advice this for production servers. This is information for regular Ubuntu users not for a live server.
The problem is by default in apache installed on Ubuntu doesn't have .htaccess enabled so first you have to manually enable it in a config file and restart apache.
Here are the steps.
1) Login to your EC2 instance with SSH.
ssh -i "Yourkey.pem" ubuntu#yourpublicip
2) Enable Mode Rewrite, this is just incase if it's not enabled.
sudo a2enmod rewrite
3) Go to the following directory.
cd /etc/apache2/sites-available
If you use LS command here you will see the following file.
000-default.conf
That's the file with default apache configuration which is applied to your sites in /var/www/html folder.
4) Open this file for editing.
sudo nano 000-default.conf
5) Add following lines after DocumentRoot /var/www/html line.
Options FollowSymLinks
AllowOverride All
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
6) Save the file and restart the apache.
sudo service apache2 restart
that's it and now your .htaccess file will work
You need to check whether the mod rewrite module is enabled or not first.
Tried everything and nothing worked...
2 hours later.. Finally fixed it.
So first the things to rule out.
Make sure that you have made the changes in /etc/apache2/apache2.conf and in /etc/apache2/sites-enabled/000-default.conf that everyone is talking about... (AllowOverride All and AccessFileName .htaccess must not be commented out)
Make sure that the Directory /var/www/html is pointing to the right folder. Some times it has /html and some times it's just /var/www (however it must not end in / i think, but that might not matter.)
Finally, and this is what worked for me. Restart the whole server, not just Apache. I figured out that this was my problem, after i tried stopping apache2 (service apache2 stop) and I was still able to access my website. So I did a full reboot and then all the changes I have been making for the last 2 hours, finally kicked in.
Success :)

Why is php not running?

I've seen this question answered many times, but most end either unanswered or by telling the asker to put this:
<?php phpinfo() ?>
in a test file. Obviously, if that produced what was expected, I wouldn't be here. Instead, I get a 404 error.
I'm using an ubuntu 12.04 server with Amazon. Apache is installed, php5 is installed, and apache was restarted. I followed the following sequence:
sudo apt-get install apache2
sudo apt-get install php5
sudo apt-get install libapache2-mod-php5
sudo /etc/init.d/apache2 restart
Each one of the first three commands now gives me "apache2 is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 3 not upgraded" Obviously, replace apache2 with php5 and libapache2-mod-php5 for the other two.
This is a sure way to tell me it's installed, correct? Well, when I use the command "top", php is not one of the services that are running, which tells me it's not running, correct?
Navigating to the IP address gives me Amazon's "It Works!" page, but navigating to any other page on the server produces a 404 error.
Any help is much appreciated.
Check out the apache config files. For Debian/Ubuntu theyre in /etc/apache2/sites-available/ for RedHat/CentOS/etc they're in /etc/httpd/conf.d/. If you've just installed it, the file in there is probably named default.
Make sure that the config file in there is pointing to the correct folder and then make sure your scripts are located there.
The line you're looking for in those files is DocumentRoot /path/to/directory.
For a blank install, your php files most likely needs to be in /var/www/.
What you'll also need to do is find your php.ini file, probably located at /etc/php5/apache2/php.ini or /etc/php.ini and find the entry for display_errors and switch it to On.
One big gotcha is that PHP is disabled in user home directories by default, so if you are testing from ~/public_html it doesn't work.
Check
/etc/apache2/mods-enabled/php5.conf
# Running PHP scripts in user directories is disabled by default
#
# To re-enable PHP in user directories comment the following lines
# (from <IfModule ...> to </IfModule>.) Do NOT set it to On as it
# prevents .htaccess files from disabling it.
#<IfModule mod_userdir.c>
# <Directory /home/*/public_html>
# php_admin_flag engine Off
# </Directory>
#</IfModule>
Other than that installing in Ubuntu is real easy, as all the stuff you used to have to put in httpd.conf is done automatically.
To answer the original question "Why is php not running?"
The file your browser is asking for must have the .php extension.
If the file has the .html extension, php will not be executed.
Type in browser localhost:80//test5.php[where 80 is your port and test.php is your file name] instead of c://xampp/htdocs/test.php.
When installing Apache and PHP under Ubuntu 14.04, I needed to specifically enable php configs by issuing a2enmod php5-cgi
You need to add the semicolon to the end of all php things like echo, functions, etc.
change <?php phpinfo() ?> to <?php phpinfo(); ?>
If that does not work, use php's function ini_set to show errors: ini_set('display_errors', 1);

How to force php to work in my browser?

I just installed PHP on my computer (Ubuntu). If I create a test.php file with simple code and then run "php test.php", it works fine (I get what I expect).
However, when I open this file (test.php) with my browser (Mozilla) it asks me how to open this file. Not a problem. I click "open with" and then I select "/usr/bin/php" and... nothing happens. Browser does not display anything.
Can anybody help me with that? Why does my browser cannot display a local php-file using a local php server?
You need to install a web server (apache, lighttpd, ngnix etc.) on your machine, then make sure the server is setup to process php files through the php interpreter. After you have done all that, copy the php files to your server web root directory and access them through the browser.
XAMPP is probably the easiest way to get a development environment up and running.
Here is forum article on getting it running on Ubuntu.
Do you have a web server installed also?
This will install a full LAMP stack on Ubuntu.
sudo tasksel install lamp-server
But keep in mind this will launch Apache and MySQL daemons in the background.
To disable when you are not developing:
sudo /etc/init.d/apache stop; sudo /etc/init.d/mysql stop
When you are:
sudo /etc/init.d/apache start; sudo /etc/init.d/mysql start
You can make a macro in your favorite IDE to do this automatically.
Removing it is as easy as:
sudo tasksel remove lamp-server
Vertex
PHP through a browser will only work if you have php running as a webserver module (cgi, fcgi)
If you are on a regular linux distro you can probably apt-get emerge or zypper apache2 and the mod_php module from a repository. If you want to build from source, here is an small howto:
apache2php
If you're on *nix, try
locate httpd.conf
to find the apache configuration file. It should be somewhere like /etc/apache2/httpd.conf.
In the httpd.conf file, you need to do two things. First load the PHP module eg.
LoadModule php5_module libexec/apache2/libphp5.so
Then you also need a PHP file handler eg.
# PHP file handlers.
<IfModule php5_module>
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>
</IfModule>
To test it's running OK, start apache with the .conf file as a parameter and load up a .php file that contains:
<?php
phpinfo();
?>
If PHP is running ok under apache, you should see a purple/white/black page listing all the settings currently in use.
See http://php.net/manual/en/configuration.changes.php for details of the myriad settings you can use.
Make sure you restart apache each and every time you change the .conf file!!!!

Categories