DEV Community

Mehedi Mansur Rafi
Mehedi Mansur Rafi

Posted on

How to put a Laravel website online, step by step

Disclosure: some links in this post may be affiliate links. If you buy hosting
through one of them, I earn a commission and you pay the same price. I only
link to services I would use for my own projects.

A Laravel app that runs on your laptop is not finished. It is finished when a
stranger can open it in a browser. This is the full path I use when I deploy
Laravel projects for myself and for clients, written in the order I actually
do the work.

What you need before you start

  1. A domain name. Any registrar works.
  2. A server or hosting account that runs PHP 8.2 or newer and MySQL.
  3. Your Laravel project in a Git repository, or as a clean zip without vendor/ and node_modules/.

For a small Laravel site you have two sane choices. Shared hosting is cheap
and fine for low traffic, but you get no SSH on most plans, and that makes
Laravel painful. A managed cloud server costs a bit more and removes most of
that pain. This walkthrough uses Cloudways, because it gives you a real
server with SSH, Git deployment and SSL handled, without making you
administer the machine itself. Their plans are listed at
https://www.cloudways.com (plain link).

Step 1: Create the server and the application

In Cloudways, launch a server (DigitalOcean or Vultr, 1–2 GB RAM is enough
for a small app), then add an application and choose "Custom PHP App" or the
Laravel option if it is offered. Cloudways creates the database, the SFTP
user and the folder structure for you. Write down three things: the server
IP, the application public path (it ends in public_html), and the database
name, user and password from the application's Access Details page.

Step 2: Get your code onto the server

Two ways work well:

  • Git: add the server as a deployment target and pull your repository into the application folder. Best if you deploy often.
  • Upload: zip your project locally (without vendor/), upload it through SFTP or the file manager, extract it into the application folder, then run composer install over SSH.

After the code is there, run these once:

composer install --no-dev --optimize-autoloader
cp .env.example .env
php artisan key:generate
php artisan storage:link
Enter fullscreen mode Exit fullscreen mode

Step 3: Point the web root at the public folder

This is the step people skip, and then every route returns 404. Laravel's
entry point is public/index.php. The web server must serve the public
folder, not the project root. On Cloudways the application already serves
from public_html, so make sure your project root sits in public_html and
that public_html/public is not an extra nested level. On shared hosting,
create the subdomain or addon domain with its document root ending in
/public. If you get this wrong, no .htaccess trick will save you.

Step 4: Fill in .env for production

Open .env on the server and set the values that matter:

APP_ENV=production
APP_DEBUG=false
APP_URL=https://yourdomain.com
DB_DATABASE=your_database_name
DB_USERNAME=your_database_user
DB_PASSWORD=your_database_password
Enter fullscreen mode Exit fullscreen mode

APP_DEBUG=false is not decoration. With debug on, an error page shows your
file paths, your environment values and parts of your code to anyone who
triggers an error. Turn it off before the site is public, every time.

Then:

php artisan config:cache
php artisan route:cache
php artisan view:cache
Enter fullscreen mode Exit fullscreen mode

Step 5: Create the database tables

php artisan migrate --force
Enter fullscreen mode Exit fullscreen mode

--force is required in production; without it Artisan asks for confirmation
and your deployment script hangs. If your host gives you no SSH and no
Artisan, export the SQL from your local database and import it through
phpMyAdmin instead. It is slower and easier to get wrong, which is one more
reason to pick hosting with SSH.

Step 6: Fix file permissions

Uploads made through a file manager often land with permissions the web
server cannot read. The symptom is a white page or a 500 error right after a
"successful" upload. The fix:

  • Files: 644
  • Folders: 755
  • storage/ and bootstrap/cache/ must be writable by the web server.
chmod -R 755 storage bootstrap/cache
Enter fullscreen mode Exit fullscreen mode

Step 7: Connect the domain and turn on SSL

Point your domain's A record to the server IP. In Cloudways, add the domain
under Domain Management for the application, then issue the free Let's
Encrypt certificate from the SSL page. Wait for DNS to spread (a few minutes
to a few hours), then open the site with https:// and check that the padlock
shows and no page element still loads over plain http://.

Step 8: Add the scheduler and queues

Laravel's scheduler needs one cron line on the server:

* * * * * cd /path-to-your-app && php artisan schedule:run >> /dev/null 2>&1
Enter fullscreen mode Exit fullscreen mode

Cloudways lets you add this from the application's Cron Job Management page.
If your app sends mail or does slow work in queues, run a queue worker under
a supervisor (Cloudways has one built in) instead of letting requests wait:

php artisan queue:work --tries=3
Enter fullscreen mode Exit fullscreen mode

Step 9: Test like a visitor, not like the developer

Open the site in a private browser window. Register an account. Log out and
back in. Submit every form. Upload a file if your app takes uploads. Then
check on your phone. I test this way because the developer session hides
bugs: you are logged in as admin, your browser has cached assets, and your
.env mistakes only show for guests. Half the deployment bugs I have fixed
for clients were invisible from the developer's own chair.

The short checklist

  1. Code on the server, composer install --no-dev done.
  2. Web root points at public.
  3. .env set, APP_DEBUG=false, key generated.
  4. Database migrated.
  5. storage/ writable, permissions sane.
  6. Domain connected, SSL on.
  7. Scheduler cron added.
  8. Tested logged-out, on desktop and on a phone.

Do those eight and your Laravel site is online in a way that stays online.


I deploy and maintain Laravel websites for small businesses. If your site is
stuck on one of these steps, or you want someone to look after updates,
backups and fixes every month, you can hire me on
Upwork or
Fiverr.

Top comments (0)