Category: Programming

  • How to install PHP from Source on Ubuntu

    How to install PHP from Source on Ubuntu

    Compiling PHP from source code can be essential when you require a specific PHP version that isn’t available through your Ubuntu distribution or a Personal Package Archive (PPA). This approach is particularly useful if you need to enable custom modules, such as pcntl, which is disabled by default on Ubuntu. By compiling PHP yourself, you gain the flexibility to customize your PHP environment to meet your exact requirements.

    Install Requirements

    Installing PHP from source code requires many packages and libraries installed. You can install it with the command

    apt-get install -y build-essential autoconf automake pkgconf re2c libbz2-dev libcurl4-openssl-dev libxml2-dev libpng-dev libjpeg-dev libssl-dev libzip-dev libmcrypt-dev libreadline-dev libonig-dev libsodium-dev bison libsqlite3-dev libfreetype-dev libsystemd-dev libwebp-dev libxslt1-dev libkrb5-dev libldap2-dev

    Download and extract PHP source code

    cd /usr/local/src/
    wget https://www.php.net/distributions/php-8.3.8.tar.gz
    tar -xzvf php-8.3.8.tar.gz
    cd php-8.3.8

    Configure PHP

    This step allows you to enable or disable PHP modules.

    ./configure \
    	--with-pic \
    	--enable-embed \
    	--prefix=/usr/serverok/php \
    	--with-config-file-scan-dir=/usr/serverok/php/lib/php.conf.d \
    	--enable-pcntl \
    	--enable-fpm \
    	--with-fpm-systemd \
    	--with-curl \
    	--enable-gd \
    	--with-gettext \
    	--with-jpeg \
    	--with-freetype \
    	--with-kerberos \
    	--with-openssl \
    	--with-mhash \
    	--with-mysql-sock=/var/lib/mysql/mysql.sock \
    	--with-mysqli=mysqlnd \
    	--with-pdo-mysql=mysqlnd \
    	--with-pear \
    	--with-sodium \
    	--with-webp \
    	--with-xsl \
    	--with-zlib \
    	--with-zip \
    	--enable-bcmath \
    	--enable-calendar \
    	--enable-exif \
    	--enable-ftp \
    	--enable-sockets \
    	--enable-soap \
    	--enable-mbstring \
    	--enable-intl \
    	--with-ldap

    Compile and Install PHP

    make
    make install
    cp ./php.ini-production /usr/serverok/php/lib/php.ini
    cp /usr/serverok/php/etc/php-fpm.conf.default  /usr/serverok/php/etc/php-fpm.conf

    Create php-fpm pool file

    Create file

    vi /usr/serverok/php/etc/php-fpm.d/www.conf

    Add following content

    [www]
    user = www-data
    group = www-data
    listen = /run/php/php-fpm.sock
    listen.owner = www-data
    listen.group = www-data
    pm = dynamic
    pm.max_children = 5
    pm.start_servers = 2
    pm.min_spare_servers = 1
    pm.max_spare_servers = 3
    catch_workers_output = yes
    php_flag[display_errors] = on
    php_admin_value[error_log] = /var/log/fpm-php.log
    php_admin_flag[log_errors] = on

    Start php-fpm on boot

    Create a service file

    vi /etc/systemd/system/php-fpm.service

    Add following content

    [Unit]
    Description=PHP FastCGI Process Manager (ServerOK)
    After=network.target
    
    [Service]
    Type=forking
    PIDFile=/run/php/php-fpm.pid
    ExecStart=/usr/serverok/php/sbin/php-fpm --daemonize --pid /run/php/php-fpm.pid
    ExecReload=/bin/kill -USR2 $MAINPID
    ExecStop=/bin/kill -QUIT $MAINPID
    PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target

    Start php-fpm service

    systemctl daemon-reload
    systemctl enable php-fpm
    systemctl start php-fpm
    systemctl status php-fpm

    Install PHP modules from PECL

    To install PHP modules from PECL, run

    apt install -y libmagick++-dev
    /usr/serverok/php/bin/pecl install APCu
    /usr/serverok/php/bin/pecl install redis
    /usr/serverok/php/bin/pecl install imagick

    To enable the PHP modules, edit

    vi /usr/serverok/php/lib/php.ini

    add

    extension=imagick.so
    extension=apcu.so
    extension=redis.so

    Restart php-fpm service

    systemctl resart php-fpm

    Enable php-fpm for your website

    To enable php-fpm for Apache, add the following code in the Apache VirtualHost entry.

    <FilesMatch \.php$>
    	SetHandler proxy:unix:/run/php/php-fpm.sock|fcgi://127.0.0.1
    </FilesMatch>

    For nginx, use socket

    /run/php/php-fpm.sock

    Back to PHP

  • Install Yarn Package Manager

    Install Yarn Package Manager

    Yarn is a package manager for JavaScript like npm. It is commonly used for managing dependencies for Node.js applications, but it can also be used for front-end development with frameworks like Rmeact, Angular, and Vue.js.

    To install the Yarn package manager on ubuntu, run:

    curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | sudo tee /usr/share/keyrings/yarnkey.gpg >/dev/null
    echo "deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/debian stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
    sudo apt-get update && sudo apt-get install yarn
    

    Back to Node.js

  • How to get Contents of a Webpage using PHP curl

    How to get Contents of a Webpage using PHP curl

    PHP cURL module allows you to connect to remote websites and API endpoints to exchange data. To see if you have php-curl enabled, you can check phpinfo() page or run the command “php -m’ and look for curl.

    boby@sok-01:~$ php -m | grep curl
    curl
    boby@sok-01:~$ 
    

    If php-curl module is not installed, you can install it on Ubuntu/Debian with the command

    apt-get install php-curl
    

    To access the content of a remote web server or API endpoint, you can use the following PHP code.

    
    

    To make script work with invalid or self signed SSL, add following

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    

    If you want to make a POST request, you can use the following code

     'Yujin Boby',
        'email' => '[email protected]'
    );
    
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 11.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3');
    $response = curl_exec($ch);
    curl_close($ch);
    

    Back to PHP

  • gridsome error:03000086:digital envelope routines::initialization error

    gridsome error:03000086:digital envelope routines::initialization error

    When starting a gridsome project on Ubuntu 20.04 with Node.js version v18.10.0, it failed with the error message “error:03000086:digital envelope routines::initialization error”.

    boby@sok-01:~/www/learn/gridsome/my-gridsome-site$ gridsome develop
    Gridsome v0.7.23
    
    Initializing plugins...
    Load sources - 0s
    Create GraphQL schema - 0.03s
    Create pages and templates - 0.03s
    Generate temporary code - 0.03s
    Bootstrap finish - 0.56s
    10% building 1/1 modules 0 activeError: error:0308010C:digital envelope routines::unsupported
        at new Hash (node:internal/crypto/hash:71:19)
        at Object.createHash (node:crypto:133:10)
        at module.exports (/mnt/data/sites/learn/gridsome/my-gridsome-site/node_modules/webpack/lib/util/createHash.js:135:53)
        at NormalModule._initBuildHash (/mnt/data/sites/learn/gridsome/my-gridsome-site/node_modules/webpack/lib/NormalModule.js:417:16)
        at handleParseError (/mnt/data/sites/learn/gridsome/my-gridsome-site/node_modules/webpack/lib/NormalModule.js:471:10)
        at /mnt/data/sites/learn/gridsome/my-gridsome-site/node_modules/webpack/lib/NormalModule.js:503:5
        at /mnt/data/sites/learn/gridsome/my-gridsome-site/node_modules/webpack/lib/NormalModule.js:358:12
        at /mnt/data/sites/learn/gridsome/my-gridsome-site/node_modules/loader-runner/lib/LoaderRunner.js:373:3
        at iterateNormalLoaders (/mnt/data/sites/learn/gridsome/my-gridsome-site/node_modules/loader-runner/lib/LoaderRunner.js:214:10)
        at iterateNormalLoaders (/mnt/data/sites/learn/gridsome/my-gridsome-site/node_modules/loader-runner/lib/LoaderRunner.js:221:10)
        at /mnt/data/sites/learn/gridsome/my-gridsome-site/node_modules/loader-runner/lib/LoaderRunner.js:236:3
        at runSyncOrAsync (/mnt/data/sites/learn/gridsome/my-gridsome-site/node_modules/loader-runner/lib/LoaderRunner.js:130:11)
        at iterateNormalLoaders (/mnt/data/sites/learn/gridsome/my-gridsome-site/node_modules/loader-runner/lib/LoaderRunner.js:232:2)
        at Array. (/mnt/data/sites/learn/gridsome/my-gridsome-site/node_modules/loader-runner/lib/LoaderRunner.js:205:4)
        at Storage.finished (/mnt/data/sites/learn/gridsome/my-gridsome-site/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:55:16)
        at /mnt/data/sites/learn/gridsome/my-gridsome-site/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:91:9
    10% building 1/3 modules 2 active /mnt/data/sites/learn/gridsome/my-gridsome-site/node_modules/webpack/hot/dev-server.jsnode:internal/crypto/hash:71
      this[kHandle] = new _Hash(algorithm, xofLen);
                      ^
    
    Error: error:0308010C:digital envelope routines::unsupported
        at new Hash (node:internal/crypto/hash:71:19)
        at Object.createHash (node:crypto:133:10)
        at module.exports (/mnt/data/sites/learn/gridsome/my-gridsome-site/node_modules/webpack/lib/util/createHash.js:135:53)
        at NormalModule._initBuildHash (/mnt/data/sites/learn/gridsome/my-gridsome-site/node_modules/webpack/lib/NormalModule.js:417:16)
        at handleParseError (/mnt/data/sites/learn/gridsome/my-gridsome-site/node_modules/webpack/lib/NormalModule.js:471:10)
        at /mnt/data/sites/learn/gridsome/my-gridsome-site/node_modules/webpack/lib/NormalModule.js:503:5
        at /mnt/data/sites/learn/gridsome/my-gridsome-site/node_modules/webpack/lib/NormalModule.js:358:12
        at /mnt/data/sites/learn/gridsome/my-gridsome-site/node_modules/loader-runner/lib/LoaderRunner.js:373:3
        at iterateNormalLoaders (/mnt/data/sites/learn/gridsome/my-gridsome-site/node_modules/loader-runner/lib/LoaderRunner.js:214:10)
        at Array. (/mnt/data/sites/learn/gridsome/my-gridsome-site/node_modules/loader-runner/lib/LoaderRunner.js:205:4)
        at Storage.finished (/mnt/data/sites/learn/gridsome/my-gridsome-site/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:55:16)
        at /mnt/data/sites/learn/gridsome/my-gridsome-site/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:91:9
        at /mnt/data/sites/learn/gridsome/my-gridsome-site/node_modules/graceful-fs/graceful-fs.js:123:16
        at FSReqCallback.readFileAfterClose [as oncomplete] (node:internal/fs/read_file_context:68:3) {
      opensslErrorStack: [ 'error:03000086:digital envelope routines::initialization error' ],
      library: 'digital envelope routines',
      reason: 'unsupported',
      code: 'ERR_OSSL_EVP_UNSUPPORTED'
    }
    
    Node.js v18.10.0
    boby@sok-01:~/www/learn/gridsome/my-gridsome-site$ 
    

    To fix the error, use the command

    NODE_OPTIONS='--openssl-legacy-provider' gridsome develop
    

    To make it permanent, you can edit the package.json file in the root directory of your gridsome project

    Find

        "develop": "gridsome develop",
    

    Replace with

        "develop": "NODE_OPTIONS='--openssl-legacy-provider' gridsome develop",
    

    Now use

    yarn develop
    

    Or

    npm run develop
    

    Instead of the “gridsome develop” command.

    Back to Node.js

  • How to change Next.js port

    How to change Next.js port

    You can start Next.js development server with the command

    npm run dev
    

    This will start the development server on port 3000.

    boby@sok-01:/www/learn/nextjs/todo (main)$ npm run dev
    
    > dev
    > next dev
    
    ready - started server on 0.0.0.0:3000, url: http://localhost:3000
    event - compiled client and server successfully in 1645 ms (163 modules)
    wait  - compiling...
    event - compiled client and server successfully in 59 ms (163 modules)
    wait  - compiling / (client and server)...
    event - compiled client and server successfully in 900 ms (183 modules)
    

    If port 3000 is already used on your system, next.js will try to use another available port. If you want to assign a fixed port for your Next.js application, you can specify a port.

    Next.js change port

    To change the port, edit file package.json

    In the file, find

      "scripts": {
        "dev": "next dev",
        "build": "next build",
        "start": "next start"
      },
    

    Replace with

      "scripts": {
        "dev": "next dev -p 8001",
        "build": "next build",
        "start": "next start"
      },
    

    This will change the next.js application port to 8001.

    If you only want to change one time, you can specify the port when you run “npm run dev” as follows.

    PORT=8081 npm run dev
    

    This will run the next.js application on port 8081.

    Another way to do the same is

    npm run  dev -- --port 8081
    

    Example.

    boby@sok-01:/www/learn/nextjs/todo (main)$ npm run  dev -- --port 8081
    
    > dev
    > next dev --port 8081
    
    ready - started server on 0.0.0.0:8081, url: http://localhost:8081
    event - compiled client and server successfully in 436 ms (163 modules)
    wait  - compiling...
    event - compiled client and server successfully in 639 ms (163 modules)
    

    See npm

  • How to Install Yarn Package Manager on Ubuntu

    How to Install Yarn Package Manager on Ubuntu

    Yarn is a javascript package manager like npm.

    Import the GPG key

    curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | sudo tee /usr/share/keyrings/yarnkey.gpg >/dev/null
    

    Add the repository

    echo "deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/debian stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
    

    Instal yrarn with apt

    sudo apt-get update && sudo apt-get install yarn
    

    See Node.js

  • Laravel supported ciphers are AES-128-CBC and AES-256-CBC

    Laravel supported ciphers are AES-128-CBC and AES-256-CBC

    On a Laravel application, I get the following error message

    The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths.
    
    Laravel error

    To fix the error, run the command

    php artisan key:generate

    This will generate a new key and add to the .env file.

    Back to Laravel

  • How to change URL of a Drupal site

    How to change URL of a Drupal site

    To change the URL of a Drupal website, edit the file

    sites/default/settings.php
    

    Find the line

    $base_url = 'https://www.your-current-domain';  // NO trailing slash!
    

    Replace your old domain name with your new domain name.

  • How to create .NET console program

    How to create .NET console program

    Once you have dotnet SDK installed, you can create your first program.

    To create a console project run

    dotnet new console -o MyApp
    

    create dotnet console project

    This will create MyApp folder with 2 files inside. Program.cs is a basic program that prints “Hello World” text in the console.

    dotnet application directory structure

    To run the dotnet project, you can use the command

    dotnet run
    

    running dotnet application

    Back to dotnet

  • How To Install ionCube on Ubuntu 20.04

    How To Install ionCube on Ubuntu 20.04

    ionCube is PHP extension that is used to load ionCube encoded PHP files. ionCube is used to protect commercial PHP scripts by encoding it, so no one can read the actual PHP code.

    First, download ioncube loader from

    https://www.ioncube.com/loaders.php

    cd /usr/local/src
    wget https://downloads.ioncube.com/loader_downloads/ioncube_loaders_lin_x86-64.tar.gz
    tar xvf ioncube_loaders_lin_x86-64.tar.gz
    cd ioncube/
    

    Find extension directory and copy .so file to PHP extension directory. To find PHP extension directory run

    php -i | grep  extension_dir
    

    On my installation, i have PHP extension_dir set to /usr/lib/php/20190902. If it’s different for you, change the path in the following command.

    For PHP 7.4, run

    cp /usr/local/src/ioncube/ioncube_loader_lin_7.4.so /usr/lib/php/20190902
    

    Enable ioncube

    echo "zend_extension=ioncube_loader_lin_7.4.so" > /etc/php/7.4/mods-available/ioncube.ini
    ln -s /etc/php/7.4/mods-available/ioncube.ini /etc/php/7.4/cli/conf.d/01-ioncube.ini
    

    for Apache, run

    ln -s /etc/php/7.4/mods-available/ioncube.ini /etc/php/7.4/apache2/conf.d/01-ioncube.ini
    systemctl restart apache2
    

    for php-fpm, run

    ln -s /etc/php/7.4/mods-available/ioncube.ini /etc/php/7.4/fpm/conf.d/01-ioncube.ini
    systemctl restart php7.4-fpm
    
  • How to install PHP PECL Extension

    How to install PHP PECL Extension

    PECL is a repository for PHP extensions. To install PHP extensions from PCEL, you can use the command

    pcel install EXTENSTION_NAME_HERE
    

    To install a specific version, you can specify version after extension name as follows.

    pecl install pecl_http-3.2.3
    

    Once an extension is installed, you need to edit PHP configuration and add a line to load the PHP module.

    extension=EXTENSTION_NAME_HERE.so
    

    Back to PECL

  • Monitoring APCu with APCu Panel

    Monitoring APCu with APCu Panel

    apcu-panel provide GUI to monitor APCu cache. To install apcu-panel on CentOS, run

    yum install apcu-panel
    

    The package provides following files

    [root@localhost ~]# rpm -q --filesbypkg apcu-panel
    apcu-panel                /etc/apcu-panel
    apcu-panel                /etc/apcu-panel/conf.php
    apcu-panel                /etc/httpd/conf.d/apcu-panel.conf
    apcu-panel                /usr/share/apcu-panel
    apcu-panel                /usr/share/apcu-panel/index.php
    [root@localhost ~]# 
    

    To enable GUI, edit file

    vi /etc/httpd/conf.d/apcu-panel.conf
    

    Modify it as follows

    # APC Control Panel
    Alias /apcu-panel /usr/share/apcu-panel
    
    
       
         Require all granted
       
    
    

    Restart Apache

    systemctl restart httpd
    

    Now you should be able to access APCu panel at

    http://your-server-ip/apcu-panel/