Update Laravel on your VPS to use the Resource API

Update Laravel on your VPS to use the Resource API

Laravel is rapidly updating. If you haven't updated your stack on your VPS, then you might not be able to use the powerful Resource API which became available in Laravel 5.5

The Laravel Resource API is Awesome!

When building an API, you might want a layer between your Eloquent models and the JSON that is returned to your application requests. Laravel introduced resource classes that allow the backend developer to easily transform collections into JSON. This addition to the Laravel framework is great if you plan on taking advantage of Vue!

So after you've built your API resources, come back here to figure out how you can upgrade the Laravel site that's sitting on your virtual private server. This guide is going to be using the LEMP stack (Linux, Engine-X or Nginx, MySQL, and PHP). If you need to update for Apache, you can follow most of this guide but will need to make the needful changes for your environment.

Setting Up Production Before Git Push

If we're going to leverage all of the power of Laravel 5.5, then we need to update PHP to version 7.1 or else a myriad of packages will not allow composer to install. 

Now is probably a good time to mention that I learned this all while I was doing it, so if you know a better way -- please leave it in the comments so I can learn! Okay first thing first. Run php -v -- if you have PHP version 7.1 then please continue to the next header. 

Update your server packages sudo apt-get update && sudo apt-get upgrade

Reboot your server through either the GUI on your hosting panel, or by running sudo systemctl reload nginx

Now we need to locate all of the PHP packages on the server and remove them. Grep through your PHP packages and remove them.

sudo apt-get install php7.1 php7.1-cli php7.1-common libapache2-mod-php7.1 php7.1-mysql php7.1-fpm php7.1-curl php7.1-gd php7.1-bz2 php7.1-mcrypt php7.1-json php7.1-tidy php7.1-mbstring php-redis php-memcached

 

You'll want to install PHP 7.1, php7.1-XML, and php7.1-mbstring at the very least. Composer will not allow you to update or install on the server otherwise because they are dependencies of PHPUnit. If you're using images on your site, you will need the php7.1-gd package as well. Then you'll need to reconfigure your NginX permissions. 

sudo chown -R :www-data /var/www/laravel

sudo chmod -R 775 /var/www/laravel/storage

You'll need to change the PHP package that is declared on your NginX sites-available directory from the previous package to the current package.

sudo nano /etc/nginx/sites-available/default

CHANGE: fastcgi_pass unix:/run/php/php7.1-fpm.sock;

Reload NginX or restart your server so that the changes can pass through the system. Without a proper setup, your site will likely go down at this point. No worries, once it comes back up you'll be back in business!

Updating Composer for the Latest Laravel

You should update your Laravel app locally before you push to your server, then all you'll need to do is run composer install. Composer will squawk at you if you're logged in as the root user, so you'll want to make sure that the packages you're installing are only the packages found in the composer.json file. 

If you're utilizing the resource API chances are you've created database tables to store the JSON objects into, at this point your should run php artisan migrate and make sure your tables have been moved over. 

That should be everything you need to get started! Leave me a comment if you need some helpful tips or just to say hello. Thanks for reading the Update Laravel on your VPS to use the Resource API Guide. 


Posted in: laravel

php backend

Leave a Comment