What We Need

  • A VPS which support mounting ISO file
  • A Computer with any Linux OS
  • Some spare space on the disk of your computer

Actually, we do not really need the Linux computer. However, my VPS is the cheapest and CPU is not powerful enough, which means I have to spend very very long time waiting it compiling. So I compiled the whole system, compressed it, uploaded it to the VPS, and finally configured it.

Backup Important Files and Databases

Firstly, DO NOT REMEMBER TO BACKUP EVERYTHING YOU NEED ON YOUR VPS! When it comes to me, I use snapshot feature of Bandwagon VPS.

You can also backup your file and database using command like this:

mysqldump --databases YOUR_DATABASE > dump.sql 
tar cvpzf backup.tar.gz YOUR_FILES

Build Up Your Gentoo System

On your computer,  shrink a disk partition, and create a new one. My new partition is /dev/sda5. Then you can do like this, remember to replace Bold words:

mkdir /mnt/gentoo
mount /dev/sda5 /mnt/gentoo
cd /mnt/gentoo
wget https://mirrors4.tuna.tsinghua.edu.cn/gentoo/releases/amd64/autobuilds/current-stage3-amd64-systemd/stage3-amd64-systemd-YYYYMMDD.tar.bz2
tar vxpf stage3-*.tar.bz2 --xattrs-include='.*' --numeric-owner

nano /mnt/gentoo/etc/portage/make.conf #AND ADD THESE TWO LINES BLELOW
GENTOO_MIRRORS="https://mirrors.tuna.tsinghua.edu.cn/gentoo/"
EMERGE_DEFAULT_OPTS="--ask --verbose=y --keep-going --with-bdeps=y --load-average"

cp --dereference /etc/resolv.conf /mnt/gentoo/etc/
mount -t proc /proc /mnt/gentoo/proc
mount --rbind /sys /mnt/gentoo/sys
mount --make-rslave /mnt/gentoo/sys
mount --rbind /dev /mnt/gentoo/dev
mount --make-rslave /mnt/gentoo/dev

test -L /dev/shm && rm /dev/shm && mkdir /dev/shm
mount --types tmpfs --options nosuid,nodev,noexec shm /dev/shm
chmod 1777 /dev/shm

chroot /mnt/gentoo /bin/bash
source /etc/profile
export PS1="(chroot) ${PS1}"

emerge-webrsync

echo "Asia/Shanghai" > /etc/timezone
emerge --config sys-libs/timezone-data

echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen
locale-gen
eselect locale list
eselect locale set "en_US.utf8"
env-update && source /etc/profile && export PS1="(chroot) $PS1"

emerge -auvDN --with-bdeps=y @world
emerge app-admin/sysklogd sys-process/cronie sudo layman vim netorkmanager grub gentoo-sources

If you want to know what these command actually means, you can refer to this essay or Gentoo Handbook.

Compile Your Kernel

cd /usr/src/linux
make menuconfig

And then configure these things:

Gentoo Linux --->
Support for init systems, system and service managers --->
[*] systemd

Processor type and features --->
[*] Linux guest support --->
[*] Enable Paravirtualization code
[*] KVM Guest support (including kvmclock) Device Drivers --->
Virtio drivers --->
<*> PCI driver for virtio devices
[*] Block devices --->
<*> Virtio block driver
SCSI device support --->
[*] SCSI low-level drivers --->
[*] virtio-scsi support
[*] Network device support --->
[*] Network core driver support
<*> Virtio network driver

File systems --->
<*> F2FS filesystem support #You can use any file system you like.

You can also cancel some useless option, like Sound Support. Finally, type the command “make” and wait it compiling. DO NOT “make install modules_install” NOW!

Install Web Server

echo "fpm mysql mysqli" >> /etc/portage/package.use/web
emerge -av "=php-7.3.3" mysql
curl https://getcaddy.com | bash -s personal http.nobots,tls.dns.cloudflare
bash <(curl -L -s https://install.direct/go.sh)

And then we should alter Caddyfile like this:

jerryding.site {
tls {
dns cloudflare
}
root /var/www/jerryding.site
gzip
fastcgi / /run/php-fpm.socket php {
index index.php login.php
}

rewrite {
if {path} not_match ^\/wp-admin
to {path} {path}/ /index.php?{query} }
}

And add these two lines to /etc/php/fpm-php7.3/fpm.d/www.conf

security.limit_extensions =
listen = /run/php-fpm.socket
listen.owner = caddy
listen.group = caddy

Pack the System

First umount everything mounted. I just use the command “reboot”.

Then mount /dev/sda5, delete useless things, change dictionary to /mnt, and pack the system up. Like this:

sudo mount /dev/sda5 /mnt/gentoo
rm /mnt/gentoo/stage3*
rm /mnt/gentoo/tmp/* -rf
cd /mnt
tar cvjpf gentoo.tar.gz gentoo/

Preparation on VPS

After Backup important files to a safe place, we can shutdown the machine, mount an ISO file (I use Arch here), and boot into the Live CD using VNC. To make it easier, I started sshd first.

systemctl start sshd
passwd

Then connect to the VPS, and partition the disk. Disk on my vps uses MBR format, so I just use cfdisk , allocating 100MB for boot partation, and the left spaces was allocated to root partation. As for swap, I will use a swap file later.

After that we should creating file systems for each partition. I prefer fat32 for boot and f2fs for rootfs.

mkfs.vfat -F32 /dev/sda1
mkfs.f2fs /dev/sda2 -f

Mount filesystem

mkdir /mnt/gentoo
mount /dev/sda2 /mnt/gentoo

Upload the New System

To upload the System, Use this command on YOUR COMPUTER:

rsync -rvz --progress /mnt/gentoo.tar.gz root@IP_of_VPS:/mnt/gentoo

Then decompress the System on your VPS:

tar xvpf gentoo.tar.gz
mv gentoo/* .
rm gentoo/ -rf

Well done! We can chroot into Gentoo now!

cp --dereference /etc/resolv.conf /mnt/gentoo/etc/
mount -t proc /proc /mnt/gentoo/proc
mount --rbind /sys /mnt/gentoo/sys
mount --make-rslave /mnt/gentoo/sys
mount --rbind /dev /mnt/gentoo/dev
mount --make-rslave /mnt/gentoo/dev

test -L /dev/shm && rm /dev/shm && mkdir /dev/shm
mount --types tmpfs --options nosuid,nodev,noexec shm /dev/shm
chmod 1777 /dev/shm

chroot /mnt/gentoo /bin/bash
source /etc/profile
export PS1="(chroot) ${PS1}"

Install the kernel:

momunt /dev/sda1 /boot
cd /usr/src/linux
make install modules_install

Install the boot-loader:

grub-install --target=i386-pc /dev/sda
grub-mkconfig -o /boot/grub/grub.cfg

Create a swap file:

dd if=/dev/zero of=/root/swapfile bs=1M count=1024 
mkswap /root/swapfile
swapon /root/swapfile

Configure fstab, ssh and NetworkMnager:

vim fstab #I think you konw how to config it.
systemctl enable NetworkManager
vim /etc/ssh/sshd_config #add "permitrootlogin = yes"
systemctl enable sshd

Now you can reboot the vps, and finally set up your website! To set up a website, you can follow this essay.

Congratulations, you make it!

10 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *