In this tutorial we look on how to upload a laravel project to live server as simple as i know how.
There are a bunch of tutorials out there that shows how to go about it but if not properly done, it can prove to be complicated or even expose the site to risks eg giving access to private files.It is important to seperate public folder files from the other project files.
This tutorial assumes you are using CPanel in Live server.
We will follow the following steps:
NB: Note the Database name, Database Username and Password.
Go back to your project in your local machine.
4. open .env ,and edit these configurations
APP_NAME=Name-of-your-project
APP_ENV=production
APP_KEY=base64:pEKzNl2ZpgIdcikNI8XuywhdLvn3F2eyZZgcW5A7j38=
APP_DEBUG=false
APP_TIMEZONE=UTC
APP_URL=your-site-domain
For APP_DEBUG you can leave as true if you need to debug your on live serve.Make sure to set it to false after debuging.
APP_KEY will already been generate when creating the project through composer but if there is no key simply run
php artisan key:generate
5. Edit the database configurations also. Use the details we created in the live server
This applies to mysqli. but if using sqlite just set and upload the sqlite database
DB_CONNECTION=mysql
DB_HOST=your-site
DB_PORT=3306
DB_DATABASE=database-name
DB_USERNAME=database-username
DB_PASSWORD=database-password
6. Edit Email configurations
For this tutorial we will be using third party email server.This configs are for google mail server, gmail.
Please create an App pasword in your google account.This enables us to use gmail outside google apps.
Checkout google mail server to learn how to create App password
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your-email
MAIL_PASSWORD=app-password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS="your-email"
MAIL_FROM_NAME="${APP_NAME}"
7. Then run following code in terminal to clear config cache
php artisan config:cache
8. Run the code to rebuild all classes
composer dump-autoload
9. Zip the project folder.
10. Upload the zipped folder to live server
NB: We have seperated the public folder files from the rest of the files for security purposes.
if (file_exists($maintenance = __DIR__.'/../your-project-folder-name/storage/framework/maintenance.php')) {
require $maintenance;
}
require __DIR__.'/../your-project-folder-name/vendor/autoload.php';
$app = require_once __DIR__.'/../your-project-folder-name/bootstrap/app.php';
11. Next we upload the database.
Go to PhpMyAdmin in the local machine and open the database.Go to the Menu -> export and export.Note the directly where the database dump file has been saved.
Back to live server, go to Cpanel Tools then PhpMyAdmin and click the database we created earlier the on menu click import the Browse and Locate the database file we exported then open.
click import.
12. Browse your site to test.
If you are using later versions of laravel probably you are using php 8.0 and above.If so you will get an php version error.
NB:the error will be visible if you left APP_DEBUG true
To solve php version error,
Notice the PHP version changes.
"php": "8.2.0", as below"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true,
"php": "8.2.0",
"allow-plugins": {
"pestphp/pest-plugin": true,
"php-http/discovery": true
}
},
Enjoy quick fix with our code snippets and complete projects templates.
codestarch@gmail.com