If you like DNray Forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...

 

PHP code is not executed

Started by pusan, Jan 09, 2023, 03:20 AM

Previous topic - Next topic

pusanTopic starter

I have been struggling all day with the PHP code on Apache (localhost) that refuses to run. I am using Ubuntu 18.04, php 7.2.24 and Apache 2.4.29, which is running smoothly and displaying a page from /var/www/html/index.html. I have tried placing a simple <?php echo phpinfo(); ?> at the end of the body of this page but it seems to be ignored.

After using the a2query -m php 7.2 command, I found out that "php 7.2 (enabled by maintainer scripts)".

Despite restarting the Apache server ten times, I still can't seem to fix the problem. I would greatly appreciate any help you can give me.

PS. Just a side note: console utilities in PHP seem to work fine on this machine.
  •  


john.vanis5

You can either use the .php extension or add the following line:

AddHandler application/x-httpd-php72 .html

to your code to enable PHP. An example of how to add PHP code to your page is shown below:

<?php
phpinfo
();
?>
  •  

katebonnerwrite

To enable PHP, you can modify the Apache config file or the virtual host config file. You can also keep the .htaccess file if you plan on switching to ols. It is highly recommended to use pretty URLs and set one access point through index.php with normal routing.

It's important to note that using self .php is not secure since hackers can easily bypass any file extension. It is better to take extra security measures to prevent such attacks.
  •  

gomez

To make a change of access rights to EVERYTHING in a folder, you can modify the permission levels. In the figure 755, the first digit represents the rights of the owner, the second digit is the rights of the group to which the owner belongs, and the third is the rights of others. The digit is a simple sum of three numbers, with 4 allowed to read, 2 allowed to write, and 1 allowed to perform.

Giving 4 allows reading, adding a two allows writing as well, and adding another one allows performing. Therefore, 755 means that everything is possible for the owner, the group can read and perform, and others can also read and perform. This raises the question of why the owner needs to be appointed at all if everyone else can read and perform.

It is important to note that php files themselves have a bit of execution, but only to the owner. The recording rights are necessary for Apache to overwrite php files, but it's not required. In fact, 400 rights are enough for files. However, directories need a bit of execution to enter them, but only for the owner. For directories, the appropriate rights are 500.
  •  

sammyf

Firstly, make sure that Apache is properly configured to handle PHP files. Apache needs to be aware of the PHP module and how to handle files with a .php extension. This is typically done through the use of the `php7.2` module in Apache.

Check the Apache configuration files, particularly `httpd.conf` or `apache2.conf`, to ensure that the PHP module is loaded and configured correctly. Look for lines similar to the following:

LoadModule php7_module modules/libphp7.so
AddHandler php7-script .php


These lines instruct Apache to load the PHP module and how to handle .php files. If these lines are missing or incorrect, Apache may not be able to interpret PHP code correctly.

Next, verify that Apache is aware of the index.php file as a default index file. This is typically set in the `DirectoryIndex` directive within the Apache configuration. Ensure that index.php is included in this directive:

DirectoryIndex index.php index.html


This ensures that when a directory is accessed, Apache will look for index.php as a default file.

After making changes to the Apache configuration, it is essential to restart the Apache server to apply the new configuration:

sudo systemctl restart apache2

Once the server has been restarted, create a test PHP file in the web server dоcument root, typically located at `/var/www/html/`, and name it info.php. Add the following code to this file:

<?php
phpinfo
();
?>


Now, access this file via a web browser using the URL `http://localhost/info.php`. If the PHP info page is displayed, then PHP is successfully integrated with Apache.

If the PHP info page is not displayed, check the Apache error logs for any messages related to PHP or the web server. The error logs can provide valuable information about what might be causing the PHP code to be ignored.

Additionally, ensure that the file permissions on the PHP files and the web server dоcument root are correctly set. Incorrect file permissions can prevent Apache from reading and executing the PHP code.

Remember to approach this troubleshooting process systematically, as there can be multiple factors causing the issue. If you continue experiencing difficulties, seeking the help of a server administrator or a community of experts in web development and server administration might provide additional insights and solutions.
  •  


If you like DNray forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...