Deploy Laravel , Laravel Echo , Queue On Heroku (P2)

Tram Ho

Preface!

Recently, my project needs to deploy to heroku to test as well as make use of many of heroku’s benefits. Actually, this is a quite big challenge for me because before I only used heroku for small and simple projects, now deploying a project running with many typical laravel services such as Queue, Laravel Echo , Schedule …. v … v …. And today I would like to talk about my own experience.


Content

Ok, before starting the content of this article if you have not read my previous article, please come here to read offline. And if you’ve read it, let’s continue to solve the problem.


Problem :

The problems solved in the previous article:

1. Set up a normal web server to run the web (Use nginx)

2. Setup to run the queue (Send mail, run notification)

3. Setup schedule to run daily jobs

The problems solved in this article:

4. Setup server laravel echo to run socket (For realtime notification … v … v … v ….)

5. Setup custom domain for the site

6. Setup https for the site

7. Extend limit upload file

Ok, let us continue with each part of the problem!


To handle :

4. Setup server laravel echo to run socket (For realtime notification … v … v … v ….)

Like other environments, users need to connect to the socket via a link. And our job is to use that link to connect to the laravel-echo server. Here we will use nginx to proxy from url to laravel-echo We need to go to the file nginx_app.conf and fix that file as follows:

With the above setup, the url domain.com/socket.io/ will be proxyed into the server’s 3000 port. The next is to run the server echo through port 3000. The start server is also simple, surely you are no stranger to running server echo by creating a file laravel-echo-server.json and then running it with the command :

Note: Remember to set up port 3000 to run. because above I have configured nginx as port 3000 already

Using the laravel-echo-server.json file is ok, but for servers like heroku it’s quite inconvenient. Why, because every time a server is deployed, heroku will deploy to a completely new node and has nothing to do with the old code and if so, you need to fix the file laravel-echo-server.json or push the code into the file. then deploy and just deploy. With method 1, you will be extremely tired every time deploy and method 2 will lead to laravel-echo server security issues that need sensitive information like redis .

For me, I suggest you do the following. Create an echo-server.js file. With the following content :

With the above method, all your information will be read by the above file in the .env file. You only need to have it setup on the env and every time the deploy runs

The server is ready to work. Deploying work also becomes much easier. ???

The following is the one that has made me extremely time-consuming to think. That I can not do how to laravel and laravel -echo connect to each other. From the beginning, running the socket ( laravel-echo ) the server can use the supervisor to run the server, but the supervisor and nginx are on 2 different dynos. And I almost found all ways to solve this connection and as a result, I did not find but found another way.

I chose to run laravel-echo and nginx on the same dynos. Nginx will run in public and laravel-echo will run in the laravel-echo . To do this, we need to go to Procfile and edit the following:

Ok, done Mission complete.

5. Setup custom domain for the site

Actually with the setup of this custom domain, I probably do not need to say anything lengthy. You can follow these steps:

  • You can buy and manage domains via heroku’s add-on point-DNS
  • Use that add-on to create subdomains appropriate for your site
  • You need to go to your app settings.
  • Fill out the subdomain information in the Domains section
  • Also, don’t forget to remember to set up the route for your subdomain.

A point of interest in using subdomains from one page is that you call the api of another page. This will result in cors error. This is a mechanism that webrowsers need to follow to avoid leaking information. You need to fix it = nginx or your own project.

For me, I chose to use lirary barryvdh/laravel-cors via composer. To config or understand more about the library. You can come here to watch it.

Good luck !

6. Setup https for the site

HTTPS of course. Basically, heroku has support for providing SSL certificates for your custom domain (Support with paid apps.) Or manually add your SSL certificates when you don’t want to use heroku’s SSL certificates.

In this article, I will show you how to use SSL certificates by heroku. You need to follow these steps:

  • Go to settings app and scroll to SSL certificates
  • Click the Configure SSL button
  • Then a dialog box opens and you select Automatically .... and press Continue
  • Now heroku auto generate SSL certificates for each domain you use. It may take a few minutes to update. Please wait a bit
  • After a few minutes or less, go back to the settings app and scroll down to the Domains tab. Now, the DNS generated for each custom domain corresponding.
  • You go to the add-on point-DNS into each of your custom domails and fill the DNS the app settings field into the Hostname field.
  • By this time, you can use https for your server (remember to setup https in laravel)
  • However, there is still a problem that you can still access the page via http . To fix this I went back to using nginx automatically switches over to https when the request is http . You just need to add this code to the beginning of nginx_app.conf file is ok.

7. Extend limit upload file

This is a fairly common request of customers. (By default, php and nginx only allow max upload of 5MB to the server, I will fix it to upload the 25MB file). To fix this problem. You need to do the following:

  • Increasing the limit of nginx you need to add this code to the beginning of the file nginx_app.conf is ok.

  • Increasing the file handling limit for php, you need to create the file .user.ini in .user.ini public directory. The specific file contents are:

Ok, I will end the post today! This will be my last post about heroku ! Thank you for reading!


References

https://devcenter.heroku.com/articles/custom-php-settings#default-behavior

https://devcenter.heroku.com/articles/custom-php-settings

https://dannyism.com/running-supervisor-with-laravel-workers-on-heroku/

https://devcenter.heroku.com/articles/custom-domains

https://elements.heroku.com/buttons/gregburek/heroku-tls-auth-nginx-sample

Share the news now

Source : Viblo