Api returns data correctly by Artisan serves but not by apache

Asked

Viewed 54 times

2

I made a REST api using Laravel, for an application I’m doing, to perform the request tests I created a VM where the api is.

Assign a fixed IP to this VM and disable the firewall to not disturb.

I set it up like this:

In /etc/apache2/sites-enabled/000-default.conf:

<VirtualHost *:80>

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

<VirtualHost *:80>
    ServerName api.dev
    DocumentRoot /var/www/html/apiTeste/public
</VirtualHost>

<Directory "/var/www/html/apiTeste">
    AllowOverride All
</Directory>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

In /etc/hosts:

127.0.0.1   localhost
127.0.1.1   ouvidoria
127.0.0.1   api.dev

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

Performing test within the VM itself, if used http://api.dev this returns the home page of Laravel, but if used http://api.dev/api/unidades this returns a 404 Not Found:

Not Found

The requested URL /api/unidades was not found on this server.

Apache/2.4.25 (Ubuntu) Server at api.dev Port 80

But if I spin the php artisan serve and access http://api.dev:8000/api/unidades this returns the data correctly.

  • 1

    Apache mod_rewrite is enabled?

  • 1

    Thanks @Adrianodeazevedo, I searched here about enabling mod_rewrite and after performing the procedure worked

  • @Adrianodeazevedo if you want to formalize the answer, or else I will put a

  • 1

    I will add an answer.

1 answer

2


Laravel uses the route concept so you can access your application.

Check if the extension mod_rewrite apache is enabled.

<?php
// teste.php
print_r(apache_get_modules());
?>

If you check that the function is not defined it is because the extension is not enabled in Apache.

So, we will need to enable the apache module using the command a2enmod. In the terminal type:

sudo a2enmod rewrite

Done this, restart your apache:

sudo service apache2 restart

Browser other questions tagged

You are not signed in. Login or sign up in order to post.