Keep Server Online
If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.
or
A donation makes a contribution towards the costs, the time and effort that's going in this site and building.
Thank You! Steffen
Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
| |
|
Topic: Virtual host alias: 404 for index.php |
|
Author |
|
Amplifiction
Joined: 31 Aug 2024 Posts: 5 Location: Belgium
|
Posted: Sun 01 Sep '24 15:25 Post subject: Virtual host alias: 404 for index.php |
|
|
Hello all,
Starting webdeveloper here, who wants to showcase a few websites to potential employers. To that end, I have set up a Linux (Mint) server and installed a LAMP stack on it, including SSL (CertBot) and ModSecurity.
I have set up a virtual host for my_domain.net. Browsing to https://my_domain.net successfully loads the index.html located under /var/www/my_domain.net.
I want to showcase a Laravel project, let's say under https://my_domain.net/laravel. For security reasons, I can't put the entire project under /var/www/my_domain.net/laravel. And so I have been trying to "link" the https://my_domain.net/laravel url to the 'public' subfolder of my project. I have tried to achieve this through symlinks and aliases. Since I would prefer to get this done with an alias, I'll be focusing on that method.
To rule out any problems originating in the complex Laravel project structure, I have created a /websites/test folder containing only a simple index.php file. (I have moved the 'websites' directory from my home folder to ensure that there are no ownership issues in the entire path.) The owner of the 'websites' folder and all of its contents is currently www-data, achieved by
Code: | sudo chown -R www-data:www-data /websites
sudo chmod -R 755 /websites |
(I have also tried setting my own Linux user as owner, to no avail.)
The full content of /etc/apache2/sites-available/my_domain.net.conf:
Code: | <VirtualHost *:443>
ServerName my_domain.net
ServerAlias www.my_domain.net
ServerAdmin webmaster@localhost
DocumentRoot /var/www/my_domain.net
RewriteEngine on
RewriteCond %{SERVER_NAME} =my_domain.net
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
Alias /test /websites/test
<Directory /websites/test>
Options Indexes FollowSymLinks
AllowOverride All
# This allows .htaccess files to override configurations.
Require all granted
SecRuleEngine Off
# Turns off ModSecurity for directory. To be disabled in production.
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost> |
'sudo apachectl configtest' outputs:
Code: | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
Syntax OK |
I'm not sure if this is relevant, but my "domain" is actually a No-Ip account. (So DDNS pointing to my IP.)
The website has been enabled using 'sudo a2ensite my_domain.net'.
Apache2 is running and I have restarted it whenever I made changes. ('sudo systemctl restart apache2')
Rewrite has been enabled by 'sudo a2enmod rewrite'.
The problem: browsing to https://my-domain.net/test results in a 404 error. What am I getting wrong? |
|
Back to top |
|
Stray78
Joined: 15 Apr 2024 Posts: 23 Location: USA
|
Posted: Mon 02 Sep '24 6:28 Post subject: |
|
|
Quote: | # Alias: Maps web paths into filesystem paths and is used to
# access content that does not live under the DocumentRoot.
# Example:
# Alias /webpath /full/filesystem/path |
Code: | /laravel /var/www/my_domain.net/laravel |
|
|
Back to top |
|
Amplifiction
Joined: 31 Aug 2024 Posts: 5 Location: Belgium
|
Posted: Mon 02 Sep '24 10:53 Post subject: |
|
|
Stray78 wrote: | Quote: | # Alias: Maps web paths into filesystem paths and is used to
# access content that does not live under the DocumentRoot.
# Example:
# Alias /webpath /full/filesystem/path |
Code: | /laravel /var/www/my_domain.net/laravel |
|
Amplifiction wrote: | For security reasons, I can't put the entire project under /var/www/my_domain.net/laravel. And so I have been trying to "link" the https://my_domain.net/laravel url to the 'public' subfolder of my project. |
I don’t quite understand your reply. If I were to use /var/www/my_domain.net/laravel, why would I need an alias in the first place?
For example, I have just created the folder /var/www/my_domain/no_alias, which only contains a simple .php file. Browsing to https://my_domain/no_alias loads that .php fine. Didn't even have to create an alias. (Or change ownership from my user to www-data, which surprised me.)
To clarify, my Laravel project consists of multiple subfolders, and the subfolder ‘public’ contains files the visitor should have access to. The rest, like the sqlite database, should not be accessible. |
|
Back to top |
|
Amplifiction
Joined: 31 Aug 2024 Posts: 5 Location: Belgium
|
Posted: Mon 02 Sep '24 17:59 Post subject: Re: Virtual host alias: 404 for index.php |
|
|
Solved by moving the Alias and <Directory> directives from
/etc/apache2/sites-available/my_domain.conf (virtual host config)
to
/etc/apache2/sites-available/my_domain-le-ssl.conf (virtual host config for https)
Also reverted back to <VirtualHost *:80> in my_domain.conf. |
|
Back to top |
|
James Blond Moderator
Joined: 19 Jan 2006 Posts: 7373 Location: Germany, Next to Hamburg
|
|
Back to top |
|
Amplifiction
Joined: 31 Aug 2024 Posts: 5 Location: Belgium
|
Posted: Tue 03 Sep '24 19:45 Post subject: Re: Virtual host alias: 404 for index.php |
|
|
Why not use a simple password protection via Require user? [1][2]. Not need to change the other working config.
[1] https://httpd.apache.org/docs/2.4/mod/mod_authz_user.html
[2] https://httpd.apache.org/docs/2.4/mod/mod_auth_basic.html[/quote]
I'm not sure what you're suggesting. You mean putting the entire project under https://my_domain.net/laravel, and password protecting all files and folders there, except for the public folder? Wouldn't that still leave me with the challenge of making sure that browsing to https://my_domain.net/laravel opens https://my_domain.net/laravel/public/index.php? So I would still need an alias, I think?
On top of that, I have discovered a second challenge. While browsing to https://my_domain.net/laravel finally loads the /websites/laravel/public/index.php file, images and other pages are not found.
In the Apache access log, I see that a get request is being made for /images/x.jpg. When I compare this to the working /test alias I set up, this should be /laravel/images/x.jpg.
To clarify, my virtual host file contains two similar aliases. The only differences are the alias name, as well as the folder name.
Code: |
Alias /laravel /websites/laravel/public
<Directory /websites/laravel/public>
AllowOverride All
# This allows .htaccess files to override configurations
Require all granted
# Access is allowed to everyone.
# SecRuleEngine Off
# Turns off ModSecurity for directory. To be disabled in production.
</Directory>
Alias /test /websites/test/public
<Directory /websites/test/public>
AllowOverride All
# This allows .htaccess files to override configurations.
Require all granted
# Access is allowed to everyone.
# SecRuleEngine Off
# Turns off ModSecurity for directory. To be disabled in production.
</Directory>
|
Both public folders contain an index.php file, as well as an images subfolder. For /test, the images load fine. For /laravel, not so.
I find this bizarre, as I have recently successfully deployed the Laravel project on the servers of a (soon to be paid) webhosting firm. There, I simply added a subdomain (laravel.my_domain.net) and using a GUI, "linked" that subdomain to the project's public folder. By which I don't mean to judge either the firm nor Apache; simply pointing out that the project worked fine, albeit in a different environment.
Anyway, I'm not insisting on aliases or any other method. I would only like to learn how to deploy Laravel projects to my_domain.net/x URL's, as I do not have the luxury of subdomains. Any advice on how to achieve this would be greatly appreciated, as I have spent many hours trying to do so. |
|
Back to top |
|
James Blond Moderator
Joined: 19 Jan 2006 Posts: 7373 Location: Germany, Next to Hamburg
|
|
Back to top |
|
Amplifiction
Joined: 31 Aug 2024 Posts: 5 Location: Belgium
|
Posted: Thu 26 Sep '24 20:06 Post subject: |
|
|
I solved this a few weeks ago, and realized I hadn't provided an update.
In the end I adjusted the code of the project. I also kept the alias. No other changes, like for example to .htaccess.
While I have seen tutorials/guides that (claim to) solve this problem for pure Laravel projects without changing the code, apparently this isn't possible with Inertia.
Using Inertia's shared data, I configured a prefix string ('/laravel') that I added in front of routes in my React components. At least this way, the prefix can be easily adjusted if it ever gets deployed to another website.
Thanks to everyone who took the time to help me. |
|
Back to top |
|
|
|
|
|
|