How to Deploy Laravel Applications on Ubuntu with Apache
Using Laravel for some time I had my fair share of deployments and everything that comes with it. I feel like every time when I do it there is something new that comes up that I’ve never have before, so let’s see the main steps in the deployment.
The project
Probably you already have you project setup, if don’t there a few things that can get you started:
composer create-project laravel/laravel example-app and there you have it a new fresh Laravel application ready and on your disposal.
If you are using a project that was cloned you need to be sure to run a few commands before continuing to the next steps:
php artisan migratecomposer installnpm install— optional if the project is using frond-end framework like Vue JS
The setup
After the project is working, it’s time to do some moves. The project folder needs to be moved in order to be able to use it with apache web server.
Starting with moving the project folder:
sudo mv example /var/www/html/After that, set the necessary permissions to ensure the project runs smoothly:
sudo chgrp -R www-data /var/www/html/example/sudo chmod -R 775 /var/www/html/example/storagein most of the cases I’m having issues with this step: the permissions even though the project may run smoothly some issues can occur later in your developing process, always be sure to check if the permissions are set correctly.
Next, you need configuration file for the apache2 if you don’t have one it can be easily created:
First navigate to the apache2 folder:
cd /etc/apache2/sites-availableand then
sudo nano example_project.confthere you go the file is created, now the more important and trickiest part — the setup
<VirtualHost *:80>
ServerName thedomain.com
ServerAdmin webmaster@thedomain.com
DocumentRoot /var/www/html/example/public
<Directory /var/www/html/example>
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>this is pretty much the simplest configuration file, but you need to be very careful for a few things, things that I’ve had problems with many times before…
ServerName— should be changed to your server IPDocumentRoot— very important this should be changed to the path to your project’s public folder, otherwise it won’t work(Trust me…I’ve been there…)AllowOveride— something also important, should be set toallso the operating system can work with the socket you can get more in-depth about it on the apache documentationErrorLog&CustomLog— optionally can be set to your Laravel project storage folder, me personally before so, otherwise the logs will be stored in theapachelog files. When opting for the Laravel option, you should keep in mind the files needs to exist before adding the path to them.
After those steps are done, you need to enable your config file so the apache server can use it, by doing the following:
Disabling the default configuration:
sudo a2dissite 000-default.confEnabling your config file
sudo a2ensite example_projectafterwards you need to activate the rewrite mode to the apache server using:
sudo a2enmod rewriteand of course to restart the apache server in order for everything to take effect:
sudo systemctl restart apache2if no errors occur while the command runs it should be ready to go, there a few ways to access your site:
Type your server IP in the browser —
127.0.0.1for localUsing an URL of your choosing
in order to do that, you need to edit some files:
Edit your configuration file and change
ServerNameto be the URL you wish to see in the browser —yourdomain.testGo to the
etc\hostsfile and change it like:
...
127.0.0.1 yourdomain.test
...this way the server will be able to find your website through it, personally prefer that way(I’m Linux guy, after all).
Alternatives
Laravel Herd — amazing tool, looks, works great, if don’t want to all this and you are on MacOS it’s a no-brainer !
XAMPP — great tool, very helpful, works on Windows & Linux, easy to install, for me if you are on Windows(for whatever reason…I’m not judging…) this is the tool for you.
Lerd - the Herd alternative for Linux, has amazing features, everything in one place it's worth it.
Conclusion
It’s pretty easy process, but there a few places were should be careful not to mess up, other than that, I encourage everyone to try, it’s free, it’s fun, you are gonna love it!
Share this post