Skip to content

Instantly share code, notes, and snippets.

@mamedinfo
Last active June 27, 2023 03:30
Show Gist options
  • Save mamedinfo/3a673039e8308eb622bd101ebfaa91de to your computer and use it in GitHub Desktop.
Save mamedinfo/3a673039e8308eb622bd101ebfaa91de to your computer and use it in GitHub Desktop.
Ec2-nginx-mysql-nodejs.mdx

Nginx

  1. sudo dnf list | grep nginx //Search

  2. sudo dnf update //To Install Latest Update

  3. sudo dnf install -y nginx // Install Nginx

  4. sudo systemctl start nginx.service //Start Nginx Server

  5. sudo systemctl status nginx.service // Check Server Status

  6. sudo systemctl enable nginx.service // Enable Auto Server Start on

  7. sudo yum update

  8. sudo yum install -y nginx

  9. sudo systemctl start nginx.service

  10. sudo systemctl enable nginx.service // auto-start nginx service

  11. nginx -v

  12. sudo systemctl status nginx.service

Certbot and auto renew

sudo yum install certbot
sudo certbot certonly --standalone -d example1.com -d www.example1.com -d example2.com -d www.example2.com

crontab -e
0 0 * * * certbot renew --quiet

Swap

sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap  /swapfile
sudo swapon /swapfile
sudo swapon  --show
free -h

Mysql

  1. sudo wget https://dev.mysql.com/get/mysql80-community-release-el9-1.noarch.rpm

  2. sudo ls -lrt

  3. sudo dnf install mysql80-community-release-el9-1.noarch.rpm

  4. dnf repolist enabled | grep "mysql.-community."

  5. sudo dnf install mysql-community-server

  6. sudo systemctl start mysqld

  7. sudo systemctl enable mysqld

  8. sudo mysql -V

  9. sudo mysql_secure_installation

sudo grep 'temporary password' /var/log/mysqld.log
mysql -u root -p

 //change root password
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'MyNewPass1!';

// create new user(sam) 
CREATE USER 'sam'@'localhost' IDENTIFIED WITH mysql_native_password BY 'MyNewPass1!';
GRANT ALL PRIVILEGES ON lotr.* TO 'sam'@'localhost'; // access only lotr database

//if you want to be able to access the database from anywhere else
CREATE USER 'frodo'@'%' IDENTIFIED WITH mysql_native_password BY 'MyNewPass1!';
GRANT ALL PRIVILEGES ON *.* TO 'frodo'@'%';

Nodejs

  1. curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash // https://github.com/nvm-sh/nvm/releases
  2. ~/.nvm/nvm.sh
  3. nvm install --lts
  4. node -v

PM2

  1. yarn global add pm2
  2. pm2 start ecosystem.config.js
ecosystem.config.js

module.exports = {
  apps: [
    {
      name: "myapp",
      script: "app.js",
      instances: 0,
      exec_mode: "cluster",
      autorestart: true,
      watch: false,
      max_memory_restart: "1G",
      env: {
        NODE_ENV: "production",
        PORT: 4000
      }
    }
  ]
};

systemd service

sudo vim /etc/systemd/system/YourAppName.service

[Unit]
Description=My Node.js App
After=network.target

[Service]
ExecStart=/usr/bin/node /path/to/your/app.js
WorkingDirectory=/path/to/your/app_directory
Restart=always
User=your_username
Environment=NODE_ENV=production

[Install]
WantedBy=multi-user.target

Another example

[Unit]
Description=YourAppName
After=multi-user.target

[Service]
ExecStart=/usr/bin/node /home/ec2-user/your_app_dir/server.js
Restart=always
RestartSec=10
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=YourAppName
User=ec2-user
EnvironmentFile=/home/ec2-user/your_app_dir/app.env

[Install]
WantedBy=multi-user.target

  1. Start the service: Run sudo systemctl start myapp to start the service.
  2. Stop the service: Run sudo systemctl stop myapp to stop the service.
  3. Restart the service: Run sudo systemctl restart myapp to restart the service.
  4. Enable the service to start at boot: Run sudo systemctl enable myapp to enable automatic startup at boot time.
  5. Disable the service from starting at boot: Run sudo systemctl disable myapp to disable automatic startup.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment