Guangning Yu's Blog

Mount the Amazon EFS File System on the EC2 Instance

2020-08-08 09:53:34
  1. Create EFS on AWS web portal

  2. Edit the security group of EFS to allow access from EC2 instances
    title

  3. Mount EFS on EC2

    1. sudo mkdir efs

    title

    1. sudo chmod 777 /efs
  4. Install amazon-efs-utils for auto-remount

    1. git clone https://github.com/aws/efs-utils
    2. cd efs-utils/
    3. ./build-deb.sh
    4. sudo apt-get -y install ./build/amazon-efs-utils*deb
  5. Configure IAM role in EC2 (already done)

  6. Edit /etc/fstab

    1. fs-xxxxxxxx:/ /efs efs _netdev,tls,iam 0 0
  7. Test mount

    1. sudo mount -fav
  8. Add Linux user in the other EC2's group to avoid readonly issue

    1. sudo usermod -a -G ubuntu guangningyu
    1. sudo usermod -a -G guangningyu ubuntu

Reference:
1. Mount the Amazon EFS File System on the EC2 Instance and Test
2. Mounting your Amazon EFS file system automatically
3. User and Group ID Permissions for Files and Directories Within a File System

Install Azure Cli on Mac

2020-02-25 15:13:21
  1. brew update && brew install azure-cli
  2. az login
  1. brew tap azure/functions
  2. brew install azure-functions-core-tools@2

References:
Install Azure CLI on macOS
Azure/azure-functions-core-tools

Create User in Windows Server 2016

2019-12-16 14:31:22
  1. Run [Server Manager] and Open [Tools] - [Computer Management].
  2. Right-Click [Users] under the [Local Users and Groups] on the left pane and select [New User].
  3. Input UserName and Password for a new user and click [Create] button. Other intems are optional to set.
  4. After creating normally, New user is shown on the list like follows.
  5. If you'd like to set administrative priviledge to the new user, Right-click the user and open [Properties].
  6. Move to [Member of] tab and click [Add] button.
  7. Specify [Administrators] group like follows.
  8. Make sure [Administrators] group is added on the list and click [OK] button to finish settings.

Reference: Windows Server 2016 : Initial Settings : Add Local Users

Mount S3 bucket on EC2 Linux Instance

2019-09-16 10:51:31  |  AWS
  1. Install dependencies

    1. sudo apt-get update
    2. sudo apt-get install automake autotools-dev fuse g++ git libcurl4-gnutls-dev libfuse-dev libssl-dev libxml2-dev make pkg-config
  2. Install s3fs

    1. git clone https://github.com/s3fs-fuse/s3fs-fuse.git
    2. cd s3fs-fuse
    3. ./autogen.sh
    4. ./configure --prefix=/usr --with-openssl
    5. make
    6. sudo make install
    7. which s3fs
  3. Config credentials

    1. echo "Your_accesskey:Your_secretkey" >> /etc/passwd-s3fs
    2. sudo chmod 640 /etc/passwd-s3fs
  4. Create mounting point

    1. mkdir /mys3bucket
    2. s3fs your_bucketname -o use_cache=/tmp -o allow_other -o uid=1001 -o mp_umask=002 -o multireq_max=5 /mys3bucket
  5. Config mount after reboot

    Add the following command in /etc/rc.local:

    1. /usr/local/bin/s3fs your_bucketname -o use_cache=/tmp -o allow_other -o uid=1001 -o mp_umask=002 -o multireq_max=5 /mys3bucket

Reference:
How to Mount S3 bucket on EC2 Linux Instance

Setup Nextcloud on Ubuntu

2019-09-09 23:06:06
  • Install Nextcloud
  1. # Install Nextcloud stack
  2. sudo snap install nextcloud
  3. # Create administrator account
  4. sudo nextcloud.manual-install <admin_username> <admin_password>
  5. # Configure trusted domains (only localhost by default)
  6. sudo nextcloud.occ config:system:get trusted_domains
  7. sudo nextcloud.occ config:system:set trusted_domains 1 --value=<dns-domain>
  8. # Set 512M as PHP memory limit
  9. sudo snap get nextcloud php.memory-limit # Should be 512M
  10. sudo snap set nextcloud php.memory-limit=512M
  11. # Set background jobs interval (e.g. checking for new emails, update RSS feeds, ...)
  12. sudo snap set nextcloud nextcloud.cron-interval=10m # Default: 15m
  • Set reverse proxy
  1. sudo snap set nextcloud ports.http=81 ports.https=444

Reference:
Nextcloud on AWS
Putting the snap behind a reverse proxy

Setup SQL Server and pyodbc

2019-03-05 10:59:13

Setup SQL Server

Connect pyodbc to SQL Server

  1. import pyodbc
  2. conn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=test;DATABASE=test;UID=user;PWD=password')
  3. cursor = conn.cursor()
  4. for row in cursor.tables():
  5. print(row.table_name)

Fix Chinese characters won't display in SSH

2019-02-24 23:29:55

Solution

Set up the same locale in both of the local laptop and remoter server:

  1. export LC_ALL=en_US.UTF-8
  2. export LANG=en_US.UTF-8

Expand the EBS root volume of EC2 Linux instance

2019-02-24 15:10:42  |  AWS
  1. Modify the EBS Volume from the console https://console.aws.amazon.com/ec2/
  2. Use the lsblk command to list the block devices attached to the instance

    1. $ lsblk
    2. NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
    3. xvda 202:0 0 30G 0 disk
    4. `-xvda1 202:1 0 8G 0 part /
    5. loop0 7:0 0 91M 1 loop /snap/core/6405
    6. loop1 7:1 0 87.9M 1 loop /snap/core/5742
    7. loop2 7:2 0 17.9M 1 loop /snap/amazon-ssm-agent/1068
    8. loop3 7:3 0 16.5M 1 loop /snap/amazon-ssm-agent/784
    9. loop4 7:4 0 18M 1 loop /snap/amazon-ssm-agent/930
  3. Use the df -h command to report the existing disk space usage on the file system

    1. $ sudo df -h /dev/xvd*
    2. Filesystem Size Used Avail Use% Mounted on
    3. udev 488M 0 488M 0% /dev
    4. /dev/xvda1 7.7G 7.4G 370M 96% /
  4. Expand the modified partition using growpart

    1. $ sudo growpart /dev/xvda 1
    2. CHANGED: partition=1 start=2048 old: size=16775135 end=16777183 new: size=62912479,end=62914

Create Free SSL/TLS Certificates using Certbot

2019-02-24 12:50:01

Install

  1. sudo add-apt-repository ppa:certbot/certbot
  2. sudo apt-get update
  3. sudo apt-get install certbot python-certbot-nginx

Setup

Running this command will get a certificate for you and have Certbot edit your Nginx configuration automatically to serve it.

  1. sudo certbot --nginx
  1. # for a specific name
  2. sudo certbot --nginx -d example.com

Renew

The Certbot packages on your system come with a cron job that will renew your certificates automatically before they expire.
You can test automatic renewal for your certificates by running this command:

  1. sudo certbot renew --dry-run

Setup Nginx on Ubuntu 14.04

2018-11-21 10:00:54
- start `nginx` service ``` sudo service apache2 stop sudo service nginx start sudo service nginx status ``` - restart `nginx` when the server is rebooted ``` sudo update-rc.d nginx defaults ``` -

Setup Shadowsocks on Ubuntu server

2015-03-02 13:35:55

Install

  1. apt-get install python-pip
  2. pip install shadowsocks

Setup

Create config file /etc/shadowsocks.json:

  1. {
  2. "server":"your_ip_address",
  3. "server_port":8388,
  4. "local_address": "127.0.0.1",
  5. "local_port":1080,
  6. "password":"your_password",
  7. "timeout":300,
  8. "method":"aes-256-cfb",
  9. "fast_open": false
  10. }

You can set multiple ports in the config file:

  1. {
  2. "server": "your_ip_address",
  3. "local_address": "127.0.0.1",
  4. "local_port": "1080",
  5. "port_password": {
  6. "8381": "password_1",
  7. "8388": "password_2"
  8. },
  9. "timeout": 300,
  10. "method": "aes-256-cfb"
  11. }

Start

  1. ssserver -c /etc/shadowsocks.json
  2. # run at background
  3. ssserver -c /etc/shadowsocks.json -d start
  4. ssserver -c /etc/shadowsocks.json -d stop

Start on boot

Edit /etc/rc.local:

  1. /usr/local/bin/ssserver -c /etc/shadowsocks.json -d start
  2. exit 0