How to Deploy Lumen on a Vps

I built a super simple app last week, it gets a ping when sales have been made on my Gumroad product and send it to a Slack channel. I know I could have done it with Zapier of IFTT but they’re either super limited or super slow.

I wanted a minimalistic framework to do it (it’s 1 route and 1 API call). I also wanted a PHP based framework, it’s way easier to host on my VPS.

I chose Lumen, it’s a “mini-Laravel”. I didn’t know it, it looked pretty well done and easy to use. So I gave it a try.

The problem is, the documentation is also super minimalistic… I spend some time browsing the web looking for a tutorial on how to deploy Lumen in production on a VPS. I didn’t find any, so here is how I did it:

  • Git clone or zip your project and put it in your web folder, usually /var/www/html/
  • Double-check the permissions, it should be readable by www-data
  • Install composer, and run composer install

I use nginx as a webserver, to install a new app copy the default config file:  cp /etc/nginx/sites-available/default /etc/nginx/sites-available/mynewapp.conf

And remplace the content by: 

server {
	listen 80;
	root /var/www/html/mynewapp/public;

	index index.html index.htm index.php;

	server_name mynewapp.com;

	location / {
	   try_files $uri $uri/ /index.php?$args;
       }

	# pass PHP scripts to FastCGI server
	location ~ \.php$ {
		include snippets/fastcgi-php.conf;

		# With php-fpm (or other unix sockets):
		fastcgi_pass unix:/run/php/php7.3-fpm.sock;
	}

	# If you want to cache the static files
	location ~* .(js|css|png|jpg|jpeg|gif|ico)$ {
		expires max;
		log_not_found off;
	}
	#
}

Then link your file to the site-enabled directory: ln -s /etc/nginx/sites-available/mynewapp /etc/nginx/sites-enabled/

Check the nginx configuration: nginx -t, and restart it service nginx restart. You should be good!

It was quite easy after all, but it’s better with a tutorial, isn’t it?


PHPTuto

302 Words

2020-11-11 10:30 -0400