Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
379 views
in Technique[技术] by (71.8m points)

php - How to configure apache web server to deploy laravel 5

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...