For new Laravel users, it is often difficult to configure the server and domain correctly, especially with /public case. This article will explain and try to help you.


Why is /public needed?

By default, the web server loads index.php file, right? So why couldn’t the Laravel framework just have this index file in the main folder? For what /public?

The answer is security. The folder name explains it all: it’s the only folder that would be publicly accessible from the browser. So whatever you put in /publicas /public/css, /public/js Or /public/images/products – these will be accessible simply by typing the URL into the browser.

At the same time, it protects all other folders one level above.
SO /application, /config, /resources folders will not be able to be loaded from the browser, regardless of what URL someone enters.

In other words, all your application logic is hidden from the browser and you only allow access to the /public folder.


How to configure the /public folder?

Another mistake I see from new Laravel developers is that they load the URL like this: – so public is part of the URL. It’s possible and might even work locally, but can you imagine someone typing that as part of a real URL?

Can you imagine writing facebook.com/public instead of facebook.com in the browser?

So our goal is for your domain to load automatically /public folder somehow. And it depends on the web server you are using.

Most likely you are using Apache Or Nginx.
Even if you use a set of web servers like XAMPP/MAMP or a virtual server like Laravel propertyunderneath they always have one or the other – Apache or Nginx.

You must therefore configure domains (or they are sometimes called Sites) on these web servers.

It will also help you work on multiple projects. So you wouldn’t just have multiple folders and load like localhost/project1 And localhost/project2but will have project1.test And project2.test as appropriate areas working locally.

Every time you load a domain like project1.testit must be configured in Apache or Nginx configuration files.

The locations and names of these files vary depending on your operating system.

For Apache, the file name can be in compatible with sites folder, for example /etc/apache2/sites-enabled/000-default.conf or, for MAMP, it would be /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf where you need this:


<VirtualHost *:80>
    ServerAdmin povilas@laraveldaily.com
    DocumentRoot "/Applications/MAMP/htdocs/project1/public"
    ServerName project1.test
</VirtualHost>

Look at this /public part at the end of Document root? This is exactly how to point your domain to this public folder.

Notice: After editing this configuration file, don’t forget to restart the Apache server.


For Nginx server, the configuration file will be located somewhere like /etc/nginx/sites-enabled/project1.testwith this code:


server {
    listen 80;
    listen [::]:80;

    . . .

    root /var/www/html/project1/public;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name example.com www.example.com;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    . . .
}

In this case, we must look root variable and put /public at the end. Also restart the server.


Don’t forget the .env file

Another thing I see a lot is people leaving .env file with default values ​​after installation, except for changing database credentials.

What you need to configure is APP_URL variable.

In your case, this should be your domain name with the full prefix http:// Or https://.


APP_URL=

I wrote an article about this with a little more detail – Why it is important to change APP_URL in Laravel .env file

Other articles that may be helpful:

Finally, if this seems too complicated, there are tools to help configure servers faster. I’m a big fan of Laravel Forge – here’s a video of how we use it for our projects.

That’s it, that’s all.
I hope now you understand why /public is needed and how to configure it.



Technology

Another Tech Information

Similar Posts