Skip to main content

Laravel, Codio, and Nginx

I have recently come across a *sweet* online IDE, this would be Codio. Codio lets you develop in the cloud and run the code on an actual server. This way you can write your code and run it in the cloud, keeping your development environment universal and in my case safe-guarding against my own nonsense. While Codio does offer their own documentation on getting Laravel going, it uses Apache and MySQL. So here is getting going with Nginx, MariaDB, and PHP-FPM on Codio for Laravel development.

Create your project

Create a project just how you normally would, except select "Git" as the method for getting the base code into the workspace. This will give you an input where you can either put in an HTTP or SSH based git repository URL. If you are using SSH and you haven't setup the SSH key on your repository, it will prompt you to do so automatically with a key Codio generates. For now, I'm going to just use the standard HTTP URL for Laravel which is https://github.com/laravel/laravel.git .

Configuring the IDE server

Once your project is opened, you should be seeing the readme.md file from the repository opened in the editor along with the listing of files in the left pane. So, if you are seeing that, open up the Terminal ( Tools > Terminal ) to get the server setup.

The first thing we need to do, is install our web server and while we are at it let's grab mariadb, php, and composer. We manage software in Codio using a tool called "parts".
parts install nginx mariadb php5 php5-fpm php5-curl php5-pdo-mysql composer 
Once this installation completes you will see some sectioned output on how to use the different installed services. One thing you may note, is mysql itself got pulled in even though we are going to use mariadb. This is because of a dependency within php5-pdo-mysql. This isn't a major issue, just remember to not start the service using parts and all will be fine.

Now that everything is installed, you will see the php5-fpm package gave you some nginx configuration to add. So, let's go modify the configuration for our server now while our mind is on it. Copy the configuration they give, then open the default configuration with vim like so:
vim ~/.parts/etc/nginx/conf.d/default.conf

Within this configuration, navigate under the location / {} block. Then go into insert mode, and just paste like normal. While we are here though, there are some other things that need to be edited. First, the root declaration within the first location block, move that outside into server {} and while you are doing that, append /public to the end. Now, within the location / block, add

try_files $uri $uri/ /index.php$is_args$args;
index index.php;
so that things will be send into laravel. Finally, just write the file and quit.

Running the server

Now that the server is configured, let's install the projects requirements and startup the server to test things. Since we installed composer, all we need to do is run composer install from ~/workspace. Once that is done, startup mariadb, php5-fpm, and then nginx.
parts start mariadb php5-fpm nginx

Now we just need to have our preview open in a new browser window. To get this, click the down arrow beside the last menu item (Project Index (static)). Select "New Browser Tab" and then select "Box URL" (or the SSL version if you wish.) It will then instantly open a new tab, and you should be greeted with the all too familiar start page for Laravel.

Startup

Now that things are running, let's follow the path of the original guide from Codio and setup a startup script. This way when you open the project back up, the server will automatically startup as well. So, go into the root directory (~) for the instance (the one just above workspace). Then
touch startup.sh
and go into vim to edit that. Add a line to stop the processes, then one to start them again like so:

parts stop mariadb php5-fpm nginx
parts start mariadb php5-fpm nginx

Enjoy

Now enjoy your cloud-based IDE and don't worry about remembering to synchronize your settings across systems again.

Comments

Popular posts from this blog

My PHP Wishlist

PHP Wishlist These are things that I think would help developers build better programs with far fewer lines of code. Simple enough? Let's get to it.
Scalar Type Hints and a Strict Mode Currently one of the hottest RFCs in PHP history perhaps, Scalar Type Hints. This is a feature that allows developers to specify any type a given parameter should be. Currently PHP only supports object types and arrays for type hinting. This allows for string, integer, boolean, and the like. 
However, nothing is simple in PHP. With this, some developers want conversion to happen if the wrong type is given (string to integer for example if '3' is passed in) while others want strict enforcing of types. The RFC has a middle-ground of loose typing by default like everything else in PHP, but the option to turn on a strict mode.
My issue with the RFC is... The syntax is horrible for declaring strict mode. I was actually entirely for it up until a few days ago. I read a comment on Reddit. This per…

Running supervisord on Codio

Codio currently does not provide a parts install for supervisord. This can make developing apps that uses Laravel queues an issue since you'd then have to keep a terminal open at all times. So let's keep things simple, and install supervisord manually.

Installing the tools The only thing from parts that we will need, is pip. This is a python package management tool, which is what supervisord is written in. Just grab this to your box like so: parts install pip Now that we have pip, we can pull in supervisord through it. pip install supervisord Configuring environment Now that supervisord is installed, we need to setup the boxes configuration to make it easier to use. We need to:

Add the python bin to the path.Make the configuration directory for supervisorMake a log directory To add the python bin folder to your path, open up /home/codio/.bash_profile and add the following: $home/.parts/packages/python2/2.7.6-2/bin to the first path entry. Remember to separate it with a colon betwe…