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

Form macros in Laravel

Form macros are a really cool part of Laravel. They are described in the documentation , but that doesn't show you exactly how to use them in your application. So here is how I do it. Load macros on the start of the application To register the macros if we follow the docs we simply need to do something like Form::macro('myField', function() { return '<input type="awesome" >'; }); But how do we get this to load when needed? My solutions is to simply have it load on the start of the application. I do this by making a folder in my app directory called "misc" which just holds files for different things that I have to startup this way. Inside of that folder I make a file called "form_macros.php". To load this file on startup, we are going to require it within app/start/global.php. Simply add require app_path() . 'misc/form_macros.php'; to the end of the global.php file. This will make sure that the form macros are