Once you have completed the implementation of your great project in Laravel, surely the next thing you will want to do is show it to everyone, that is, take it to production or do the deployment, to do this, the steps you have to follow do not differ much from any another PHP project, regardless of whether this project is a framework or not, but, in this chapter we will see some considerations that you can take into account, it is important to remember that there are many ways to do the same thing and the steps may vary depending on the options provided by the hosting you are using.
The server par excellence used to publish or serve a project in Laravel on hosting is Apache, Laravel can be served in several ways:
https://laravel.com/docs/master/deployment
But, if we review the current market, most hosting has at least the following features for the basic plan, which is the cheapest:
The first thing you should do in these cases is to verify the minimum versions, usually the most important is the PHP version, which for the current version of the framework is 8.2 or higher, if your hosting does not have the minimum version of PHP, you will not be able to use version 11 or higher, in these cases, you can use a lower version of the framework that is supported by the hosting and this is the first recommendation that you should take into account. Likewise, if you have doubts, you can ask the hosting you want to hire if it has support for Laravel.
One of the great advantages and disadvantages of Laravel is that the latest versions usually use modern versions of PHP, this is good since we can use all the modern features and fixes to security flaws, but it is also a disadvantage since many times it is not It is possible to use the latest versions of PHP or the hosting simply has not updated to these latest versions.
Being clear about the main aspect that we have to take into account when publishing our project, we are going to discuss another important point, Node.
As has been mentioned, one of the fundamental differences that we have in Laravel with respect to other frameworks is that we have direct integration with Node, we can easily add client-side technologies (or server-side if required) with Node, install React, Vue or Angular to develop our applications; for example, suppose that we also have an integration with Vue, as we teach in the book, before publishing, remember to also execute the command to generate the files from the client to production, which is usually:
$ npm run build
And automatically, you must test the development carried out in Node, which should work in the same way as when you were in the development environment:
$ npm run dev
If the application was developed correctly, in case you have an error, you must correct it and execute the command:
$ npm run build
Again.
You can consult the commands used to generate the builds from:
package.json
Although usually the previous two are enough.
We have already used the dev command, which is the one used at the time of development, for production you must use the build command; with this, the output files will be generated in the public folder.
The next point that we have to take into account is that we are going to take it to production, we must publish the entire project except the node_modules folder.
With special interest to the vendor folder, which is where the libraries and packages that make up the project are located, including the Laravel framework itself, which tends to be quite large, the Node node_modules folder is only used in the development phase.
As we mentioned previously, avoid using environment variables in the .env as much as possible. If it is essential for you, you can use it, but simplify it as much as possible.
At this point we are assuming that you have contracted hosting. In the hosting administration panel, there is a section to manage FTP connections, which will allow us to upload each of the folders and files of the project as listed previously, so you must create an account from your hosting's Cpanel.
And with this data, you can use an FTP client like FileZilla, in which, in the section:
Files - Site Manager
You can enter the credentials specified by your hosting and user; with this, it is possible to make a connection and upload all the files of the project, usually you must upload it in the public_html or www folder, you will surely have several folders on the hosting, but there must be one of them in which they point you with a file like the following:
/domains/<YourDomain>/DO_NOT_UPLOAD_HERE
/domains/<YourDomain>/public_html
Where you can upload the files and where you cannot, usually you must put all the content of the project in the root of this directory, and this is, so that, from the domain configured to the server, you have direct access without concatenating the folder or folders in the domain URL; for example:
/domains/<YourDomain>/public_html/laravelproject
YourDomain.com/laravelproject
There are some servers like iPage in which you can place a pointer to be able to place the root folder of the project in the domain, and in this way it is easier to organize several projects on the same server since you can point the domain not to the root of the server if not to a specific folder within it.
Here you can see how the project looks when uploading it to hostinger:
At this point, you must have all the project files and folders on the hosting. As a recommendation, keep the development environment active to see the project errors as we have it up to this point:
.env
APP_ENV=local
APP_DEBUG=true
Since, with this we can quickly verify when an error occurs. Also, remember that when you have the project in production, Laravel generates an error log in:
storage/logs/laravel.log
As an additional recommendation, try to keep all possible configurations in their corresponding files and do without the .env, which, although it is true that it can be used in both development and production environments, is more prone to errors or deleting or changing a key value by mistake or simply being deleted by mistake, this can have fatal consequences for your application and fail catastrophically; by managing the configurations in their corresponding files, they make the application more secure to avoid this type of situation.
From your hosting, you must create a database and run your project's database. Since it is a production application, you must decide which records you delete and which ones you keep for production; the process begins by exporting the database from your project. For this, you can use any client like the ones we use in this book, such as HeidiSQL or TablePlus, and export it in SQL format.
Then, with this database exported, the next step is to create an empty database on the hosting for the subsequent execution of the previously generated SQL. When creating the database, remember to place the connection data in the project in production:
config\database.php
'mysql' => [
'driver' => 'mysql',
'url' => env('DB_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', '<YourDB>'),
'username' => env('DB_USERNAME', '<YourUsername>'),
'password' => env('DB_PASSWORD', '<YourPassword>'),
Possibly, if you try to enter the application from the production domain, you will see a 403 error or similar, this is because the only public files are located within the public folder, which is at the root of the project, we need a mechanism to tell Apache that the framework's startup file, index.php, is located within the public folder and not in the root, to do this, we define the following content within the .htaccess:
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
With this configuration ready, the application should now work normally.
- Andrés Cruz
This material is part of my complete course and book; You can purchase them from the books and/or courses section, Curso y Libro Laravel 11 con Tailwind Vue 3, introducción a Jetstream Livewire e Inerta desde cero - 2024.
Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter
I agree to receive announcements of interest about this Blog.
!Courses from!
10$
On Udemy
There are 2d 19:59!
!Courses from!
4$
In Academy
View courses!Books from!
1$
See the books