Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincej's avatar
Level 15

Can't get Ubuntu / Apache to render application

For those of you all who saw an earlier post from me on the topic of Bootstrap not loading properly, I have come to the conclusion that I have a problem with Apache on my Ubuntu server on the grounds that my application will render using the Laravel internal web server, but not Apache. I created a brand new fresh Laravel 10 test site. I have enabled the site. and I have set my permissions to 775 except for storage and bootstrap which are 777. This is a development server, so I will tighten things later. Advice Please. :o)

Ok, I created a Laravel 10 server, named "test". When I load http://test.local I am getting the index.php and that is all:

<?php

use Illuminate\Contracts\Http\Kernel;
use Illuminate\Http\Request;

define('LARAVEL_START', microtime(true));

/*
|--------------------------------------------------------------------------
| Check If The Application Is Under Maintenance
|--------------------------------------------------------------------------
|
| If the application is in maintenance / demo mode via the "down" command
| we will load this file so that any pre-rendered content can be shown
| instead of starting the framework, which could cause an exception.
|
*/

if (file_exists($maintenance = __DIR__.'/../storage/framework/maintenance.php')) {
    require $maintenance;
}

/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| this application. We just need to utilize it! We'll simply require it
| into the script here so we don't need to manually load our classes.
|
*/

require __DIR__.'/../vendor/autoload.php';

ETC ETC

Ok, So I have tried various virtual host settings, all to no avail. Here is my current one:

<VirtualHost *:80>
         ServerName test.local
         ServerAlias www.test.local
         DocumentRoot /var/www/test/public

         <Directory "/var/www/test/public">
		   Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
            Require all granted
         </Directory>
    
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

And my hosts file:

127.0.0.1	localhost
127.0.0.1	test.local
0 likes
22 replies
LaryAI's avatar
Level 58

It looks like you've set up your virtual host and hosts file correctly, but the issue might be related to Apache not interpreting the PHP code and instead serving the index.php file as plain text. This can happen if the PHP module is not enabled in Apache or if the .htaccess file is not being read correctly.

Here are some steps to troubleshoot and resolve the issue:

  1. Enable mod_rewrite: Make sure that the mod_rewrite module is enabled in Apache. This module is necessary for the .htaccess file to work properly.

    You can enable it by running the following command:

    sudo a2enmod rewrite
    
  2. Restart Apache: After enabling mod_rewrite, you need to restart Apache for the changes to take effect.

    sudo systemctl restart apache2
    
  3. Check .htaccess: Ensure that the .htaccess file is present in your /var/www/test/public directory and that it is the default one provided by Laravel. If it's missing, you can get a fresh copy from a new Laravel installation or from the Laravel repository.

  4. Enable PHP module: Make sure that the PHP module is enabled in Apache. You can enable it with the following command:

    sudo a2enmod php<version>
    

    Replace <version> with the version of PHP you are using (e.g., php7.4).

  5. Check Apache Configuration: Ensure that the AllowOverride directive is set to All within your <Directory> block in the virtual host file, which it is as per your provided configuration. This allows .htaccess files to override server configuration settings.

  6. Permissions: You mentioned setting permissions to 775 and 777. While this is okay for a development server, make sure that the owner of the files is the Apache user (usually www-data on Ubuntu). You can set the correct ownership with:

    sudo chown -R www-data:www-data /var/www/test
    
  7. Check Apache Error Logs: If none of the above steps work, check the Apache error logs for any specific errors that might be occurring. You can view the error log with:

    sudo tail -f /var/log/apache2/error.log
    
  8. Ensure PHP is Working: Create a phpinfo.php file in your /var/www/test/public directory with the following content to ensure PHP is working:

    <?php phpinfo(); ?>
    

    Then navigate to http://test.local/phpinfo.php in your browser. If PHP is configured correctly, you should see the PHP information page.

  9. Clear Laravel Cache: Sometimes, Laravel cache can cause issues. Clear the cache by running these commands from your project root:

    php artisan config:clear
    php artisan cache:clear
    php artisan view:clear
    php artisan route:clear
    

After following these steps, try accessing http://test.local again. If the problem persists, please provide any error messages you're seeing in the Apache error log for further assistance.

vincej's avatar
Level 15

@Tray2 thanks for that. I will check it out! I spent the afternoon with PHP Storm/Jetbrains AI assistant. Very frustrating experience. It just went around and around in circles without a solution. The crazy thing is my Ubuntu server used to work just fine 5 months ago.

jlrdw's avatar

@vincej Like I mentioned in other posts you have to chown for yourself (sudo user).

vincej's avatar
Level 15

@jlrdw Thanks for that. Can you clarify, Apache requires www-data as the user.

jlrdw's avatar

@vincej this article will help:

https://www.computernetworkingnotes.com/linux-tutorials/xampp-htdocs-permission-issue-and-fix-in-ubuntu.html

I just ran:

sudo chown -R sudo_user:sudo_user /opt/lampp/htdocs
sudo chmod 755 -R /opt/lampp/htdocs

Change sudo_user to actual sudo user.

Example your user name is billybob.

so:

sudo chown -R billybob:billybob /opt/lampp/htdocs

SImilar for other folders as needed. Development only here.

Edit:

Also Digital Ocean has articles on setup for production.

https://www.digitalocean.com/community/tutorials/how-to-install-the-apache-web-server-on-ubuntu-22-04

jpmg's avatar

@vincej I had a similar problem and with a guide that I got I can make it work correctly, I leave you the link https://www.digitalocean.com/community/tutorials/how-to-install-the-apache-web-server-on-ubuntu-22-04 from point 5 was the solution.. Currently this works perfectly for me.

 <VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName cronoapp.net
    DocumentRoot /var/www/html/cronoapp/public

    <Directory /var/www/html/cronoapp>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
           Order allow,deny
           allow from all
           Require all granted
</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
erikverbeek's avatar

It might also help you to have a look in the /var/log/apache2/ folder. That's where Apache logs incoming requests and generated errors. Those might give you a clue to which setting is currently misconfigured.

vincej's avatar
Level 15

Ok Guys, I have been working on this problem and I have quadrupled checked virtual hosts and hosts file. @erikverbeek looking at the apache error logs I am getting this error:

PHP Fatal error:  Uncaught Error: Call to undefined function env() in /var/www/contraxiq/public/index.php:8

Line 8 of the index.php file is: echo env('APP_ENV');'

My .env file is good, at least Intelj AI Assistant thinks so.

be advised I cloned this application code, from my MacBook/ valet where it works perfectly. Of course with a clone you don't bring down a .env file so this file was created specifically for this Ubuntu / Apache platform.

I created a small test site on my Ubuntu server with just some PHP and some html and it rendered fined.

any suggestions??

JussiMannisto's avatar

@vincej Index.php is the entry point of the application. Nothing is yet loaded at that point, and that includes the env() helper function.

You shouldn't have any application logic in index.php. You should only touch that file if you want to change how the application is bootstrapped.

vincej's avatar
Level 15

@jlrdw yes, I have vince : www-data and 755 permissions.

I have a Laravel app running nicely on DigitalOcean. I even open that up and copied the virtual host and check the hosts file.

I also deleted Apache and reinstalled it.

I have tried everything. But a simple php file works fine. I also created a brand new vanilla L10 install just for testing purposes. Nothing with Laravel works. It looks like a Laravel problem Ugh.

@jussimannisto I don't have any logic running in index.php. This is a vanilla application which works perfectly on my MacBook.

JussiMannisto's avatar

@vincej A vanilla app wouldn't have an env() call at the start of index.php because that would crash. That's what happened to you:

PHP Fatal error:  Uncaught Error: Call to undefined function env() in /var/www/contraxiq/public/index.php:8
JussiMannisto's avatar

@vincej Well yes. It doesn't belong there for sure.

But if you don't know how it got there, that's a bit concerning.

vincej's avatar
Level 15

@JussiMannisto I have been working on this for days using the intelij AI assistant in PHPStorm. What a waste of time that is! I am accessing the server which is remote on my lan and so I can not display a result on my MacBook browser. I did try http://local-ip/mysite but got nothing. More positive, I did manage to ping the site from my Macbook which is a first!

JussiMannisto's avatar

@vincej If you use an IP address to access the site over LAN, Apache doesn't know which virtual host the request should be routed to. The requested hostname should match the ServerName (or ServerAlias) directive in your virtual host config.

You could try adding a wildcard ServerAlias to catch all traffic:

ServerAlias *

That is, unless you have configured other virtual hosts on the same port and this causes conflicts.

vincej's avatar
Level 15

@JussiMannisto I just went down to my home office ie the Ubuntu server / Apache and tried the Laravel app having deleted the function eval(). Nope - did not help. Still nothing. This is driving me crazy. I forgot to say that today I entirely purged Apache from my system as reinstalled it. Tomorrow I am going to delete the app from the system and reinstall an empty L10 just to see if I can get the welcome page! Ugh! I know my install is good as I used the DigitalOcean tut. I repeating myself. I can load a sample file with phpinfo() and it renders fine as too HTML. this has to be a Laravel issue, no?

Thanks for all your help!

JussiMannisto's avatar

@vincej What happens exactly when you try to try to access the site? Does the request time out or do you get a server error? Do you see anything in your Apache logs or storage/laravel.log?

vincej's avatar
Level 15

@JussiMannisto When I access the site, I just a blank page with "can not access the site"> I've shut down the Ubuntu server now as I am in western Canada and it it 11:35PM. I'll have a look tomorrow. Cheers!

vincej's avatar
Level 15

SUCCESS! I have been using Linux for years, but since I purged everything and reinstalled, I was essentially starting from fresh. Essentially the problem was that when you install PHP Laravel requires a whole mountain of additional features and modules which I had forgotten about. They are not listed in L10 docs. Once I found and installed them all I was off the the races!

I find the L10 docs focuses on the use of Docker when it is not a prerequisite. I think the docs could be improved by a description of installing the "old school way":

sudo apt-get install php8.3 php8.3-xml php8.3-mbstring php8.3-mysql php8.3-curl php8.3-gd php8.3-common

Please or to participate in this conversation.