-2

I am a neophyte on Apache, but I see some similar questions to this list and hope you can help.

I just want to set up a basic local LAMP system under my Ubuntu 18.04 LTS (Asus VivoBook S15) to test out a website that I have to modify before playing with the version on my commercial ISP host.

The basic installation went fine (Apache/2.4.29, PHP 7.2) and both html and php could be processed from the var/www/html folder through the url localhost. I then did the following to set up public_html access:

i) add in /etc/apache2/apache2.conf the line:

ServerName localhost:80

ii) activate the the UserDir module (sudo a2enmod userdir) and edit the file /etc/apache2/mods-enabled/userdir.conf as follows :

<IfModule mod_userdir.c>
    UserDir public_html
    UserDir disabled root

    <Directory /home/*/public_html>
 #      AllowOverride FileInfo AuthConfig Limit Indexes
    AllowOverride All
 #      Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
    Options ExecCGI Indexes MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
        Require method GET POST OPTIONS
    </Directory>
</IfModule>

iii) activate default virtual user directory (sudo a2ensite 000-default.conf) and edit the file /etc/apache2/sites-enabled/000-default.conf as follows:

 # Global configuration
    ServerName localhost
 # without port according to recommendation found on the web

and

#ServerAdmin webmaster@localhost
ServerAdmin john@john-VivoBook
#DocumentRoot /var/www/html
DocumentRoot /home/john/public_html

iv) created the public_html directory under my home directory (/home/john) with ownership john:john and permissions 755 (also tried with 777), and added my index.html and index.php files

v) restarted Apache (sudo apache2ctl restart)

vi) configured my Firefox browser not to add www to urls.

When I try to access the url "localhost" I get a blank screen, whereas with "localhost/public_html", "localhost/public_html/index.html" or "localhost/public_html/index.php" I get the message:

Not Found
The requested URL was not found on this server.
Apache/2.4.29 (Ubuntu) Server at localhost Port 80

No errors in the log file /var/log/apache2/error.log

The tail of the log file /var/log/apache2/access log says:

127.0.0.1 - - [05/Apr/2020:13:24:44 +0200] "GET /public_html HTTP/1.1" 404 488 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:74.0) Gecko/20100101 Firefox/74.0"
127.0.0.1 - - [05/Apr/2020:13:26:41 +0200] "GET /public_html/index.html HTTP/1.1" 404 488 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:74.0) Gecko/20100101 Firefox/74.0"
127.0.0.1 - - [05/Apr/2020:13:27:29 +0200] "GET /public_html/index.php HTTP/1.1" 404 488 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:74.0) Gecko/20100101 Firefox/74.0"

The log file /var/log/apache2/other_vhosts_access.log does not exist.

What am I doing wrong?

Thanks and best regards, John

user13228110
  • 1
  • 1
  • 3

2 Answers2

3

If you want to use 'userdir' feature, you should just install apache2 and enable 'userdir'. You don't have to change the DocumentRoot.

sudo apt install apache2

sudo a2enmod userdir

sudo systemctl restart apache2

Now, switch to the user (in your case, john)

mkdir /home/john/public_html

echo 'hello' >> index.html

And you can now access index.html using "http://localhost/~john/index.html"

If you change your DocumentRoot to /home/john/public_html, then you should access the html page through "http://localhost/index.html", because the directory(/home/john/public_html) is already the Root.


Update after your comment

If you want php work with apache2 user directory feature.

First, install php environment.

  1. sudo apt install php7.3

  2. sudo a2enmod php7.3

Second,

sudo vim /etc/apache2/mods-enabled/php7.3.conf

Comment below block to re-enable PHP in user directories.

#<IfModule mod_userdir.c>
#    <Directory /home/*/public_html>
#        php_admin_flag engine Off
#    </Directory>
#</IfModule>

Third, restart apache2 service.

sudo systemctl restart apache2

You should see the page working perfect through http://localhost/~john/index.php

Michael Ouyang
  • 1,767
  • 1
  • 11
  • 19
  • Thanks so much Michael - effectively http://localhost/index.html works fine. But http://localhost/index.php changes to www.localhost/index.php (even though I in principle deactivated this for Firefox) and yields a blank screen while http://localhost/ also yields a blank screen (without adding www). So almost there, how to go the last mile? – user13228110 Apr 05 '20 at 14:47
  • I had /etc/apache2/mods-available/dir.conf set to give preference to index.php but when I change to preference to index.html, no difference, won't call automatically from the document root. – user13228110 Apr 05 '20 at 14:50
  • Still hoping that someone can help on this. I am wondering whether there is something that the PHP package is not getting properly, perhaps can be fixed by .htaccess ? In the meantime I have gotten a satisfactory alternative result by changing the owner of var/www/html to john:john, creating a symbolic link to it in home/john, and changing the link's owner to john:john . But this has the disadvantage of filling my root partition with user data. Strange that the so-called default option of using public_html is so complicated to set up in practice ??!! – user13228110 Apr 06 '20 at 12:23
  • This is a problem with Firefox. I installed Opera and all works fine. Any ideas? – user13228110 Apr 07 '20 at 13:50
  • @user13228110 It's the default security feature in Firefox. FYI https://support.mozilla.org/en-US/questions/1233981 – Michael Ouyang Apr 11 '20 at 04:33
  • Thanks so much Michael, but this did not help. I had already set ***browser.fixup.alternate.enabled*** and ***browser.urlbar.autoFill*** to false to stop autoediting of the location bar (the latter did not seem to do more relative to the former). No change when I also change ***network.dns.blockDotOnion*** to false: still blank screens for http://localhost and http://localhost/php . It may be that something is coming up before the blank screen but I can't really tell and don't know how to record it (tried Firefox debugger but maybe lack expertise on exploiting it). – user13228110 Apr 12 '20 at 08:24
  • But Opera is not perfect yet either. Although http://localhost , http://localhost/index.html and http://localhost/index.php work fine, calling php files embedded in directory structure returns the text of the file instead of executing. I am working this out, but it seems strange to me that while the symbolic link from var/www/html works simply and perfectly, public_html which should be simple according to the doc is fraught with undocumented problems. My gut feeling is that the main problem is coming from PHP and could be better to try to fix that instead of fiddling with the browser? – user13228110 Apr 12 '20 at 08:26
  • Update the Answer. – Michael Ouyang Apr 12 '20 at 09:17
  • For me there is not yet an answer, still working to overcome the problems using Firefox and Opera, in meantime further ideas, especially on Firefox or on the generic Apache/PHP configuration, would be most welcome. – user13228110 Apr 13 '20 at 19:10
0

I am now not sure that PHP was partially OK under Opera, since my index.php code was very short and I may well have mistaken a listing for an output. At any rate I upgraded opera-stable to version 67.0.3575.137 (remember that I have Apache/2.4.29 running under Ubuntu 18.04 LTS and PHP version 7.2.24), and all php files including http://localhost then showed listings rather than executing.

[Sorry, I've struggled for a half hour trying without success to show "sharp" (comment) signs and initial triangular brackets in code, and so am abbreviating without some code.]

I then did the following two steps (both taken from Apache shows php code instead of executing):

  • edit /etc/apache2/mods-enabled/php7.2.conf to comment out five lines as per the instructions after the commented line "Running PHP scripts in user directories is disabled by default":

At this point http://localhost worked but direct calls to php files still yielded listings only.

  • edit /etc/apache2/mods-enabled/mime.conf and after the following two commented lines:

comment: AddType allows you to add to or override the MIME configuration

comment: file mime.types for specific file types.

add the following lines:

AddType application/x-httpd-php .php
AddHandler application/x-httpd-php .php

With these two changes, my local php-based website seems to be working fine with both Opera and with Firefox 75.0 64 bits (previously blank screens for http://localhost and for explicit calls to php files).

user13228110
  • 1
  • 1
  • 3