user3095721
user3095721

Reputation: 51

How do I automatically run Node and Redis servers on laravel app start up

I have never used Node or Redis before, however in the app that I am building I am using node and Redis to run socket.io.js for some live updating features.

Currently in order for me to make this work, I have to open terminal and run

$ cd project/nodejs/Redis/lib/src
$ ./redis-server

Then I am not able to use that terminal window anymore. So I open a new terminal window and run

$ cd project/nodejs
$ node server.js

Once again I am also not able to use this terminal window any more.

Then I open another window and run

$ cd project 
$ php artisan serve

How can I combine all of this and run it when some one navigates to the app URL?

Do I need to build a commands in the commands directory? I've done some research, but I am still confused. I had never used terminal until I started using laravel, so I really have no idea what I need to research.

Upvotes: 3

Views: 3356

Answers (2)

Antonio Carlos Ribeiro
Antonio Carlos Ribeiro

Reputation: 87779

You can create one script file (myserver.sh), with all your commands in sequence:

cd /your/www/folder/project/nodejs/Redis/lib/src
./redis-server &

cd /your/www/folder/project/nodejs
node server.js &

cd /your/www/folder/project 
php artisan serve &

Note that I added an & at the end of each command, this will make that command run in background.

Then you run it by running:

bash myserver.sh

Or you can make it executable

chmod +x myserver.sh

And then just run

./myserver.sh

This will work for development, but it's not a really good option for production. You should use a real web server, like Apache 2 or NGINX and not PHP builtin server. But this is just my opinion.

Don't think about creating Laravel commands to execute Node nor your Redis server, it's not what it serves for, take a look at their own wiki and see how people run those applications in production servers.

Upvotes: 2

user3095721
user3095721

Reputation: 51

Oh NVM. I added the full path to both CD in myserver.sh cd /Applications/MAMP/htdocs/work/nodejs/Redis/src ./redis-server &

cd /Applications/MAMP/htdocs/work/nodejs node server.js &

Upvotes: 1

Related Questions