First step is to merge all the code in the main branch.Because that's the version of choice we would like to send to production.There are lots of ways to deploy an app, you can use filezilla,github actions or other ci/cd tools,docker,etc.
options on how to deploy an app
- Set up ssh access of your github account on your vps
- Github Actions
- Docker based deployment
option 1 is for small projects and there aren't too many things you need to take care of so you can manage this for small projects.Since my project is small I have tried the manual process and since this is my first time deploying an app i tried doing it with a small project and with the simplest of all process.
During the development process I created a DEV branch and started pushing my code there.After I compleated my development process i had written scenerio based test scripts and manually ran them locally and after all the test cases have passed merged all the code with the main branch.
Now set up ssh access of your github account on you vps and clone the repo in the home directory, and make sure to stay on the MAIN branch btw. And in this way we can have our program files on our vps.
if you are successfull doing upto this point then 🥳. But we have just started out and there is lot of work to do 😭.
First things first is to setup up LAMP stack. I will suggest to install the version of php that you have used on your local system to develop.
Repeat all the process that you would use to set up a laravel project on your local machine and make sure to use the DB name,username,password that you have used while setting up your Database.
Now set up the .env.
-
I have kept SESSION_DRIVER,CACHE_STORE in a file. I don't like keeping these in a DB table. If you want to store these in a DB table that's totally upto you.
-
In the APP_URL inside the .env remove the local host and use your domain name there.
-
Set APP_ENV = 'production' & APP_DEBUG = 'false'
now after completing all the above steps, move your project dir from home to /var/www/
now its time to setup Apache config
sudo nano /etc/apache2/sites-available<name-of-your-config-file>.conf
our config file should look like this
<VirtualHost *:80>
ServerName <Domain name>
ServerAlias www.<Domain-name>
DocumentRoot /var/www/<your-project-dir>/public
<Directory /var/www/my-<your-project-dir>/public>
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =<Domain name> [OR]
RewriteCond %{SERVER_NAME} =www.<Domain name>
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
Now we need certbot
Certbot is a free, open-source software tool developed by the Electronic Frontier Foundation (EFF) that automates the process of obtaining and renewing Let's Encrypt SSL/TLS certificates
purpose - without this in the browser if we open our app it will say connection unsecure
Installation
sudo apt install certbot python3-certbot-apache -y
sudo certbot --apache -d <Domain name> -d www.<Domain name>
at last we need to run these commands
sudo a2ensite my-blogs.conf
sudo a2enmod rewrite
sudo systemctl reload apache2
run this command for production env
composer install --no-dev --optimize-autoloader
this commands basically removes packages that are needed only in dev env
now in the dashbaord of your vps provider we need to setup dns with our domain
This will complete the deployment process 🎉🎉
and lastly run this command php artisan optimize:clear
The php artisan optimize:clear command is a convenient utility in Laravel that clears a variety of cached application files in a single operation, which is helpful during development and crucial during deployment
What it does The optimize:clear command runs the following individual Artisan commands in sequence:
-
config:clear: Removes the cached configuration file, ensuring that changes in your .env file or configuration files are reflected.
-
cache:clear: Flushes the application's default cache driver (e.g., file, Redis, database).
-
clear-compiled: Removes the compiled services and packages files that are used for performance optimization.
-
event:clear: Clears the cached events and listeners.
-
route:clear: Removes the cached route file, ensuring that any route changes are recognized by the application.
-
view:clear: Deletes the compiled Blade views, forcing them to be recompiled on the next request.
then reload apache with this sudo systemctl reload apache2