Deploying Laravel 5 Projects with Rocketeer to Ajenti powered VPS

Deploying Laravel applications is not that hard, however, it's hard to find documentations on how to do it with Rocketeer. In this article I'll cover on how to install Ajenti to freshly installed Ubuntu VPS and deploy Laravel projects.

Firstly, if you haven't read this article of Bryan Kennedy's about the security of Linux servers, start with it, I'll assume that, you have read and applied the instructions mentioned within that article. Thus, I also assume that you now how to use git and you have your project stored within a git repository that can be accessed.

After logging in to your VPS, with the user that you have created previously, not the root,
wget http://repo.ajenti.org/debian/key -O- | sudo apt-key add -
echo "deb http://repo.ajenti.org/ng/debian main main ubuntu" | sudo tee -a /etc/apt/sources.list
sudo apt-get update && sudo apt-get install ajenti
sudo service ajenti restart
sudo apt-get install ajenti-v ajenti-v-nginx ajenti-v-mysql ajenti-v-php-fpm ajenti-v-mail ajenti-v-nodejs php5-mysql php5-cli php5-mcrypt php5-curl
sudo service php5-fpm restart
sudo service nginx restart
sudo service ajenti restart
Within the installation, you will be prompted by some questions, you can skip them with Enter, however, you should better set your MySQL root password something strong. After the installation is finished, you should be able to access to Ajenti on https://YOUR_VPS_IP:8000 with the username root and password as admin. If you can't access to Ajenti, do the following below:
sudo apt-get install python-dev
sudo apt-get install python-pip
sudo pip install gevent --upgrade
Still if there is no luck for you, check if anything is occupying the port 8000.
sudo netstat -tlnp | grep 8000
Afterwards, it would be better for you to change your the user root's password, admin, on Ajenti that can be accessed through the users menu. Finally, from the websites section of the menu, add your project and within that project.
ajenti-websites

Then add your domain and create the initial path which you can set it like /srv/yourproject.com. However, we will be changing it in the end of this article.
ajenti-website

Don't forget to add your domain. (By the way, if you haven't created your DNS records, do it from your VPS provider.)
ajenti-domains

On the content section, that can be seen below, select PHP FastCGI and hit create, or your php files won't be run. On the advanced part of this section, add the following lines.
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
Thus from the PHP section, add the configuration below, you can set the memory_limit as you wish, but the default one won't be sufficient probably.
open_basedir = none;
cgi.fix_pathinfo = 0;
memory_limit = 512M;
ajenti-content

Thus, within the Advanced menu section, next to content, add the part below to Custom configuration:
location ~ /\. {
deny all;
}
    
location ~ ^/(robots.txt|favicon.ico)(/|$) {
try_files $uri =404;
}

location / {
try_files $uri $uri/ /index.php?$query_string;
}
Finally, from MySQL section, create your database, add the user and grant all permissions. If you haven't set the root MySQL password within the Ajenti's MySQL plugin, can be accessed within the menu on the left, set it first, then restart the Ajenti service and retry this step.

Then again within your VPS, install git as we will need a git repository to fetch the project files and composer.
sudu apt-get install git
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
If you want to compile css and javascript files with gulp and bower within your server, don't forget to install them too.
curl -sL https://deb.nodesource.com/setup | sudo bash -
sudo apt-get install -y nodejs
npm install --global gulp bower
Then within your local computer, not within the VPN, install the rocketeer.
wget http://rocketeer.autopergamene.eu/versions/rocketeer.phar
chmod +x rocketeer.phar
mv rocketeer.phar /usr/local/bin/rocketeer
Finally, hit rocketeer ignite within your local laravel directory. You will be prompted to give some answers to some details about your project and VPS. If you have problems on answering them, you can see an example from one of my projects. For the Ajenti example, lets go one by one for the each files. Within the config.php file, set your application name as the one that you have created previously as the Ajenti website project name which is yourproject.com within our example. Then within the connections array, set the information with changing the uppercased strings. If you haven't read about the article that I've mentioned within the start of this article, deploy is the username that is created within that article and as we are not using passwords, we are using our SSH key.
'application_name' => 'yourproject.com',

'connections'      => [
    'production' => [
        'host'      => 'VPS_IP_ADDRESS',
        'username'  => 'deploy',
        'password'  => '',
        'key'       => '/home/USERNAME/.ssh/id_rsa',
        'keyphrase' => '',
        'agent'     => true,
        'db_role'   => true,
    ],
],
The other file is hooks.php where you manage the tasks that will be run. You can configure it as you wish, below is a just example on how you can configure it. If you copy paste the one below, after the deployment is done, it will migrate if there are any available migrations and will clear the cache and expired password resets.
'after'  => [
    'setup'   => [],
    'deploy'  => [ 'migrate'],
    'cleanup' => ['php artisan auth:clear-resets', 'php artisan cache:clear'],
],
remote.php file is the one that you make the VPS configurations. For our example, root directory will be /srv and you can leave the app_directory field blank as we have set the application_name field within the config.php file. There is also another array of shared folders which is used for sharing the data between the previous releases. Assume that you have uploads folder within your public folder where users upload their photos or something like that. If you deploy a new release, they may be lost or lose the reference through their paths. To prevent that, you can sync those folders with indicating them within the shared array.
'root_directory' => '/srv',

'shared'         => [
    'storage/framework',
    'storage/logs',
    'public/uploads'
],
scm.php is the file that you indicate your git repository that I think which is clear. I'll skip the stages.php file, however I wanna also mention about the strategies.php file. If you want to skip the gulp and bower instructions and just run the composer, you can change the dependicies variable.
//'dependencies' => 'Polyglot',
'dependencies' => 'Composer',
After setting all those, you can deploy to your VPS with hitting rocketeer deploy. If you have followed the instructions one by one, you will see that, on the /src/yourproject.com, you will have your deployments. Then within the Ajenti, websites' domain section, you can set the website path, that we previously set as /srv/yourproject.com, to /srv/yourproject.com/current/public so that whatever release you deploy, always the current will run.

One possible issue that I've seen is, if you can access to the root page of the project, but not the others, so that you get again 500 internal server error, you need to fix file permissions. Either use the fix file permissions button on your project from the general section on Ajenti, or from the terminal, run the command below.
sudo chown -R :www-data /srv/yourproject.com/