Seize the day

POST : Backend study

우분투 vm 생성후 각종 서버 설정

https://www.youtube.com/watch?v=4yXIYUzsjbA&t=325s 를 참고했다. 

======= new 계정 생성 (daejeong) =======
# ID 추가
sudo adduser daejeong

		dajkim76@base-backend-1:~$ sudo adduser daejeong
		Adding user `daejeong' ...
		Adding new group `daejeong' (1004) ...
		Adding new user `daejeong' (1003) with group `daejeong' ...
		Creating home directory `/home/daejeong' ...
		Copying files from `/etc/skel' ...
		New password:
		Retype new password:
		No password supplied
		New password:
		Retype new password:
		passwd: password updated successfully
		Changing the user information for daejeong
		Enter the new value, or press ENTER for the default
			Full Name []:
			Room Number []:
			Work Phone []:
			Home Phone []:
			Other []:
		Is the information correct? [Y/n]

#sudo 권한 주기
sudo usermod -aG sudo daejeong


# 로그인 후 ssh 설정 복사하기
sudo su - daejeong
sudo cp -r /home/dajkim76/.ssh /home/daejeong/
sudo chown -R daejeong:daejeong /home/daejeong/.ssh

======== time zone ===========
# 타임존 확인
timedatectl

		daejeong@base-backend-1:~$ timedatectl
		               Local time: Fri 2024-06-28 17:12:14 UTC
		           Universal time: Fri 2024-06-28 17:12:14 UTC
		                 RTC time: Fri 2024-06-28 17:12:14
		                Time zone: Etc/UTC (UTC, +0000)
		System clock synchronized: yes
		              NTP service: active
		          RTC in local TZ: no


# Aisa/Seoul로 변경
sudo timedatectl set-timezone Asia/Seoul		          


========= docker 설치 =========
# apt 업데이트
sudo apt update
sudo apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common

		// 에러 발생시
		sudo killall apt apt-get
		// lock 삭제
		sudo rm /var/lib/apt/lists/lock
		sudo rm /var/cache/apt/archives/lock
		sudo rm /var/lib/dpkg/lock*
		// 마무리
		sudo rm -rf /var/lib/dpkg/updates/*
		sudo dpkg --configure -a
		sudo apt update

# 도커 GPG 추가
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add

# repository 경로 추가
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

# 도커 설치
sudo apt update
sudo apt install docker-ce

#도커 소켓 권한
sudo chmod 666 /var/run/docker.sock
sudo chown root:docker /var/run/docker.sock

# docker를 sudo없이 사용하기위해서
sudo usermod -aG docker daejeong
newgrp docker

# 도커 설치 확인
docker info
service docker status
		● docker.service - Docker Application Container Engine
		     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
		     Active: active (running) since Sat 2024-06-29 02:34:11 KST; 19min ago
		TriggeredBy: ● docker.socket
		       Docs: https://docs.docker.com
		   Main PID: 20815 (dockerd)
		      Tasks: 11
		     Memory: 33.2M
		     CGroup: /system.slice/docker.service
		             └─20815 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock


# docker compose 설치
sudo curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose

sudo chmod +x /usr/local/bin/docker-compose
docker-compose version

# hello world 실행해보기
docker run hello-world


========= nvm & node ==========
# nvm 설치 'https://github.com/nvm-sh/nvm/releases'
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

source ~/.bashrc

# 사용가능한 node 조회
nvm ls-remote

# node 20.x 최신설치
nvm install 20

		daejeong@base-backend-1:~$ node -v
		v20.15.0
		daejeong@base-backend-1:~$ npm -v
		10.7.0

		nvm use 20.15.0
		nvm unsintall 20.15.0
		nvm use 20.15.0

# 443 포트에서 node실행하려면 권한추가 
which node
	/home/daejeong/.nvm/versions/node/v20.15.0/bin/node

sudo setcap 'cap_net_bind_service=+ep' /home/daejeong/.nvm/versions/node/v20.15.0/bin/node


========= bun js runtime 설치 ========
curl -fsSL https://bun.sh/install | bash

source ~/.bashrc

bun --version
		daejeong@base-backend-1:~$ bun --version
		1.1.17

		나중에 업그레이드
		bun upgrade


======= 기타 nginx, git =========
sudo apt install nginx

cat /etc/nginx/sites-enabled/default

sudo service nginx start


sudo apt install git


======== 보안상 안려진 계정 삭제 ========
sudo deluser ubuntu



====== 주기적 실행 ========
daejeong@base-backend-1:~$ cat cron_job.sh
		#!/bin/sh

		echo $(date +'%Y%m%d_%H%M%S') >> ~/cron.log

chmod +x cron_job.sh


# crontab -e는 현재 사용자의 주기적 실행이므로 실행 아이디를 지정할 필요가 없고, 저장하면 자동으로 반영됨 
crontab -e

# 매 분 마다 실행
* * * * * ~/cron_job.sh
		
# 몇 분후 결과 화인
		daejeong@base-backend-1:~$ cat cron.log
		20240629_034101
		20240629_034201
		20240629_034302
		20240629_034401

or

vi /etc/crontab
sudo service cron restart
sudo service cron status
		참고..
		https://okkks.tistory.com/904


======= 서버 자동 실행 =======
sudo vi  /lib/systemd/system/rc-local.service

#맨 마지막에 아래 두 줄 추가
[Install]
WantedBy=multi-user.target

# 설정파일에 rc.local의 경로가 나온다. 
# ConditionFileIsExecutable=/etc/rc.local
# /etc/rc.d/rc.local  아니면 /etc/rc.local 이다. 

sudo vi /etc/rc.local
		#!/bin/bash

		#test
		su - daejeong -c 'echo "reboot at $(date +"%Y%m%d_%H%M%S")"'

# 실행권한 주기
sudo chmod +x /etc/rc.local

# 서비스 등록
sudo systemctl enable rc-local.service

# reboot후에 정상 실행 확인
sudo reboot

# 좀 있다 다시 접속해서 rc-local.service 동작 확인 (reboot at 20240629_034720 확인)
sudo systemctl status rc-local.service

		daejeong@base-backend-1:~$ sudo systemctl status rc-local.service
		[sudo] password for daejeong:
		● rc-local.service - /etc/rc.local Compatibility
		     Loaded: loaded (/lib/systemd/system/rc-local.service; enabled; vendor preset: enabled)
		    Drop-In: /usr/lib/systemd/system/rc-local.service.d
		             └─debian.conf
		     Active: active (exited) since Sat 2024-06-29 03:47:20 KST; 1min 30s ago
		       Docs: man:systemd-rc-local-generator(8)
		      Tasks: 0 (limit: 650)
		     Memory: 0B
		     CGroup: /system.slice/rc-local.service

		Jun 29 03:47:11 base-backend-1 systemd[1]: Starting /etc/rc.local Compatibility...
		Jun 29 03:47:13 base-backend-1 su[499]: (to daejeong) root on none
		Jun 29 03:47:14 base-backend-1 su[499]: pam_unix(su-l:session): session opened for user daejeong by (uid=0)
		Jun 29 03:47:21 base-backend-1 rc.local[605]: reboot at 20240629_034720
		Jun 29 03:47:20 base-backend-1 systemd[1]: Started /etc/rc.local Compatibility.
top

posted at

2024. 6. 29. 04:44


CONTENTS

Seize the day
BLOG main image
김대정의 앱 개발 노트와 사는 이야기
RSS 2.0Tattertools
공지
아카이브
최근 글 최근 댓글
카테고리 태그 구름사이트 링크