Category: Programming

  • Upgrade ruby gem

    To upgrade a ruby gem run

    gem update --system VERSION_HERE
    

    Example

    gem update --system 1.3.7
    

    See Ruby

  • How to Install Visual Studio Code Server

    How to Install Visual Studio Code Server

    Code Server allow you to run Microsoft Visual Studio Code in the browser. Code Server runs on a remote web server, you can access it by going to special url. This allow business to move development environment to cloud, deploy new developer environment on cloud or on premise server quickly with automation tools. Developers can access their secure work environment from any device that have a web browser and internet connection. This is helpful when employees work remotely so all code/files stay on the cloud instead of developers local computer.

    https://github.com/cdr/code-server

    Install Code Server

    You can download latest version of Code Server from

    https://github.com/cdr/code-server/releases

    To install Code Server 3.5.0, run

    cd /usr/local/src
    wget https://github.com/cdr/code-server/releases/download/v3.5.0/code-server-3.5.0-linux-amd64.tar.gz
    tar xvf code-server-3.5.0-linux-amd64.tar.gz
    mv code-server-3.5.0-linux-amd64 /usr/local
    

    To start code server, run

    /usr/local/code-server-3.5.0-linux-amd64/bin/code-server --bind-addr 0.0.0.0:10090
    

    Visual Studio Code Server

    This will make code server listen to port 10090 on all IP address configured on the server.

    You can access the code server at

    http://SERVER_IP_HERE:10090
    

    You can find password to login in file ~/.config/code-server/config.yaml

    Visual Studio Code Server

    If you don’t want to auto start code server on server boot, you can run the command in tmux or screen.

    Enable SSL for Code Server

    You can get Free SSL certficate using LetsEncrypt.

    Once you got SSL certficate for your domain, you need to do the following. This this example, i use lab.serverok.in domain and user “boby”.

    Copy SSL to users directory, so code server can access it.

    mkdir -p /home/boby/.config/code-server
    cp /etc/letsencrypt/live/lab.serverok.in/fullchain.pem  /etc/letsencrypt/live/lab.serverok.in/privkey.pem /home/boby/.config/code-server
    chown -R boby:boby /home/boby/.config
    

    Create Systemd service file to autostart code server

    create file

    vi /lib/systemd/system/code-server.service
    

    Add

    [Unit]
    Description=code-server
    
    [Service]
    Type=simple
    ExecStart=/usr/local/code-server-3.5.0-linux-amd64/bin/code-server --bind-addr 0.0.0.0:10090 --cert /home/boby/.config/code-server/fullchain.pem  --cert-key  /home/boby/.config/code-server/privkey.pem
    Restart=always
    User=boby
    Group=boby
    WorkingDirectory=/home/boby/
    
    [Install]
    WantedBy=multi-user.target
    

    Start Code Server

    To start code server, run a user root

    systemctl start code-server
    

    To get Password to login to code server, run

    root@lab:~# cat /home/boby/.config/code-server/config.yaml 
    bind-addr: 127.0.0.1:8080
    auth: password
    password: dd39e18a466d89b6579169ae
    cert: false
    root@lab:~# 
    

    If you want to change password, you can edit the file and restart code server with

    systemctl restart code-server
    

    Update SSL

    LetsEncrypt SSL expire every 90 days and renew the SSL certificate every month. We will copy over the SSL certficate using a cronjob and restart code server every week.

    create file

    vi /usr/local/bin/code-server-update-ssl
    

    Add following

    #!/bin/bash
    
    cp /etc/letsencrypt/live/lab.serverok.in/fullchain.pem  /etc/letsencrypt/live/lab.serverok.in/privkey.pem /home/boby/.config/code-server
    chown boby:boby /home/boby/.config/code-server/fullchain.pem
    chown boby:boby /home/boby/.config/code-server/privkey.pem
    systemctl restart code-server
    

    Make it executable

    chmod 755 /usr/local/bin/code-server-update-ssl
    

    Create cron job

    crontab -e
    

    Add

    @weekly /usr/local/bin/code-server-update-ssl > /dev/null 2>&1
    
  • HTTP 301 Redirect using PHP

    HTTP 301 Redirect using PHP

    You can use the PHP header() function to do redirect.

    To do 301 redirect, use

    header('Location: https://new-url/', true, 301);
    exit;

    For 302 redirect, use

    header('Location: https://new-url/');
    exit;

    If you need to redirect a site to a new site while keeping the same URL structure, you can use

    $newUrl = "https://NEW-URL-HERE" . $_SERVER['REQUEST_URI'];
    header("HTTP/1.1 301 Moved Permanently");
    Header("Location: $newUrl");
    exit;

    Back to redirect

  • PHP PECL extension

    PHP PECL extension

    PECL is a repository for PHP Extensions, providing a directory of all known extensions and hosting facilities for downloading and development of PHP extensions.

    https://pecl.php.net/packages.php

    To install a PECL extension, run

    pecl install pecl_http
    

    To install a specific version, run

    pecl install pecl_http-3.2.3
    
  • Redirect a site to HTTPS using PHP

    This PHP script will redirect website visitors to HTTPS (SSL) URL. You can add this in your index.php of the website

    if (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on" ) {
        header("HTTP/1.1 301 Moved Permanently");
        $newUrl = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        Header("Location: $newUrl");
        exit;
    }

    To redirect visitors to a new URL using PHP, use the following PHP code

    $newUrl = "https://NEW-URL-HERE" . $_SERVER['REQUEST_URI'];
    header("HTTP/1.1 301 Moved Permanently");
    Header("Location: $newUrl");
    exit;

    You can also use Apache mod_rewrite .htacess to do the redirection.

  • Gem – Package Manager for Ruby

    gem is package manager for Ruby.

    To get list of all install packages, run

    gem list
    

    To install a package, run

    gem install PACKAGE_NAME
    

    To uninstall a package, run

    gem uninstall PACKAGE_NAME
    

    To search for a package, run

    gem search PACKAGE_NAME
    

    gem install error libxslt is missing

    See Ruby

  • GoLang Hello World

    Create a file 1.go with following content

    package main
    
    import "fmt"
    
    func main() {
    	fmt.Println("My First GoLang Program")
    }
    

    To run the program, use command

    go run 1.go
    

  • Installing golang on Ubuntu 18.04

    To install golang on Ubuntu 18.04, run

    apt install golang -y
    
  • PHP running system commands with exec

    To execure a system command on a server, use PHP exec function.

    For example

    &1", $result);
    echo "
    ";
    print_r($result);
    

    See php