Skip to content

Instantly share code, notes, and snippets.

@moaalaa
Last active April 15, 2024 14:13
Show Gist options
  • Save moaalaa/010ac252cb91eb2d2c00248a820e989d to your computer and use it in GitHub Desktop.
Save moaalaa/010ac252cb91eb2d2c00248a820e989d to your computer and use it in GitHub Desktop.
How to run PM2 with laravel queue and laravel echo server
# Node Js
# Centos7
curl -sL https://rpm.nodesource.com/setup_14.x | sudo bash -
sudo yum install nodejs
# Ubuntu 20.04
curl -sL https://deb.nodesource.com/setup_14.x | sudo bash -
sudo apt install nodejs
node --version
npm --version
# Redis
# Centos 7
sudo yum install epel-release
sudo yum install redis -y
# Ubuntu 20.04
curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list
sudo apt install redis
# redis in ubunut by default not working in systemd to auto start so we must tell it to do so
sudo nano /etc/redis/redis.conf
# Seach for the supervisor and change it to systemd
supervised systemd
# Check Redis status
sudo systemctl status redis.service
redis-cli ping
BROADCAST_DRIVER=redis
QUEUE_CONNECTION=redis
// laravel-echo-server init
{
"authHost": "https://your-domain.com",
"authEndpoint": "/broadcasting/auth",
"clients": [
{
"appId": "bac3036615df07cf",
"key": "15fdd7bdd669b966a11f1a9bb6409595"
}
],
"database": "redis",
"databaseConfig": {
"redis": {},
"sqlite": {
"databasePath": "/database/laravel-echo-server.sqlite"
}
},
"devMode": true,
"host": "your-domain.com",
"port": "6001",
"protocol": "https",
"socketio": {},
"secureOptions": 67108864,
"sslCertPath": "/home/CPANEL USER NAME/ssl/certs/domain_cert",
"sslKeyPath": "/home/CPANEL USER NAME/ssl/keys/domain key", // it will be hashed file name try all fiels if you can
"sslCertChainPath": "",
"sslPassphrase": "",
"subscribers": {
"http": true,
"redis": true
},
"apiOriginAllow": {
"allowCors": true,
"allowOrigin": "https://your-domain.com",
"allowMethods": "GET, POST",
"allowHeaders": "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id"
}
}
require('dotenv').config();
const env = process.env;
require('laravel-echo-server').run({
authHost: env.APP_URL,
devMode: env.APP_DEBUG,
database: "redis",
databaseConfig: {
redis: {
host: env.REDIS_HOST,
port: env.REDIS_PORT,
}
}
});
module.exports = {
apps: [
{
name: 'laravel-echo-server',
script: 'laravel-echo-server',
instances : 1, // default 'laravel-echo-server' required 1 instance only to work
exec_mode : 'fork', // default
interpreter: 'node', // default
args: 'start',
error_file: './storage/logs/pm2/pm2.error.log',
out_file: './storage/logs/pm2/pm2.out.log',
pid_file: './storage/logs/pm2/pm2.pid.log',
cwd: "/home/CPANEL USER NAME/www",
},
{
name: 'laravel-queue-worker',
script: 'artisan',
exec_mode: 'fork', // default
interpreter: 'php',
instances: 1,
args: 'queue:work --queue=videos_conversions,notifications,default --tries=5 --sleep=1',
error_file: './storage/logs/pm2/pm2.error.log',
out_file: './storage/logs/pm2/pm2.out.log',
pid_file: './storage/logs/pm2/pm2.pid.log',
cwd: "/home/CPANEL USER NAME/www",
},
]
};
# You Must Be Root
sudo npm install -g pm2
# Register pm2 to startup
# Run without sudo it will generate a command just copy/past the generated command
pm2 startup
# unRegister pm2 to startup
# Run without sudo it will generate a command just copy/past the generated command
pm2 unstartup
# All Command are on CentOs7
# Allow port 6001 in firewall for using it in laravel echo server
sudo firewall-cmd --zone=public --add-port=6001/tcp --permanent
# Reload Firewall
sudo firewall-cmd --reload
# Check for port
sudo iptables-save | grep 6001
# -A IN_public_allow -p tcp -m tcp --dport 6001 -m conntrack --ctstate NEW,UNTRACKED -j ACCEPT
# Method 1: Start PM2 as normal user (not server user but host or cpanel normal user)
# Add your host or cpanel user to "wheel" group to use "su" command
# Centos 7
usermod -G wheel your_user_name
# Ubunut 20.04
usermod -G sudo your_user_name
# Remomber now your user can use "su" command but it will required "root" user passowrd keep in mind
# By allowing your user to use "su" now pm2 will work correctlly and yu don't need to use "su" with it
# Go to your app root path and Start PM2 and Provide Path to pm2.config.js (autocomplete paths in terminal may not work)
pm2 start pm2.config.js
# Stop PM2 and Provide Path to pm2.config.js (autocomplete paths in terminal may not work)
pm2 stop pm2.config.js
# Restart PM2 and Provide Path to pm2.config.js (autocomplete paths in terminal may not work)
pm2 restart pm2.config.js
# Log All pm2 Apps
pm2 logs
# Log specific pm2 app
# sudo pm2 logs [app name]
pm2 logs laravel-echo-server
pm2 logs laravel-queue-worker
#---------------------------------------#---------------------------------------#---------------------------------------
# Method 2: Start PM2 as Root (server user not host or cpanel normal user) or it will not work
# Start PM2 and Provide Path to pm2.config.js (autocomplete paths in terminal may not work)
sudo pm2 start "/home/CPANEL USER NAME/www/pm2.config.js"
# Stop PM2 and Provide Path to pm2.config.js (autocomplete paths in terminal may not work)
sudo pm2 stop "/home/CPANEL USER NAME/www/pm2.config.js"
# Restart PM2 and Provide Path to pm2.config.js (autocomplete paths in terminal may not work)
sudo pm2 restart "/home/CPANEL USER NAME/www/pm2.config.js"
# Log All pm2 Apps
sudo pm2 logs
# Log specific pm2 app
# sudo pm2 logs [app name]
sudo pm2 logs laravel-echo-server
sudo pm2 logs laravel-queue-worker

Install ffmpeg for centos 7 using build source file

1 - Download FFmpeg build tar file

cd /opt

sudo wget https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-amd64-static.tar.xz

2 - Verify FFmpeg tar file using md5sum

sudo wget https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-amd64-static.tar.xz.md5

md5sum -c ffmpeg-git-amd64-static.tar.xz.md5

3 - Untar the FFmpeg build

udo tar xvf ffmpeg*.xz

4 - Go to ffmpeg directory

cd ffmpeg-*-static

5 - Create symlink for ffmpeg and ffprobe binaries

sudo ln -s "${PWD}/ffmpeg" /usr/bin/

sudo ln -s "${PWD}/ffprobe" /usr/bin/

6 - Check ffmpeg version

ffmpeg -version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment