Start http, mysql, ftp and cronjobs at boot on EC2 running Linux? - linux

Is there a script that can be run at launch to start all these? If so, where do I put it?
If not, is there a way to do it via the management console?

This Just Works if you install these from packages. An init script will be placed in /etc/init.d and they will be started on boot.
for example installing apache with:
sudo apt-get install apache2
will result in the file /etc/init.d/apache2 being installed and the appropriate links set up to ensure the script gets called on system startup and shutdown.
You can edit the crontab with the command:
crontab -e
The cron daemon is automatically started at boot time and will run these commands according to the times you specify.

Related

Launch local node server at system boot

I'm working on a Raspberry Pi running nextjs (node server) connected with nginx. I tried using x-terminal-emulator ‘/usr/bin/bash -c /home/pi/Desktop/startup.sh’ but many of them didn’t recognize /usr/bin/bash as a directory and most of them didn’t budge to execute the script. In how many ways can I start my bash script? What is the most useful way possible?
These are the ways that I tried and the errors corresponding to them.
contrab -e: Didn’t budge to execute the command line
rc.local: DIdn’t budge to execute the command line
systemd: /usr/bin/bash file or directory doesn’t exist.
I’ve must of been using them incorrectly. If so what is the way to use them?
This recipe allows you to launch node server at system boot. In this recipe it is assumed that you have a node server running on port 3000.
Steps
Add node server to the rc.local file
sudo nano /etc/rc.local
Add the following line to the rc.local file
sudo node /home/pi/node_server.js
Make rc.local executable
sudo chmod +x /etc/rc.local
Reboot the system
sudo reboot
Verify that the server is running at boot
sudo netstat –lntp | grep 3000
This should output node server which is running on port 3000.
Verify that the server is accessible from remote computer
curl -i http://<ip_address>:3000

centos7 I set this script to boot, after reboot The service did not start up

Run the shell script with the boot already included
cd /source/finance-calendar
nohup npm run server:prod >/dev/null 2>&1 &
It is possible to start the service normally.
But I set this script to boot, after reboot
The service did not start up, what caused this?
I solve my question.
I add PATH=$PATH:$HOME/bin:/softwate/node-v10.14.2-linux-x64/bin to .bash_profile, so it can run the shell script when i finish starting my system.
But it can't start before user login.
So, I link npm and node to /use/bin,it will start.

avoiding php artisan queue:work : Queue Driver - Redis or Database (Laravel 5.4)

I am currently using database as my queue driver, I have installed Laravel 5.4 on Windows 10 PC. In order to process queues I have been using php artisan queue:work which was completely fine in development stage. Now, the project is completely ready and needs to be deployed on Linux Server (Dedicated) I am not sure how to avoid running command php artisan queue:work on terminal in order to process mail jobs?
I have deployed once in shared hosting and I have used cron jobs, But now I have dedicated server I guess I should be able to use something else to run jobs, I was also thinking of using Redis as queue driver rather than database as queue driver
I need some suggestion on what is best. And how to avoid php artisan queue:work on dedicated server? Do I need to write small script to make sure jobs run in background as a service.
Laravel documentation covers this with supervisor.
See: Laravel Supervisor configuration
Supervisor is a process monitor which makes sure your queue command (or any other command for that matter) is executed and restarted if it dies.
Edit:
See: Supervisor documentation
Basically for centos, you can use yum:
yum install supervisor
Easy install
// required for easy_install (if not installed already)
yum install python-setuptools
// install supervisor
easy_install supervisor
Or pip
pip install supervisor
After that it's just creating your config (based on the example given on Laravel's documentation), this is handled step by step in:
Supervisor: creating-a-configuration-file
And create the service: Setup Supervisor
After that you can start the service with:
service supervisord start

NodeJS API with external deps in other language

I am developing a NodeJS API and everything is ok.
For an specific issue I am using a local CLI dependency that process some input files and output other stuff, in case to return from the API.
I wanted to know (maybe open my mind on) what kind of service I can use to serve this API in production.
The idea is to have a Node environment (like in my local) that can have installed in the same machine an external dependency not necessarily written in Node.
My specific dependency is fontforge and other little things.
Thanks in advance.
It's hard to beat a good VPS if you need to install custom software that is not easy to install with npm. My favorite VPS provider is Digital Ocean. You can have two months of a basic server for free with this link so you can see if it's ok for you before you pay anything. By second favorite VPS provider is Vultr because you can install custom ISOs on their servers. You can try it for free with this link. But it will mean taking care of the server yourself. With services like Heroku all of that is taken care for you - but you can't install whatever you want there. With a VPS you get your own server with root access. Usually it's Linux but Digital Ocean also supports FreeBSD and some people install OpenBSD, though it's not officially supported. With a VPS you can install whatever you want, but you have to do it yourself. There is always a trade off.
More info
Installing Node
To install Node on the VPS, my recommendation is to install in /opt with a versioned directory and a symlink - this is an example procedure that I wrote for a different answer:
# change dir to your home:
cd ~
# download the source:
curl -O https://nodejs.org/dist/v6.1.0/node-v6.1.0.tar.gz
# extract the archive:
tar xzvf node-v6.1.0.tar.gz
# go into the extracted dir:
cd node-v6.1.0
# configure for installation:
./configure --prefix=/opt/node-v6.1.0
# build and test:
make && make test
# install:
sudo make install
# make a symlink to that version:
sudo ln -svf /opt/node-v6.1.0 /opt/node
See this answer for more info.
Your start scripts
To have your own application nicely started on server startup - here is an example Upstart script based on the one that I'm using - it should work on Ubuntu 14.04, not tested on newer versions - save it in /etc/init/YOURAPP.conf:
# When to start the service
start on runlevel [2345]
# When to stop the service
stop on runlevel [06]
# If the process quits unexpectadly trigger a respawn
respawn
# Start the process
exec start-stop-daemon --start --chuid node --make-pidfile --pidfile /www/YOURAPP/run/node-upstart.pid --exec /opt/node/bin/node -- /www/YOURAPP/app/app.js >> /www/YOURAPP/log/node-upstart.log 2>&1
Just change:
YOURAPP to the name of your own app
/opt/node/bin/node to your path to node
/www/YOURAPP/app/app.js to the path of your Node app
/www/YOURAPP/run to where you want your PID file
/www/YOURAPP/log to where you want your logs
--chuid node to --chuid OTHERUSER if you want it to run as a different user than node
(make sure to add a user with a name from --chuid above)
With your /etc/init/YOURAPP.conf in place you can safely restart your server and have your app still running, you can run:
start YOURAPP
restart YOURAPP
stop YOURAPP
to start, restart and stop your app - which would also happen automatically during the system boot or shutdown.

How do you use pm2 startup with a non-root user?

According to the documentation here: http://pm2.keymetrics.io/docs/usage/startup/#startup-systems-support
You can use the command pm2 startup ubuntu -u nodeapps to resurrect all saved pm2 jobs on server startup.
I ran this command as the nodeapps user. Then I was given a sudo su command to run. I logged out of nodeapps, used sudo su to log into the system as root, and ran the command:
sudo su -c "env PATH=$PATH:/usr/bin pm2 startup ubuntu -u nodapps --hp /home/nodeapps"
The processes did not restart on server restart. I found this question on Stack Overflow: Ubuntu 14.04 - pm2 startup not starting after reboot.
In the script /etc/init.d/pm2-init.sh I found the line that question recommended addressing:
export PATH=/usr/bin:$PATH
export PM2_HOME="/home/nodeapps/.pm2"
But it looks correct to me so I didn't change anything.
I then found this question: pm2 Startup not starting up on Ubuntu
and in my boot logs I find the following line:
Starting pm2
/usr/bin/env: node: No such file or directory
I know that 'node' on Ubuntu is actually 'nodejs'. Could this be the reason?
If it is, what can I do to make the startup command look for nodejs instead of node.
Alternatively, could this be a $PATH problem? If it is, how can I add the correct path to root (at least I think it should be added to root)
I don't know if it will help you but I use in this way:
As a non-root user
pm2 startup -u <YOUR_NON_ROOT_USER>
Copy line showed like
env PATH=$PATH:/usr/bin pm2 startup systemd -u delivery --hp /home/delivery
As a root execute
env PATH=$PATH:/usr/bin pm2 startup systemd -u delivery --hp /home/delivery
Back to non root user and type:
pm2 start <YOUR /PATH/TO/INDEX.JS> --name <YOU_APPLICATION_NAME>
As a non-root type:
pm2 save
reboot
sudo reboot
As a non-root user type the commando bellow to check if it works
pm2 status
PS: Change as needed.
I hope it will be useful for you or someone.
(Posted on behalf of the OP).
In fact that was the problem. Fixed via creating a symlink (as root):
ln -s /usr/bin/nodejs /usr/sbin/node

Resources