Visting a laravel project with localhost via apache

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-23 04:56:37

问题


I have just installed a laravel project into my Home/public_html folder. I gave it a name, let's call it laravel for this purpose. So the template installed to Home/public_html/laravel and then I ran a composer install. I got a database password error. I fixed that, ran it again, and all went well. I then created a migration, a controller, and a view. I tried to navigate to it via localhost/laravel/public/events (events is my created view) I get a 404. I remembered I needed a config file in etc/apache2/sites-available I created the config file with this structure:

<VirtualHost *:80>

ServerAdmin webmaster@localhost
DocumentRoot /home/user/public_html/laravel/public/

<Directory /home/user/public_html/laravel/public/>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
</Directory>

ErrorLog ${APACHE_LOG_DIR}/laravel-error.log
CustomLog ${APACHE_LOG_DIR}/laravel-access.log combined

</VirtualHost>

I then created a symlink into the sites-enabled folder. That didn't help. I then navigated to localhost/laravel and it just gives me the directory structure.

What have I missed? What have I done wrong?

I have another folder inside public_html with its own config file that works perfectly (it points to home/user/public_html/. It also has a symlink. Is there something in each project that tell the project which config to use? Do they just work? My knowledge of this really stops here. Basically, I've exhausted what I know. What am I missing/doing wrong?

Oh and I have restarted apache as well. - a couple of times through the steps I just detailed.


回答1:


Exposing the Laravel instalation to the web server is never a good idea. Since you can access the file listing of the laravel folder, I believe your document root is set to /home/user/public_html.

Better approach to solve this problem will be as follows.

  1. Keep entire laravel installation outside public_html directory. Let's assume inside /home/user/development

  2. You may need to add following to the apache configuration below DocumentRoot.

    Alias /laravel /home/user/development/laravel/public
    
  3. Add following Directory Configuration

    <Directory /home/user/development/laravel/public/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    
  4. Add following to .htaccess file in /home/user/development/laravel/public/ below RewriteEngine rule.

     RewriteBase /laravel
    

Restart the apache2 server, your site should be available on localhost/laravel. Make sure there are no folder named laravel inside the public_html folder.



来源:https://stackoverflow.com/questions/23622219/visting-a-laravel-project-with-localhost-via-apache

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!