Skip to content

Amazon Linux 2

Tibor Bödecs edited this page Jul 27, 2023 · 19 revisions

⚠️ This is a work in progress guide about setting up a really basic environment for Feather CMS.

These are the most necessary steps to host Feather using an (AWS) Amazon Linux 2 instance.

OS

cat /etc/os-release

Environment

sudo vi /etc/environment

# contents
LANG=en_US.utf-8
LC_ALL=en_US.utf-8

🔒 Keep in mind that you might want to enable other security features or install monitoring tools.

EPEL

Add EPEL - https://fedoraproject.org/wiki/EPEL

sudo amazon-linux-extras install epel -y
# sudo yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
# sudo yum-config-manager --enable epel

Set editor & custom PS (personal preference)

# editor & PS1 .bashrc

export EDITOR='/usr/bin/vi'
export VISUAL='/usr/bin/vi'
PS1='\u@\w: '

SSH agent

# .bashrc
if [ ! -S ~/.ssh/ssh_auth_sock ]; then
   eval `ssh-agent`
   ln -sf "$SSH_AUTH_SOCK" ~/.ssh/ssh_auth_sock
fi
export SSH_AUTH_SOCK=~/.ssh/ssh_auth_sock

Add swapfile

When physical RAM is not enough

# 128mb * 32 = 4gb
sudo dd if=/dev/zero of=/swapfile bs=128M count=32
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo swapon -s
sudo vi /etc/fstab
#add line
/swapfile swap swap defaults 0 0

# reboot
free -hm

Install nginx

sudo yum install nginx
# sudo amazon-linux-extras list | grep nginx
# sudo amazon-linux-extras enable nginx1
# sudo amazon-linux-extras install -y nginx1 
# nginx -v

# auto start nginx after boot
sudo chkconfig nginx on

Configure nginx

Setup web directory

# sudo mkdir -p /var/www/feathercms.com/
# sudo vi /var/www/feathercms.com/index.html

# contents:
feathercms.com

Setup domain configuration

# sudo vi /etc/nginx/conf.d/feathercms.com.conf

# contents
server {
    listen	 80;
    server_name  www.feathercms.com feathercms.com;
    root /var/www/feathercms.com/;
}
sudo service nginx restart

Install letsencrypt's certbot

# install certbot with nginx plugin
sudo yum install certbot python-certbot-nginx

# generate certificate 
sudo certbot --nginx -d feathercms.com

Auto renew certificates

# sudo crontab -e 

0 12 * * * /usr/bin/certbot renew --quiet

Install git

sudo yum install git -y

Install swiftenv

git clone https://github.com/kylef/swiftenv.git ~/.swiftenv
echo 'export SWIFTENV_ROOT="$HOME/.swiftenv"' >> ~/.bash_profile
echo 'export PATH="$SWIFTENV_ROOT/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(swiftenv init -)"' >> ~/.bash_profile

Install Swift

https://swift.org/download/#releases

sudo yum install binutils gcc git glibc-static gzip libbsd libcurl libedit libicu libsqlite libstdc++-static libuuid libxml2 tar tzdata
sudo yum install libsqlite3x.x86_64 libsqlite3x-devel.x86_64 
sudo yum install -y zlib zlib-devel

swiftenv install --list
swiftenv install 5.5.2
swift --version

Systemctl

# sudo vi /lib/systemd/system/todo.service
[Unit]
Description=Todo server daemon

[Service]
User=ec2-user
Group=ec2-user
WorkingDirectory=/var/www/todoapp/
ExecStart=/var/www/todoapp/Run --log notice
Restart=always

[Install]
WantedBy=multi-user.target

# commands to run
sudo chown -R ec2-user:ec2-user /var/www/todoapp/
sudo chmod +x /lib/systemd/system/todo.service
sudo systemctl daemon-reload
sudo systemctl enable todo.service
sudo systemctl start todo
sudo systemctl status todo

env

Nginx proxy

server {
    listen 80;
    server_name mytododomain.com;

    location / {
        proxy_pass              http://localhost:8080;
        proxy_set_header        Host $host;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header        X-Forwarded-Proto $scheme;
        proxy_read_timeout      90;
    }
}

Build & deploy

swift package update
swift build
sudo systemctl stop todo

rm Run
rm -rf ./Resources
rm -rf feather-core_FeatherCore.resources/

cp -R .build/debug/feather-core_FeatherCore.resources/ ./
cp .build/debug/Feather ./Run

sudo systemctl start todo 
sudo systemctl status todo