Skip to content

abecms/platformsh-example-abecms

 
 

Repository files navigation

recipe-pm2

This Abe recipe demonstrates how to deploy your Abe project admin in production with pm2.

This recipe will be split into 4 parts:

  • Creation of your package.json for your project
  • Creation of your pm2 start script
  • Creation of your pm2 config file
  • Using a reverse proxy (Apache or Nginx)

package.json

If you have not created a package.json yet, do your homework :) (npm init)

Then you'll add the following dependencies:

{
  "dependencies": {
    "abecms": "2.14.5",
    "babel-preset-es2015": "6.6.0",
    "pm2": "2.2.0",
    "cli-color": "1.1.0"
  }
}

(Run npm i to install dependencies).

abecms

This will install Abe in your project. You could decide to use an Abe installed globally, but this is not a best practice: We strongly recommend to have a local Abe installation for each of your projects. This way, you'll be able to select a different version of Abe for each project. Note also that we recommend to fix the Abe version: Letting a * for the patches to be applied is ok, although in production, a fully fixed version (2.14.8) is better.

babel-preset-es2015

Abe is using Babel to transpile the code (we're ECMASCRIPT 2016). If you want to create scripts in your own repo which will extend Abe, add this mandatory dependency to be able to transpile your code.

pm2

We use PM2 in this recipe, so you'd better install it ! :)

cli-color

This is not mandatory. This dependency will colorize the Abe messages in your console.

npm start command

Once done, let's add a npm start script in package.json:

  "scripts": {
    "start": "./start.js"
  },

This will ease your deployment automation. To start Abe admin, you'll just have to enter : npm start This will launch the PM2 script (see below).

PM2 script

Add your pm2 start script by creating a start.js file in your project (you can name it as you want and put it in whatever directory you want. Just remember to adjust your package.json accordingly (by modifying the path to your "start" script.

Put the following code in your start.js file. This code will take in charge the startup of PM2:

'use strict';

var clc = require('cli-color');
var pm2 = require('pm2');

var dir = process.cwd();
if(process.env.ROOT) {
  dir = process.env.ROOT
}
dir = dir.replace(/\/$/, '')
console.log("run abe into", clc.green(dir), "folder");

var processFile = require(dir + '/pm2-config.json')
console.log(clc.green('[ pm2-config ]'), JSON.stringify(processFile))

var processName = processFile.name || 'abe'
pm2.connect((err) => {
  if (err instanceof Error) throw err
  var start = pm2.start

  pm2.list(function(err, process_list) {
    var found = false;

    Array.prototype.forEach.call(process_list, function(process) {
      if (process.name === processName) {
        found = true
      }
    })

    var cb = function() {
      if(typeof processFile.env === 'undefined' || processFile.env === null) {
        processFile.env = {}
      }
      processFile.env.ROOT = dir
      console.log(clc.green('[ pm2 ]'), 'start', 'pm2-config.json', JSON.stringify(processFile))
      pm2.start(
        processFile,
        function(err, proc) {
          if (err instanceof Error) throw err

          pm2.list((err, list) => {
             if (err instanceof Error) throw err
            Array.prototype.forEach.call(list, (item) => {
              console.log(clc.green('[ pm2 ]'), '{', '"pid":', item.pid + ',', '"process":', '"' + item.name + '"', '}')
            })
            process.exit(0);
          })
        })
    }

    if (!found) {
      cb()
    }else {
      console.log(clc.green('[ pm2 ]'), 'stop ', processName)
      pm2.delete(processName, function(err, proc) {
        if (err) throw new Error(err);
        console.log(clc.green('[ pm2 ]'), processName,  'server stopped')
        cb()
      });
    }
  });
})

Add pm2-config.json

This is the config file for pm2. See pm2 process file documentation for more details. As a summary, name will give a precise name to your pm2 instance of the project, script is the command to start Abe, and we're not in cluster mode (Not possible yet because of the stateful paradigm in Abe). Last but not least, you can choose the port on which Abe admin will listen (here it's 8012)

{
  "name": "pm2processname",
  "script": "./node_modules/abecms/dist/server/app.js",
  "args": "serve",
  "nodeArgs": ["--harmony","--max-old-space-size=2048"], # You can pass node arguments here
  "log_date_format" : "YYYY-MM-DD HH:mm Z", # Pm2 log format
  "exec_mode": "fork", # Pm2 exec mode (fork or cluster)
  "instances": "1", # if cluster mode number of instances -1 = server number of cpus -1 
  "env": {
    "PORT": "8012" # local port to run Abe
  }
}

Then run npm start

That's all ! You're ready to go and launch your production server. As a recommandation, we will show you how to configure a reverse proxy in the following chapters.

Apache as a reverse proxy

You can find how to configure Apache to proxify Abecms here

Nginx as a reverse proxy

You can find how to configure Nginx to proxify Abecms here

About

AbeCMS example for Platform.sh

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • CSS 75.3%
  • HTML 15.1%
  • JavaScript 9.6%