Category: Linux

  • View systemctl service file

    View systemctl service file

    To view the content of a systemd service file, run the command

    systemctl cat SERVICE_NAME_HERE
    

    Example

    boby@sok-01:~$ systemctl cat nginx
    # /lib/systemd/system/nginx.service
    # Stop dance for nginx
    # =======================
    #
    # ExecStop sends SIGSTOP (graceful stop) to the nginx process.
    # If, after 5s (–retry QUIT/5) nginx is still running, systemd takes control
    # and sends SIGTERM (fast shutdown) to the main process.
    # After another 5s (TimeoutStopSec=5), and if nginx is alive, systemd sends
    # SIGKILL to all the remaining processes in the process group (KillMode=mixed).
    #
    # nginx signals reference doc:
    # http://nginx.org/en/docs/control.html
    #
    [Unit]
    Description=A high performance web server and a reverse proxy server
    Documentation=man:nginx(8)
    After=network.target

    [Service]
    Type=forking
    PIDFile=/run/nginx.pid
    ExecStartPre=/usr/sbin/nginx -t -q -g ‘daemon on; master_process on;’
    ExecStart=/usr/sbin/nginx -g ‘daemon on; master_process on;’
    ExecReload=/usr/sbin/nginx -g ‘daemon on; master_process on;’ -s reload
    ExecStop=-/sbin/start-stop-daemon –quiet –stop –retry QUIT/5 –pidfile /run/nginx.pid
    TimeoutStopSec=5
    KillMode=mixed

    [Install]
    WantedBy=multi-user.target
    boby@sok-01:~$

  • Redis error currently not able to persist on disk

    Redis error currently not able to persist on disk

    When doing a FLUSHALl on redis server, i get following error message

    [root@server ~]# redis-cli FLUSHALL
    (error) MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persist on disk. Commands that may modify the data set are disabled, because this instance is configured to report errors during writes if RDB snapshotting fails (stop-writes-on-bgsave-error option). Please check the Redis logs for details about the RDB error.
    [root@server ~]#

    The error was because redis data directory was not properly configured.

    To fix, the error, edit redis config file

    vi /etc/redis/redis.conf
    

    Find

    dir ./
    

    Replace with

    dir /var/lib/redis
    

    Create a directory

    mkdir /var/lib/redis
    

    Make it owned by user redis

    chown redis:redis /var/lib/redis
    

    Restart redis server

    systemctl restart redis
    

    See Redis

  • How to flush Redis cache

    How to flush Redis cache

    To delete everything from redis cache, run the command

    redis-cli FLUSHDB
    

    This will delete all keys from the current DB.

    To delete cache from all DB, run

    redis-cli FLUSHALL
    

    If Redis is on another server or needs password, you can use the following command

    redis-cli -h IP_HERE -p PORT_HERE -a PASSWORD_HERE FLUSHDB
    

    See Redis

  • How to find Redis data directory

    How to find Redis data directory

    To find the data directory in redis, run the command

    redis-cli config get dir
    

    redis data directory

    Redis data directory is specified in the configuration file

    /etc/redis/redis.conf 
    

    The entry is

    dir /var/lib/redis
    

    redis data directory should be owned by user redis

    chown redis:redis /var/lib/redis
    

    Example

    [root@server redis]# cd /var/lib/redis/
    [root@server redis]# ls -la
    total 12
    drwxrwx---   2 redis redis 4096 Dec  2 06:44 .
    drwxr-xr-x. 37 root  root  4096 Nov 20 17:30 ..
    -rw-r--r--   1 redis redis   92 Dec  2 06:44 dump.rdb
    [root@server redis]#
    

    See Redis

  • How to Install Gnome and xRDP on RHEL 8 (CentOS 8/Alma Linux/Rocky Linux)

    How to Install Gnome and xRDP on RHEL 8 (CentOS 8/Alma Linux/Rocky Linux)

    You can install Gnome Desktop + xdrp on a remote server (VPS/cloud or dedicated) and use it as a remote desktop for browsing or running applications. This instruction works for any RHEL 8 based distributions like CentOS 8, Alma Linux 8, Rocky Linux 8, and Oracle Linux 8.

    Install Gnome Desktop with

    dnf groupinstall -y --nobest Workstation
    

    After installing, reboot the server

    reboot
    

    xrdp package is provided by epel repository, so let’s enable it with

    dnf install -y epel-release
    

    install xrdp

    dnf install -y xrdp xrdp-selinux xorgxrdp
    

    Enable and start xrdp

    systemctl enable xrdp
    systemctl start xrdp
    

    Open port 3389 in the firewall

    firewall-cmd --add-port=3389/tcp --permanent
    firewall-cmd --reload
    

    Create a user and set a password for the user. This user will be used to login to remote desktop

    useradd -m --shell /bin/bash serverok
    usermod -aG wheel serverok
    passwd serverok
    

    Now you should be able to login to remote desktop using RDP.

    Once logged in, you may need to disable desktop animations to avoid lag by running the following command in the terminal.

    gsettings set org.gnome.desktop.interface enable-animations false
    
  • Find files older than X minutes

    Find files older than X minutes

    To find files that are older than X minutes, use the command

    find /path/ -type f -mmin +30

    This will find all files inside /path/ that are older than 30 minutes.

    If you need to delete those files, run

    find /path/ -type f -mmin +30 -delete

    To find files created in the last 30 minutes, run

    find /path/ -type f -mmin 30

    See find

  • Configure Failover IP in Ubuntu 20.04 OVH VPS

    Configure Failover IP in Ubuntu 20.04 OVH VPS

    To configure IP on OVH VPS, you need to manually configure networking as OVH gateway is outside the failover IP subnet. For most hosting providers getaway will be in the same subnet as the IP address. To configure IP using netplan, see instruction here.

    First, install ifupdown package

    apt install -y ifupdown
    

    Now edit file

    vi /etc/network/interfaces
    

    Add

    auto NETWORK_INTERFACE_HERE
    iface NETWORK_INTERFACE_HERE inet static
        address FO_IP_ADDR
        netmask 255.255.255.255
        broadcast FO_IP_ADDR
        dns-nameservers 8.8.8.8 8.8.4.4
        post-up ip route add GATEWAY_ADDR dev NETWORK_INTERFACE_HERE
        post-up ip route add default via GATEWAY_ADDR dev NETWORK_INTERFACE_HERE
        pre-down ip route del GATEWAY_ADDR dev NETWORK_INTERFACE_HERE
        pre-down ip route del default via GATEWAY_ADDR dev NETWORK_INTERFACE_HERE
    

    In the above text, replace

    NETWORK_INTERFACE_HERE = with your actual network interface name, for example, eth0, ens18, etc.
    FO_IP_ADDR = Your VPS IP address, OVH calls this Failover IP address. It is just any secondary IP address you purchase.
    GATEWAY_ADDR = Gateway IP address. This is the same as the Main IP of the server with the last octal replaced by 254

    Example

    Ubuntu 20.04 failover IP

    Configuration is the same as Ubuntu 18.04, the only difference is post-up and pre-down tines, where Ubuntu 18.04 used older route command, in Ubuntu 20.04, we use “ip route” command instead.

  • Bitnami WordPress Failed to update Plugin

    Bitnami WordPress Failed to update Plugin

    On a Bitnami WordPress server, plugin installation/upgrade failed with permission error. The error is because the web server in bitmai servers runs as user daemon. When you upload a file as user bitnami, you need to make sure you set file permission properly so anyone in the group can write.

    To fix the error, run

    sudo chown -R bitnami:daemon /home/bitnami/stack/wordpress
    sudo chown -R bitnami:daemon /bitnami/wordpress/wp-content
    sudo chmod -R 770 /bitnami/wordpress/wp-content
    sudo chmod -R 770 /home/bitnami/stack/wordpress
    

    Some bitnami installations use different file structure

    sudo chown -R bitnami:daemon /home/bitnami/apps/wordpress/htdocs
    sudo chmod -R 770 /home/bitnami/apps/wordpress/htdocs
    

    in wp-config.php, added the following when I migrated a site, refer to original wp-config.php for your exact path

    define('FS_METHOD', 'direct');
    define('WP_TEMP_DIR', '/opt/bitnami/apps/wordpress/tmp');
    
  • Install latest Redis on Ubuntu/Debian Server

    Install latest Redis on Ubuntu/Debian Server

    To install the latest version of Redis on Ubuntu/Debian server, run

    curl https://packages.redis.io/gpg | sudo apt-key add -
    echo "deb https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list
    sudo apt-get update
    sudo apt-get install redis
    

    Enable redis server to start on boot

    systemctl enable redis-server
    

    Start redis server

    systemctl start redis-server
    

    See Redis

  • How to Install PHP 5.6 on Oracle Linux 7

    How to Install PHP 5.6 on Oracle Linux 7

    Oracle Linux is a free RHEL based Linux distro. We will install PHP 5.6 on an Oracle Linux 7 server.

    First, enable EPEL repo

    yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
    

    Enable Remi repo

    yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
    

    Install yum-utils package

    yum install yum-utils
    

    Enable remi-php56

    yum-config-manager --enable remi-php56
    

    Install PHP 5.6 with

    yum install -y php php-bcmath php-cli php-common php-devel php-gd \
        php-imap php-intl php-json php-ldap php-lz4 php-mbstring php-mysqlnd \
        php-soap php-intl php-opcache php-xml php-pdo
    

    This will only install the CLI version of PHP 5.6. To make Apache work with PHP 5.6, install

    yum install php-pecl-http.x86_64
    

    This will create file /etc/httpd/conf.d/php.conf, that enable PHP module in Apache.

    Restart apache webserver

    systemctl restart httpd
    

    Now Apache will be able to serve PHP files.

  • Install Jenkins on Ubuntu

    Install Jenkins on Ubuntu

    Jenkins is an open source automation server. It helps automate the parts of software development related to building, testing, and deploying, facilitating continuous integration and continuous delivery.

    Jenkins is a Java Application. So you need to install JRE (Java Runtime Environment). Install it with

    sudo apt-get install default-jre
    

    You can find instructions for installing Jenkins on Debian/Ubuntu at

    https://pkg.jenkins.io/debian-stable/

    Install Jenkins

    wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
    sudo sh -c 'echo deb https://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
    sudo apt-get update
    sudo apt-get install jenkins
    

    Enable Jenkins to start on boot

    sudo systemctl enable jenkins
    

    Start Jenkins

    sudo systemctl start jenkins
    

    To check status

    sudo systemctl status jenkins
    

    You can verify Jenkins running on port 8080 with command

    root@jenkins:~# netstat -lntp
    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
    tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      186/systemd-resolve 
    tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      256/sshd: /usr/sbin 
    tcp6       0      0 :::22                   :::*                    LISTEN      256/sshd: /usr/sbin 
    tcp6       0      0 :::8080                 :::*                    LISTEN      4774/java           
    root@jenkins:~#
    

    To find the initial password, run the command

    cat /var/lib/jenkins/secrets/initialAdminPassword