Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure apache web server to deploy laravel 5

Tags:

php

apache

I would like to serve my Laravel application on my local apache web server. However, I am having issues.

To test if the application I made would work on an apache server, I have created a new very simple application which contains two routes.

Route::group(['middleware' => 'web'], function() {
    Route::get('/', function() { return view('myview'); });
    Route::get('/link', function() { return view('myotherview'); });
});

When I enter my public directory from my browser, it works fine, it connects to the / route. But when I give a link to the other route (/link), and try enter that route, it gives me 404 not found the error. Here is the link I give in my myview view to reaching /link route:

<a href="{{ url('/link') }}">Go</a>

When I show the source of the page, the above line is rendered as localhost/mylaravelapp/public/link.

I have researched this issue on the internet and there are a couple of suggestions on enabling apache mod_rewrite. I have also done that by typing a2enmod mod_rewrite. However this isn't seemed to be working, getting the same result. How can I solve this issue?

My laravel version is 5.2, apache 2.4.7 and I am using xubuntu 14.04.

like image 977
Bora Avatar asked Feb 19 '16 08:02

Bora


People also ask

Can Laravel run on Apache?

For Laravel to work, you'll need Apache. It is one of the most popular HTTP server tools, so it's likely that your VPS has it installed. Luckily, you can check easily! Once you connect to your server using SSH, verify that an Apache system service exists.


1 Answers

This is the usual configuration that works for me (same OS, Apache and Laravel version as you have). Edit the apache2 config file (it should be under /etc/apache2/sites-available/000-default.conf), adding this:

Alias /yourdir /var/www/html/yourdir/public/
<Directory "/var/www/html/yourdir/public">
        AllowOverride All
        Order allow,deny
        allow from all
</Directory>

Where "yourdir" is obviously your folder under the /var/www/html path. Restart your server after you modified the config file

sudo service apache2 restart

Now your public/.htaccess should be something like:

RewriteEngine On
RewriteBase /yourdir/

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Authorization Headers
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
like image 166
Damien Pirsy Avatar answered Sep 28 '22 10:09

Damien Pirsy