[작성자:] Saturn

  • Install Nginx web server (ubuntu)

    Install Nginx web server (ubuntu)

    1. Nginx web server Vs Apache web server

    Here we proceed with installing the Nginx web server. Web servers installed on Linux are divided into Nginx web servers and Apache web servers, and have the following differences. Apache is an open source HTTP server, while Nginx is an open source, high-performance asynchronous web server and reverse proxy server.

    Original Korean article: Install Nginx web server (ubuntu)

    While the development and evolution of Apache HTTP Server is managed and maintained by a worldwide user community (Apache Software Foundation), Nginx is maintained and maintained by the company of the same name, founded in 2011.

    While Apache provides various multiprocessing modules to handle client requests and web traffic, Nginx is designed to handle multiple client requests simultaneously with minimal hardware resources.

    In Apache, a single thread is associated with one connection, but in Nginx, a single thread can handle multiple connections. This process consumes less memory and improves performance.

    While Apache HTTP Server has a non-scalable multi-threaded architecture, Nginx follows an asynchronous event-based approach for handling multiple client requests.

    The Apache server serves static content using traditional methods and handles dynamic content natively within the web server itself. Nginx, on the other hand, cannot handle dynamic content internally and relies on external processes to do so.

    2. Install Nginx web server

    Update package information with apt update and apt upgrade. Check packages related to nginx with the list option. Install the nginx web server using the nginx installation command.

    • This command displays nginx-related package information.
    sudo apt list nginx*
    nginx ubuntu
    nginx ubuntu
    • Run the command to install nginx.
    sudo apt install nginx
    nginx ubuntu
    nginx ubuntu
    • Check the version of nginx installed.
    sudo nginx -v
    nginx ubuntu
    nginx ubuntu
    • Check the status of the nginx service.
    sudo systemctl status nginx
    nginx ubuntu
    nginx ubuntu

    2. Port settings for external connection

    Even though the web server is installed, it does not mean that you can connect to it right away. The environment where Ubuntu is installed is composed of a virtual server on the host computer, and the Ubuntu server is provided with a firewall service using iptables.

    The first step is to open a port on your ubuntu server. If you need to understand iptables, you can check it out through the following article.

    Ubuntu firewall iptables configuration and management – ​​Thinknote

    1) Allow nginx http port

    The http port uses 80. Open iptables to use TCP port 80 and print out the rules to see if the chain is registered in iptables.

    • -A : Add to chain
    • -p tcp : protocol tcp
    • -m tcp: tcp match, extended match
    • –dport: Port number
    • — j : Jump to target
    • ACCEPT: Allow
    sudo iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
    sudo iptables -S
    iptables
    iptables

    Save the added rules and reload.

    sudo netfilter-persistent save
    sudo netfilter-persistent reload
    iptables
    iptables

    2) Add VirtualBox port

    You must add a port to VirtualBox to be able to connect from the host computer’s IP or localhost (127.0.0.1). And if you are using a router, you can connect from outside by using the router’s DMZ function or the router’s port forwarding function. This does not cover the router’s DMZ settings.

    If you want to know the detailed procedures of VirtualBox, you can click the link below.

    Ubuntu SSH port settings and connection – Thinknote

    Add forwarding to port 80 as shown in the image below.

    virtualbox 80 port
    virtualbox 80 port

    3. Check Nginx web server connection

    Once the port opening is complete, connect to the nginx web server on the Ubuntu server through the host computer’s Internet browser.

    • Connect to localhost (127.0.0.1)
    nginx
    nginx
    nginx
    nginx
    • Connect to the internal IP of the host computer
    nginx
    nginx
    • Connect to external IP
    nginx
    nginx

    Good article to read together

    • Install Nginx web server (Centos 8)
    • Install and enable Ubuntu firewall iptables
    • Ubuntu firewall iptables setup and management
    • Install PHP 8 (ubuntu)
    • Setting up Nginx + Php8

    Related Reading

    Continue with these related Thinknote English articles in the Server & Infrastructure cluster.

    FAQ

    What is this article about?

    This article is part of Thinknote’s English server and infrastructure archive. It focuses on practical Linux, Ubuntu, web-server, database, SSH, firewall, or hosting operations that readers can adapt to their own environment.

    How should I use this guide?

    Use it as a practical checklist and concept guide. Before applying commands on a live server, verify package names, OS versions, ports, and backup requirements for your own setup.

    Where can I read the original Korean article?

    The original Korean article is available here: Original Korean article.

  • Setting up and connecting Ubuntu SSH ports

    Setting up and connecting Ubuntu SSH ports

    1. Open Ubuntu SSH port

    To open the SSH port, you need to do two things. First, you need to open port 22 to Ubuntu iptables, and second, you need to forward the port of virtualbox. If you installed Ubuntu using an external IP, you can just do the first step. If the host PC is connected to the router, you must connect port 22 to the host PC through DMZ settings and port forwarding to be able to access it from outside.

    Original Korean article: Setting up and connecting Ubuntu SSH ports

    1) Basic environment configuration

    The content starting from now on assumes the following environment.

    • Ubuntu is installed as virtual
    • The host PC is assigned an IP from the internal router.
    • Virtual IP: 10.0.2.15

    2) Allow Iptables SSH port

    SSH port uses 22. Open IPtables to use TCP port 22.

    • -A : Add to chain
    • -p tcp : protocol tcp
    • -m tcp: tcp match, extended match
    • –dport: Port number
    • — j : Jump to target
    • ACCEPT: Allow
    sudo iptables -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT

    Prints rules to see if the chain is registered in iptables. Save the added rules and reload.

    sudo iptables -S
    sudo netfilter-persistent save
    sudo netfilter-persistent reload
    Article image 1
    Article image 1

    Let’s check the IP of ubuntu. When you check the IP using the ip addr command, the following output appears and it is confirmed that Ubuntu’s IP is 10.0.2.15.

    ip addr
    Article image 2
    Article image 2

    2. SSH port forwarding in VirtualBox

    Opening Ubuntu’s SSH port allows Ubuntu to connect, but connection is not yet possible from the host computer. To connect to Ubuntu using MobaXterm on the host computer, you need to forward the port in VirtualBox.

    In VirtualBox, you can enable port foreclosure in the settings without shutting down the virtual machine. Click Network in Virtual PC Settings.

    virtualbox ssh
    virtualbox ssh
    virtualbox ssh
    virtualbox ssh

    Expand Network Advanced and click the Port Forwarding button.

    virtualbox ssh
    virtualbox ssh

    Click the plus icon at the top right to add a port forwarding rule. Enter the name [SSH], protocol [TCP], host IP (you can leave it blank because you are connecting as a local host) [ ], host port [22], guest IP (ubuntu IP can be checked with the ip addr command) [10.0.2.15], and guest port [22].

    virtualbox ssh
    virtualbox ssh

    3. Access Ubuntu terminal using MobaXterm

    Run MobaXterm. Since there are no registered sessions yet, click Session at the top left to register a new session.

    mobaxterm ssh
    mobaxterm ssh

    To connect to Ubuntu using SSH, click SSH in the upper left corner.

    mobaxterm ssh
    mobaxterm ssh

    Remnote Host refers to the IP of the host PC, not the ubuntu IP. The Host IP can be checked by running the terminal on the host PC using the cmd command and using the ipconfig /all command. Since we are connecting using localhost IP here, we will enter 127.0.0.1. Click the OK button to close the window.

    mobaxterm ssh
    mobaxterm ssh

    You can see that the 127.0.0.1 session has been added to the user session section on the left side of MobaXterm. If you double-click the session and the login terminal appears, the port is normally open.

    mobaxterm ssh
    mobaxterm ssh

    Good article to read together

    • Install Ubuntu OpenSSH and install MobaXterm
    • VirtualBox Virtual Box virtual machine download and installation
    • Download Ubuntu and set up Ubuntu (virtualbox)
    • Install Ubuntu 22.04.1 (VirtualBox)
    • firewall settings

    Related Reading

    Continue with these related Thinknote English articles in the Server & Infrastructure cluster.

    FAQ

    What is this article about?

    This article is part of Thinknote’s English server and infrastructure archive. It focuses on practical Linux, Ubuntu, web-server, database, SSH, firewall, or hosting operations that readers can adapt to their own environment.

    How should I use this guide?

    Use it as a practical checklist and concept guide. Before applying commands on a live server, verify package names, OS versions, ports, and backup requirements for your own setup.

    Where can I read the original Korean article?

    The original Korean article is available here: Original Korean article.

  • Install PHP 8 (ubuntu)

    Install PHP 8 (ubuntu)

    1. Understanding and features of PHP

    1) Understanding PHP

    The purpose of installing PHP 8 is to use WordPress in conjunction with the Nginx web server. In the future, we will also cover how to use PHP to manipulate some data and insert PHP code, but rather, we will explain it for the purpose of operating WordPress developed with PHP.

    Original Korean article: Install PHP 8 (ubuntu)

    PHP stands for Hypertext Preprocessor and is designed to implement dynamic web pages. You can create the desired web page by processing code written in PHP like an HTML file in the PHP engine. PHP is moving to version 8.x, and after 7.0, PHP code and HTML are written separately as separate files and are increasingly executed through php-fpm (PHP FastCGI Process Manager) rather than a web server.

    Server-side open source software is often implemented in PHP. Representative programs based on PHP include WordPress, MediaWiki, and NextCloud. PHP has strengths in text and HTML processing, so it can apply a variety of things such as URL parsing, form processing, and regular expressions, and supports various databases.

    Various programming languages ​​such as Java and Python are widely used, but continuous development is underway based on the influence of open source.

    2) PHP features

    PHP has four characteristics: simplicity, efficiency, security, flexibility, and familiarity.

    • PHP can perform system functions i.e. create, open, read, write and close files on the system.
    • PHP can collect data from forms, i.e. files, save data to files, send data via email, and return data to the user.
    • Add, delete and modify elements in the database via PHP.
    • Access cookie variables and set cookies.
    • PHP allows you to restrict users from accessing some pages on your website.
    • You can encrypt your data.

    3) Utilization of PHP

    Tasks that would have required multiple Includes to be accessed in Java or C language are built-in functions, so they can be easily implemented with a small amount of code. Current PHP has evolved from a procedural form to a state where ‘object-oriented (class)’ programs can be written.

    • PHP can generate dynamic page content.
    • PHP can create, open, read, write, delete and close files on the server.
    • PHP can collect form data.
    • PHP can send and receive cookies.
    • PHP can add, delete and modify data in the database.
    • You can control user access using PHP.
    • PHP can encrypt data

    2. Install PHP 8

    1) Preparation for PHP 8

    Update and upgrade Ubuntu packages.

    sudo apt update
    sudo apt upgrade
    php 8
    php 8

    2) Check and install PHP 8 package

    You can check items related to PHP with the apt list command. However, because there are so many PHP-related packages, I am hesitant about what to install. In particular, since PHP versions vary, a specific version may be required for the development environment of existing programs.

    The core of the PHP installation is php-fpm. Depending on the PHP version, it exists for each version such as php7.x-fpm, php8.x-fpm, etc. We will cover how to install a specific version in another article.

    Check the core packages of your PHP 8 installation with apt list *fpm. Here, php-fpm is version 8.1 and php-fpm is version 8.1.2. The latest version may be good, but it is also a good idea to avoid the most recent version because compatibility problems may occur.

    sudo apt list *fpm
    php 8
    php 8

    Here we install php.

    sudo apt install php8.1-fpm
    php 8
    php 8
    php 8
    php 8
    php 8
    php 8

    3) Check PHP 8 status

    To check that the installation is complete and operating properly, run the sudo systemctl status php8.1-fpm command. If you previously installed a specific version, you can check it by writing the version in php.

    sudo systemctl status php8.1-fpm
    php 8
    php 8

    The service is running normally. Looking at the output, the service operates as php8.1-fpm.service and the conf file is /etc/php/8.1/fpm/php-fpm.conf.

    4) Additional installation of php 8 package for WordPress

    Some packages may not be needed right now. However, since it doesn’t really matter if you install it in advance, install all the basic packages.

    sudo apt install php8.1-common php8.1-mysql php8.1-xml php8.1-xmlrpc php8.1-curl php8.1-gd php8.1-imagick php8.1-cli php8.1-dev php8.1-imap php8.1-mbstring php8.1-opcache php8.1-redis php8.1-soap php8.1-zip
    php 8
    php 8

    3. Connect Nginx and PHP 8

    Installing the PHP package does not mean that the PHP file will be applied to the web server. Some files need to be modified so that nginx can serve PHP.

    1 file exists and 3 files need to be modified.

    1. The /etc/nginx/nginx.conf file exists.
    2. Check and add fastcig_param in /etc/nginx/fastcgi_params
    3. Add and modify php script in /etc/nginx/sites-available/default setting
    4. default_type in /etc/nginx/nginx.conf

    Fourth, if you do not fix it, there will be a problem where the php file will not run and be downloaded.

    1) Check /etc/nginx/nginx.conf

    If you followed the previous installation and steps, the nginx.conf file exists. If you don’t have it, just copy the following content and create a file. If you copy the contents below to create a file, you can skip step 4.

    Create a file.

    sudo nano /etc/nginx/nginx.conf

    Paste the following code:

    user www-data;
    worker_processes auto;
    pid /run/nginx.pid;
    include /etc/nginx/modules-enabled/*.conf;
    
    events {
            worker_connections 768;
            # multi_accept on;
    }
    
    http {
            ##
            # Basic Settings
            ##
            sendfile on;
            tcp_nopush on;
            types_hash_max_size 2048;
            # server_tokens off;
            # server_names_hash_bucket_size 64;
            # server_name_in_redirect off;
            include /etc/nginx/mime.types;
            #default_type application/octet-stream;
            default_type text/html;
            ##
            # SSL Settings
            ##
            ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
            ssl_prefer_server_ciphers on;
    
            ##
            # Logging Settings
            ##
            access_log /var/log/nginx/access.log;
            error_log /var/log/nginx/error.log;
    
            ##
            # Gzip Settings
            ##
            gzip on;
            # gzip_vary on;
            # gzip_proxied any;
            # gzip_comp_level 6;
            # gzip_buffers 16 8k;
            # gzip_http_version 1.1;
            # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
    
            ##
            # Virtual Host Configs
            ##
            include /etc/nginx/conf.d/*.conf;
            include /etc/nginx/sites-enabled/*;
    }
    
    #mail {
    #       # See sample authentication script at:
    #       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
    #
    #       # auth_http localhost/auth.php;
    #       # pop3_capabilities "TOP" "USER";
    #       # imap_capabilities "IMAP4rev1" "UIDPLUS";
    #
    #       server {
    #               listen     localhost:110;
    #               protocol   pop3;
    #               proxy      on;
    #       }
    #
    #       server {
    #               listen     localhost:143;
    #               protocol   imap;
    #               proxy      on;
    #       }
    #}

    2) Check and add fastcig_param in /etc/nginx/fastcgi_params

    Open /etc/nginx/fastcgi_param with an editor and add the following if it does not already exist:

    sudo nano /etc/nginx/fastcgi_params

    Add the following code to the first line:

    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    Article image 8
    Article image 8

    3) Add and modify php script in /etc/nginx/sites-available/default setting.

    Run the default file with an editor and modify the location part as follows. Here, we modify it based on version 8.1. Simply find and change the following part of the existing file:

    # pass PHP scripts to FastCGI server
    
    location ~ \.php$ {
           include snippets/fastcgi-php.conf;
    
           # With php-fpm (or other unix sockets):
           fastcgi_pass unix:/run/php/php8.1-fpm.sock;
    }
    Article image 9
    Article image 9

    Restart both Nginx and PHP.

    sudo systemctl restart nginx
    sudo systemctl restart php8.1-fpm

    4. Run PHP 8 and check

    Create a file to check PHP information. The file is created in the web root.

    sudo nano /var/www/html/info.php

    Copy and paste the following content according to the PHP code rules.

    <?php
    phpinfo();
    ?>

    Check the php information on the website with localhost/info.php or address/info.php.

    Article image 10
    Article image 10

    Please note that in order to install WordPress, you must modify the php.ini file. Executing the php file and modifying the php.ini file will be covered in the WordPress installation article.

    Good article to read together

    • Setting up Nginx + Php8
    • Free HTTPS setup (Let’s Encrypt, Cloudflare)
    • Install memory caching APCu, Redis, Memcached
    • Install Nginx web server (Centos 8)
    • Install Nginx web server (ubuntu)

    Related Reading

    Continue with these related Thinknote English articles in the Server & Infrastructure cluster.

    FAQ

    What is this article about?

    This article is part of Thinknote’s English server and infrastructure archive. It focuses on practical Linux, Ubuntu, web-server, database, SSH, firewall, or hosting operations that readers can adapt to their own environment.

    How should I use this guide?

    Use it as a practical checklist and concept guide. Before applying commands on a live server, verify package names, OS versions, ports, and backup requirements for your own setup.

    Where can I read the original Korean article?

    The original Korean article is available here: Original Korean article.

  • Setting up Nginx + Php8

    Setting up Nginx + Php8

    1. Connect Nginx and PHP 8

    Installing the nginx php8 package does not mean that the php file will be applied to the web server. Some files need to be modified so that nginx can serve php8. If your settings are incorrect, php8 will not work properly with nginx. In particular, there is a problem where nothing appears when the php file is downloaded or when phpinfo or php code is run.

    Original Korean article: Setting up Nginx + Php8

    The part we are checking now will help PHP operate properly on the web server through nginx and PHP8 settings.

    1 file exists and 3 files need to be modified.

    1. The /etc/nginx/nginx.conf file exists.
    2. Check and add fastcig_param in /etc/nginx/fastcgi_params
    3. Add and modify php script in /etc/nginx/sites-available/default setting
    4. default_type in /etc/nginx/nginx.conf

    Fourth, if you do not fix it, there will be a problem where the php file will not run and be downloaded.

    1) Check /etc/nginx/nginx.conf

    If you followed the previous installation and steps, the nginx.conf file exists. If you don’t have it, just copy the following content and create a file. If you copy the contents below to create a file, you can skip step 4.

    The user set below may differ depending on the PHP version. user must match the user, group, listen.owner, and listen.group information in sudo nano /etc/php/8.1/fpm/pool.d/www.conf.

    Create a file.

    sudo nano /etc/nginx/nginx.conf

    Paste the following code:

    user www-data;
    worker_processes auto;
    pid /run/nginx.pid;
    include /etc/nginx/modules-enabled/*.conf;
    
    events {
            worker_connections 768;
            # multi_accept on;
    }
    
    http {
            ##
            # Basic Settings
            ##
            sendfile on;
            tcp_nopush on;
            types_hash_max_size 2048;
            # server_tokens off;
            # server_names_hash_bucket_size 64;
            # server_name_in_redirect off;
            include /etc/nginx/mime.types;
            #default_type application/octet-stream;
            default_type text/html;
            ##
            # SSL Settings
            ##
            ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
            ssl_prefer_server_ciphers on;
    
            ##
            # Logging Settings
            ##
            access_log /var/log/nginx/access.log;
            error_log /var/log/nginx/error.log;
    
            ##
            # Gzip Settings
            ##
            gzip on;
            # gzip_vary on;
            # gzip_proxied any;
            # gzip_comp_level 6;
            # gzip_buffers 16 8k;
            # gzip_http_version 1.1;
            # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
    
            ##
            # Virtual Host Configs
            ##
            include /etc/nginx/conf.d/*.conf;
            include /etc/nginx/sites-enabled/*;
    }
    
    #mail {
    #       # See sample authentication script at:
    #       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
    #
    #       # auth_http localhost/auth.php;
    #       # pop3_capabilities "TOP" "USER";
    #       # imap_capabilities "IMAP4rev1" "UIDPLUS";
    #
    #       server {
    #               listen     localhost:110;
    #               protocol   pop3;
    #               proxy      on;
    #       }
    #
    #       server {
    #               listen     localhost:143;
    #               protocol   imap;
    #               proxy      on;
    #       }
    #}

    2) Check and add fastcig_param in /etc/nginx/fastcgi_params

    Open /etc/nginx/fastcgi_param with an editor and add the following if it does not already exist:

    sudo nano /etc/nginx/fastcgi_params

    Add the following code to the first line:

    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    Article image 1
    Article image 1

    3) Add and modify php script in /etc/nginx/sites-available/default setting.

    Run the default file with an editor and modify the location part as follows. Here, we modify it based on version 8.1. Simply find and change the following part of the existing file:

    # pass PHP scripts to FastCGI server
    
    location ~ \.php$ {
           include snippets/fastcgi-php.conf;
    
           # With php-fpm (or other unix sockets):
           fastcgi_pass unix:/run/php/php8.1-fpm.sock;
    }
    Article image 2
    Article image 2

    Restart both Nginx and PHP.

    sudo systemctl restart nginx
    sudo systemctl restart php8.1-fpm

    4. Run PHP 8 and check

    Create a file to check PHP information. The file is created in the web root.

    sudo nano /var/www/html/info.php

    Copy and paste the following content according to the PHP code rules.

    <?php
    phpinfo();
    ?>

    Check the php information on the website with localhost/info.php or address/info.php.

    Article image 3
    Article image 3

    This article was written by selecting only the problem-solving part of the PHP8 settings after installing nginx, and is duplicated with the previous article. If you want to know more about installing nginx, click the following link:

    Install Nginx web server (ubuntu) – Thinknote

    If you want to install php8 with Ubuntu’s nginx installed, click the following link. PHP installation is a preparatory step for installing WordPress.

    Install PHP 8 (ubuntu) – Thinknote

    Good article to read together

    • Install PHP 8 (ubuntu)
    • Install memory caching APCu, Redis, Memcached
    • Install Nginx web server (ubuntu)
    • Free HTTPS setup (Let’s Encrypt, Cloudflare)
    • Install Nginx web server (Centos 8)

    Related Reading

    Continue with these related Thinknote English articles in the Server & Infrastructure cluster.

    FAQ

    What is this article about?

    This article is part of Thinknote’s English server and infrastructure archive. It focuses on practical Linux, Ubuntu, web-server, database, SSH, firewall, or hosting operations that readers can adapt to their own environment.

    How should I use this guide?

    Use it as a practical checklist and concept guide. Before applying commands on a live server, verify package names, OS versions, ports, and backup requirements for your own setup.

    Where can I read the original Korean article?

    The original Korean article is available here: Original Korean article.

  • How to Use the R Pipe Operator for Data Analysis Workflows

    How to Use the R Pipe Operator for Data Analysis Workflows

    The pipe operator (%>%) in R allows you to increase both the readability and efficiency of your R programming code at the same time.

    파이프 연산자
    파이프 연산자

    Original Korean article: How to use the R pipe operator %>%: An easy way to read data analysis flows

    The R pipe operator is a grammar that helps you read the multi-step data processing process from top to bottom. The more nested functions you have, the more complex your code becomes, but pipes allow you to organize your analysis flow into a natural order. This article explains the basic structure of %>%, how to read it, and frequently used patterns in data analysis.

    In this article, we will take a detailed look at what these pipe operators are, why they are needed, and how they can be used.

    1. What is the pipe operator %>%?

    The %>% operator, or pipe operator, is mainly used in the dplyr and tidyverse packages. The main purpose of this operator is to clearly pass data or results to the next function.

    This makes your code more modular and makes it clear what's happening at each step.

    # example example
    result <- data %>%
      filter(age > 30) %>%
      select(name, age)

    2. Why should we use the pipe operator %>%?

    1) Improved code readability

    It makes complex data processing processes easier to understand at a glance. Typically, when multiple functions and operations are listed on a single line in R code, reading that code requires considerable effort.

    However, by using the pipe operator, you can clearly distinguish each step and understand the code more intuitively.

    2) Increased maintainability

    Code written using the pipe operator is easy to modify and extend. If you need to add or delete a new operation in a specific step, you only need to modify that part. This makes code easier to maintain.

    3) Intuitive data processing

    Pipe operators represent the flow of data vertically. This helps you understand more intuitively how your data is transformed.

    3. Example of pipe operator usage

    The %>% operator is also called the pipe operator and is mainly used in R, especially in the dplyr package and tidyverse package. The basic role of this operator is to connect the input and output of a function in a clear and readable way.

    The pipe operator receives data, processes it, and passes the result as the first argument to the next function. This makes the code much more readable and provides a clearer view of the data processing flow.

    For example, the following two pieces of code, using dplyr 's filter() and select() functions, accomplish the same thing:

    If you don't use the pipe operator:

    filtered_data <- filter(data, age > 30)
    result <- select(filtered_data, name, age)

    When using the pipe operator:

    result <- data %>%
      filter(age > 30) %>%
      select(name, age)

    In the second example using pipes, you can see at a glance the code starting from data ( data ) and what transformations ( filter , select ) it goes through. In this way, the pipe operator improves the readability of your code and helps you express your logic more clearly.

    1) Pipe operator basic data processing

    First, let's load the dplyr package and do simple data filtering, selection, and sorting.

    # dplyr example example
    library(dplyr)
    
    # example example
    filtered_data <- mtcars %>%
      filter(mpg > 20)
    
    # example example example example
    sorted_data <- mtcars %>%
      select(mpg, cyl) %>%
      arrange(desc(mpg))

    2) Pipe operator complex data processing scenarios

    Even complex data processing can be expressed concisely through the pipe operator.

    The example below shows the process of filtering, grouping, summarizing, and sorting mtcars data all at once.

    result <- mtcars %>%
      filter(mpg > 20) %>%
      group_by(cyl) %>%
      summarise(avg_mpg = mean(mpg)) %>%
      arrange(desc(avg_mpg))

    4. Conclusion

    The pipe operator %>% is a powerful tool for effectively processing data in R programming. You can increase the readability of your code, improve maintainability, and clearly express the logic of data processing.

    So, the use of this operator is almost essential when performing data analysis or data science work in R. Enjoy a more efficient data analysis experience with the %>% operator.

    To download the R program, you can click the download link on the R program's official website (https://www.r-project.org/).

    View all R programs

    Good article to read together

    • Text replacement str_replace, str_replace_all functions
    • str_squish function to remove unnecessary spaces
    • Understanding Tibble and the as_tibble() function
    • unnest_tokens() function
    • Execute PHP and R code in conjunction

    Key Checklist

    • Can the analysis sequence be read from top to bottom?
    • Is piped code clearer than nested functions?
    • Have you confirmed what the input and output data are for each step?
    • Are you creating pipe chains that are longer than necessary?

    Good R statistics articles to read together

    • How to use unnest_tokens function: How to split by words in R text mining
    • Variables and Measurement R Statistics: Understanding independent variables, dependent variables and measurement levels
    • What is research: Summary of research concepts for introduction to R statistics
    • Validity/Reliability R Statistics: Criteria for judging a good measurement tool

    Related Reading

    Continue with these related Thinknote English articles in the Data Analysis cluster.

    FAQ

    What is this article about?

    This article is part of Thinknote’s English R statistics and data-analysis archive. It explains research, measurement, text processing, or tidyverse-style workflow concepts in practical language.

    How should I use this guide?

    Use it as a learning note and starter reference. When applying code, adjust package versions, object names, and dataset structure to your own R environment.

    Where can I read the original Korean article?

    The original Korean article is available here: Original Korean article.

  • Text replacement str_replace, str_replace_all functions

    Text replacement str_replace, str_replace_all functions

    The text replacement (str_replace, str_replace_all) function is a very useful tool in string processing. The text replacement function is included in the stringr package and replaces one string with another.

    텍스트 교체

    Original Korean article: Text replacement str_replace, str_replace_all functions

    1. Concept of text replacement (str_replace, str_replace_all) function

    1) str_replace()

    The str_replace() function replaces the first occurrence of a specific pattern within a string with another string.

    # stringr example example
    library(stringr)
    
    # example
    str_replace("apple orange apple", "apple", "banana")
    # example: "banana orange apple"

    In the example above, “apple” was replaced with “banana” only on its first occurrence.

    2) str_replace_all()

    On the other hand, the str_replace_all() function replaces every specific pattern within a string with another string.

    # example
    str_replace_all("apple orange apple", "apple", "banana")
    # example: "banana orange banana"

    In the example above, all “apple” words have been replaced with “banana”.

    2. Main usage of str_replace, str_replace_all functions

    Basic usage

    • str_replace(string, pattern, replacement)
    • str_replace_all(string, pattern, replacement)

    string: target string pattern: pattern to find replacement: string to replace

    1) Pattern matching using regular expressions

    You can use regular expressions in the pattern parameter. For example, if you want to remove all numbers, you can do this:

    # str_replace() example example
    str_replace("apple1 orange2", "[0-9]", "")
    # example: "apple orange2"
    
    # str_replace_all() example example
    str_replace_all("apple1 orange2", "[0-9]", "")
    # example: "apple orange"

    2) Replace multiple patterns at once

    The str_replace_all() function can replace multiple patterns at once. In this case, we pass pattern and replacement as named vectors.

    # example example example example
    str_replace_all("apple orange pear", c("apple" = "banana", "orange" = "grape"))
    # example: "banana grape pear"

    3) Replace all characters except Korean, English, and numbers with empty data

    You can use the str_replace_all function to replace all characters except Korean, English, and numbers with empty data. Let's apply this using regular expressions.

    Below is an example using the stringr package.

    # stringr example example
    library(stringr)
    
    # example example
    example_str <- "example! Hello, 1234!!@@"
    
    # example, example, example example example example example example example
    cleaned_str <- str_replace_all(example_str, "[^example-examplea-zA-Z0-9]", "")
    
    # example example
    print(cleaned_str)
    #exampleHello1234

    In the above code, "[^ga-hia-zA-Z0-9]" means all characters except Korean (ga-hi), English (a-zA-Z), and numbers (0-9). You can replace these with an empty string and get the result:

    3. Concluding how to use the text replacement function

    The text replacement (str_replace, str_replace_all) function is a very useful tool when processing text data. Using these functions, you can easily solve complex string processing tasks.

    In particular, when used with regular expressions, you can achieve more powerful string processing capabilities.

    To download the R program, you can click the download link on the R program's official website (https://www.r-project.org/).

    View all R programs

    Good article to read together

    • Install PHP 8 (ubuntu)
    • Setting up Nginx + Php8
    • Install memory caching APCu, Redis, Memcached
    • Install Centos 8
    • Linux user management useradd usermod userdel

    Related Reading

    Continue with these related Thinknote English articles in the Data Analysis cluster.

    FAQ

    What is this article about?

    This article is part of Thinknote’s English R statistics and data-analysis archive. It explains research, measurement, text processing, or tidyverse-style workflow concepts in practical language.

    How should I use this guide?

    Use it as a learning note and starter reference. When applying code, adjust package versions, object names, and dataset structure to your own R environment.

    Where can I read the original Korean article?

    The original Korean article is available here: Original Korean article.

  • str_squish function to remove unnecessary spaces

    str_squish function to remove unnecessary spaces

    To remove unnecessary spaces, use the str_squish function. The str_squish function is included in the stringr package of the R programming language and performs the function of removing leading, trailing, and intervening whitespace from a string. To remove unnecessary spaces, let's check the concept and main uses of the str_squish function.

    불필요한 공백제거

    Original Korean article: str_squish function to remove unnecessary spaces

    1. The concept of str_squish

    str_squish() removes unnecessary spaces at the beginning and end of the target string, and reduces consecutive spaces within the string to a single space. For example, if you have a string called “Hello World”, applying the str_squish() function will convert it to “Hello World”.

    # stringr example example
    library(stringr)
    
    # example
    str_squish("   Hello   World  ")
    # example: "Hello World"

    2. Main usage

    Basic usage

    • str_squish(string)

    string: target string

    1) Apply to vector

    The str_squish function can also be applied to string vectors. In this case, the function is applied to each string element.

    # example
    str_squish(c("   Hello  ", "  World  "))
    # example: "Hello" "World"

    2) Apply to data frame

    The str_squish function with the dplyr package allows you to apply a function to specific columns in a data frame.

    # dplyr example example
    library(dplyr)
    
    # example example example
    df <- data.frame(name = c("  Alice  ", "  Bob  ", "  Carol  "),
                     age = c(30, 40, 50))
    
    # str_squish example
    df <- df %>%
      mutate(name = str_squish(name))
    
    # example example
    print(df)

    3. Conclusion

    The str_squish() function is a very useful tool when squeezing text data. This function simplifies complex string processing tasks. It is often used in data analysis or text mining tasks, so it is a good idea to learn how to use this function.

    To download the R program, you can click the download link on the R program's official website (https://www.r-project.org/).

    View all R programs

    Good article to read together

    • 1. What is research? [R Statistics]
    • Text replacement str_replace, str_replace_all functions
    • Understanding Tibble and the as_tibble() function
    • unnest_tokens() function
    • Execute PHP and R code in conjunction

    Practical Use Cases for str_squish()

    In real text analysis projects, unnecessary spaces often appear when data is copied from web pages, spreadsheets, survey answers, PDF extractions, or manually typed forms. A string may look clean on the screen, but it can contain repeated spaces, tabs, or line-break-like spacing that makes grouping, joining, filtering, or tokenizing less reliable.

    The value of str_squish() is that it solves two problems at once. First, it removes leading and trailing whitespace. Second, it compresses repeated internal whitespace into a single normal space. This makes the function especially useful before comparing labels, cleaning names, standardizing categories, or preparing text for tokenization.

    When to Use It in an R Workflow

    • Before using text as a key for joins or matching.
    • Before counting unique values in a survey or category column.
    • Before tokenizing Korean or English text for text mining.
    • Before exporting cleaned data to a report or dashboard.

    A practical rule is simple: if a text column came from outside your own controlled data pipeline, normalize whitespace early. That small cleaning step can prevent confusing duplicate categories and unexpected mismatches later in the analysis.

    Related Reading

    Continue with these related Thinknote English articles in the Data Analysis cluster.

    FAQ

    What is this article about?

    This article is part of Thinknote’s English R statistics and data-analysis archive. It explains research, measurement, text processing, or tidyverse-style workflow concepts in practical language.

    How should I use this guide?

    Use it as a learning note and starter reference. When applying code, adjust package versions, object names, and dataset structure to your own R environment.

    Where can I read the original Korean article?

    The original Korean article is available here: Original Korean article.

  • Understanding Tibble and the as_tibble() function

    Understanding Tibble and the as_tibble() function

    1. What is Tibble?

    Tibble is one of the data structures for handling data in R and can be seen as a more useful extension of R's data frame (data.frame).

    tibble

    Original Korean article: Understanding Tibble and the as_tibble() function

    Tibble is provided as part of the tidyverse package and is compatible with data frames. Tibble has nice-looking data output, is useful when processing partially large data, and is simpler when dealing with variable types or variable names.

    1) Main features

    • Output: Tibble is highly readable when output to the console. First, you may want to show only 10 rows and not all columns.
    • Column data types: Tibble maintains column data types better. For example, character data remains character type.
    • Partial selection of columns and rows: Tibble is also more reliable when using [[]] or $. For example, requesting a column name that does not exist will return an error.

    2) Creating Tibble

    There are several ways to create a Tibble.

    • Create your own using the tibble() function:
    library(tibble) my_tibble <- tibble(x = 1:5, y = 1, z = x ^ 2 + y)
    • Convert an existing data frame to the as_tibble() function:
    my_data_frame <- data.frame(x = 1:5, y = 1, z = x ^ 2 + y) my_tibble <- as_tibble(my_data_frame)
    • Create it directly using the tibble() function: library(tibble) my_tibble <- tibble(x = 1:5, y = 1, z = x ^ 2 + y)
    • Convert an existing data frame to the as_tibble() function: my_data_frame <- data.frame(x = 1:5, y = 1, z = x ^ 2 + y) my_tibble <- as_tibble(my_data_frame)

    3) Using Tibble

    Tibble works much like a data frame, so it is compatible with most data frame functions.

    # example example
    my_tibble$x
    # example example
    my_tibble[1:2,]
    # dplyr example
    library(dplyr)
    my_tibble %>% filter(x > 2)

    4) Tibble add-on

    Tibble also offers some additional features and options. For example, you can force data types and add metadata for rows and columns.

    Tibble is easier to handle, more readable, and can effectively handle large data sets than traditional data frames.

    tibble is a subclass of data frame (data.frame) used when handling data in R. tibble is part of the tidyverse, making data processing simpler and more efficient in a variety of ways. The as_tibble() function converts a given data object into a tibble object.

    3) Check Tibble data type

    To check the tibble data type, you must install the tibble package.

    install.packages("tibble")

    You can check tibble data with is_tibble. The output value of class(ti_iris) [1] “tbl_df” “tbl” “data.frame” indicates that the ti_iris object has multiple classes. In R, an object can have multiple classes, which reflects the object's inheritance structure.

    1. “tbl_df”: This indicates that ti_iris is a data frame in tibble format. tbl_df is a class defined in the tibble package.
    2. “tbl”: This class is the superclass of tbl_df and represents the basic characteristics of a tbl object. This usually appears together with tbl_df and is defined in the tibble package.
    3. “data.frame”: This indicates that ti_iris is also a regular R data frame by default. data.frame is one of the base classes in R.
    is_tibble(ti_iris)
    TRUE
    class(ti_iris)
    [1] "tbl_df"     "tbl"        "data.frame"

    2. Understanding as_tibble() function

    The as_tibble() function converts various data objects (e.g. data.frame, matrix, etc.) into tibble form. A tibble is similar to a data frame, but has some important differences. For example, tibble allows for cleaner data output and more flexibility in handling variable types.

    The as_tibble() function is used to convert data into tibble format and supports various options. Below is a detailed description of the options:

    1) x

    The first argument x is the data you want to convert. This can take many forms: vectors, lists, data frames, matrices, etc.

    as_tibble(data.frame(x = 1:3, y = 4:6))

    2) .rows

    The .rows option specifies the number or range of rows to load. This allows you to select only some rows from a large dataset.

    as_tibble(iris, .rows = 1:5)

    3) .name_repair

    The .name_repair option specifies how to handle column names. This option can have the following values:

    • “minimal”: No modifications are made.
    • “unique”: Makes column names unique.
    • “universal”: Converts to a valid column name.
    • “check_unique”: Checks if column names are unique, otherwise raises an error.
    as_tibble(data.frame(x = 1, x = 2), .name_repair = "unique")

    4) .col_names (deprecated)

    This option was used to specify column names in previous versions, but is now deprecated. Use .name_repair instead.

    5) … (deprecated)

    This is an option to accept additional arguments and is currently deprecated.

    Example:

    # .name_repairexample example example example example
    as_tibble(data.frame(` ` = 1:3, x = c('a', 'b', 'c')), .name_repair = "universal")
    # .rowsexample example example example example
    as_tibble(mtcars, .rows = 1:5)

    Combining these options allows you to perform a wide variety of data transformation tasks.

    3. Main usage of as_tibble() function

    1) Basic usage

    • as_tibble(x, …)

    x : target data object … : additional optional arguments

    2) Convert data frame to tibble

    # tibble example example
    library(tibble)
    # example example example
    df <- data.frame(name = c("Alice", "Bob", "Carol"),
                     age = c(30, 40, 50))
    # as_tibbleexample example
    df_tibble <- as_tibble(df)
    # example example
    print(df_tibble)

    3) Convert matrix to tibble

    # example example
    mat <- matrix(1:6, nrow = 2)
    # as_tibbleexample example
    mat_tibble <- as_tibble(mat)
    # example example
    print(mat_tibble)

    4) Convert list to tibble

    For lists, each list element becomes a column of tibble.

    # example example
    lst <- list(name = c("Alice", "Bob"), age = c(30, 40))
    # as_tibbleexample example
    lst_tibble <- as_tibble(lst)
    # example example
    print(lst_tibble)

    4. as_tibble() complex example

    1) Overlapping data

    The as_tibble function can also handle nested data structures, such as lists of lists.

    # example example
    nested_list <- list(
      meta = list(name = "sample", version = "1.0"),
      data = list(
        id = 1:3,
        value = c("a", "b", "c")
      )
    )
    # example example tibbleexample example
    nested_tibble <- as_tibble(nested_list)
    print(nested_tibble)

    Here, nested_tibble has two columns, meta and data, and each column is again made up of a list.

    2) Combination of data frame and list

    The as_tibble function is also useful when some columns of the data frame are made up of lists.

    # example example example example
    df_with_list <- data.frame(
      id = 1:3,
      meta = I(list(
        list(name = "Alice", age = 30),
        list(name = "Bob", age = 40),
        list(name = "Carol", age = 50)
      ))
    )
    # as_tibbleexample example
    tibble_with_list <- as_tibble(df_with_list)
    print(tibble_with_list)

    In this way, the as_tibble function can flexibly handle complex data structures along with various options. You can obtain more efficient results by utilizing the various features of this function in complex data analysis or preprocessing tasks.

    To download the R program, you can click the download link on the R program's official website (https://www.r-project.org/).

    Good article to read together

    • Text replacement str_replace, str_replace_all functions
    • str_squish function to remove unnecessary spaces
    • unnest_tokens() function
    • Execute PHP and R code in conjunction
    • Importance and usage of pipe operator %>%

    Related Reading

    Continue with these related Thinknote English articles in the Data Analysis cluster.

    FAQ

    What is this article about?

    This article is part of Thinknote’s English R statistics and data-analysis archive. It explains research, measurement, text processing, or tidyverse-style workflow concepts in practical language.

    How should I use this guide?

    Use it as a learning note and starter reference. When applying code, adjust package versions, object names, and dataset structure to your own R environment.

    Where can I read the original Korean article?

    The original Korean article is available here: Original Korean article.

  • How to Use unnest_tokens() in R Text Mining

    How to Use unnest_tokens() in R Text Mining

    The unnest_tokens () function is a function included in the tidytext package of the R programming language, and separates text data into tokens. This function processes text appropriately for the ‘tidy data’ format, making it useful for text mining and natural language processing. This function creates a new row for each token, leaving columns other than those containing the text intact.

    unnest_tokens

    Original Korean article: How to use unnest_tokens function: How to split by words in R text mining

    The unnest_tokens function is a key tool in R text mining to divide sentences or documents into words. To analyze text data, you must first break sentences into tokens and connect them to the next step, such as word frequency or sentiment analysis. This article summarizes the tidytext-based tokenization flow and usage of the unnest_tokens function.

    1. unnest_tokens() concept

    The unnest_tokens() function is included in R's tidytext package and is used to tokenize text data. Tokenization is the process of breaking down long text strings into smaller units, such as words or sentences.

    This function is useful for converting text data into a form that is easy to process and analyze. For example, you can separate words that make up a single document or sentence.

    1) Installation and library loading

    install.packages("tidytext")
    library(tidytext)

    2) basic usage of unnest_tokens

    The basic function form can be seen like this.

    unnest_tokens(data, output_column, input_column, token = "words", ...)
    • data: The data frame to tokenize.
    • output_column: The name of the new column in which to store the token.
    • input_column: Name of the column containing the text to tokenize.
    • token: Type of token (default is “words”).

    example

    library(dplyr)
    library(tibble)
    
    # example example
    data <- tibble(id = c(1, 2), text = c("I love R", "Data science is awesome"))
    
    # example example
    tokenized_data <- data %>%
      unnest_tokens(word, text)
    
    # example example
    print(tokenized_data)

    In this example, we used a tibble dataframe with an id column and a text column. We applied the unnest_tokens() function to tokenize the text in the text column into words, and stored the results in a new word column.

    Additional options

    • drop : If set to FALSE, include the input column in the results.
    • to_lower: If set to FALSE, it is case sensitive.
    • strip_numeric , strip_punct , strip_mark , collapse etc: Additional text cleaning options.

    This function is very flexible and can be applied to a variety of text data. It can be used with several tokenization options and other tidytext functions to perform more complex text analysis tasks.

    • Token: This is the basic unit when analyzing text, and can usually be a word, phrase, or sentence.
    • Tidy Text: This refers to a text data format in which each word (token) forms one line and is stored together with a document or other identifier.

    2. unnest_tokens parameter

    The unnest_tokens() function has several options that allow you to fine-tune the process of tokenizing text. I'll explain some of the main options below.

    1) Basic parameters:

    1. data: The data frame to tokenize.
    2. output_column: The name of the new column in which to store the token.
    3. input_column: Name of the column containing the text to tokenize.
    4. token: Type of token (e.g. “words”, “characters”, etc.)

    2) Additional options:

    1. drop: logical type. Whether to remove the input column from the results. The default is TRUE.
    2. to_lower: logical type. Whether to convert all characters to lowercase. The default is TRUE.
    3. strip_numeric: logical type. Whether to remove numbers. The default is FALSE.
    4. strip_punct: logical type. Whether to remove punctuation. The default is FALSE.
    5. collapse: string. Whether to concatenate tokens with this string. The default is NULL.
    library(dplyr)
    library(tidytext)
    
    # example example
    data <- tibble(id = c(1, 2), text = c("I love R", "Data science is awesome"))
    
    # example example, example example example, example example
    tokenized_data <- data %>%
      unnest_tokens(word, text, drop = FALSE, to_lower = FALSE)
    
    # example example
    print(tokenized_data)

    3) Remarks

    • If you set drop = FALSE, the original input_column will be retained in the result even after tokenization.
    • If you set to_lower = FALSE, case will be preserved.
    • If you set strip_numeric = TRUE, numbers will be removed.
    • If you set strip_punct = TRUE, punctuation will be removed.

    By combining these options, you can increase the precision of tokenization or simplify the preprocessing process.

    • input: The name of the column containing the text to tokenize.
    • output: The name of the new column in which to store the tokenized results.
    • token: This is an option that determines what unit to tokenize in. These include ‘words’, ‘characters’, ‘ngrams’, ‘sentences’, ‘lines’, ‘paragraphs’, and ‘regex’.

    4) Omitted

    You can omit the input and output parameters in the unnest_tokens() function, but in that case the function will use the default settings of the first column of the data frame as input and word as the output column name. So you can also use it in the following form:

    text %>%
      unnest_tokens(token = "sentences")

    However, if you do this, it can be difficult to clearly understand from code alone which columns are being tokenized and in which columns the tokens are stored. For code readability and maintainability, it is recommended to specify input and output explicitly.

    Explicitly specifying column names is recommended because it makes it easier for readers of your code or when modifying your code later to know what the column means.

    To download the R program, you can click the download link on the R program's official website (https://www.r-project.org/).

    Good article to read together

    • Text replacement str_replace, str_replace_all functions
    • str_squish function to remove unnecessary spaces
    • Understanding Tibble and the as_tibble() function
    • Execute PHP and R code in conjunction
    • Importance and usage of pipe operator %>%

    Key Checklist

    • Is the text column to be analyzed clear?
    • Have you decided which unit to divide into: sentences, words, or n-grams?
    • Is there a plan to remove stop words and analyze frequencies after tokenization?
    • Have you confirmed whether morpheme analysis is necessary in processing Korean text?

    Good R statistics articles to read together

    • How to use the R pipe operator %>%: An easy way to read data analysis flows
    • Research Method Introduction to R Statistics: Understanding research design and analysis methods at a glance
    • Variables and Measurement R Statistics: Understanding independent variables, dependent variables and measurement levels
    • What is research: Summary of research concepts for introduction to R statistics

    Related Reading

    Continue with these related Thinknote English articles in the Data Analysis cluster.

    FAQ

    What is this article about?

    This article is part of Thinknote’s English R statistics and data-analysis archive. It explains research, measurement, text processing, or tidyverse-style workflow concepts in practical language.

    How should I use this guide?

    Use it as a learning note and starter reference. When applying code, adjust package versions, object names, and dataset structure to your own R environment.

    Where can I read the original Korean article?

    The original Korean article is available here: Original Korean article.

  • How to connect PHP to R: How to call an R script on the web and receive the result

    How to connect PHP to R: How to call an R script on the web and receive the result

    1. Execute PHP and R code in conjunction

    Linking PHP and R code: Running PHP and R code in conjunction combines the advantages of both programming languages ​​to handle complex web applications and data analysis together. However, since there are many pros and cons, you must consider your development goals and scenarios to decide whether to link PHP and R code.

    php와 R 코드

    Original Korean article: How to connect PHP to R: How to call an R script on the web and receive the result

    1) Advantages of linking PHP and R code

    1. Leverage language expertise: PHP is strong in web development, and R is strong in data analysis. You can take advantage of the strengths of both languages.
    2. Code reuse: Data analysis code or models already written in R can be easily reused in web applications.
    3. Create dynamic web content: By running R code in PHP, you can dynamically display real-time data analysis results on your website.
    4. Capable of complex analysis: R provides a wide variety of data analysis functions, including statistical analysis, machine learning, and graph creation.
    5. System resource efficiency: Make efficient use of system resources by executing R code only when needed.

    2) Disadvantages of linking PHP and R code

    1. Performance Issues: Running R code in PHP can be slow in general, and performance can be especially problematic when dealing with large amounts of data or complex analysis.
    2. Security Vulnerability: Using functions like exec or shell_exec puts your server at risk of vulnerability. Be careful when using these functions.
    3. Environment setup and management: Running R and PHP together requires both environments to be well set up and maintained, which can increase complexity.
    4. Error handling: Error handling can become complicated when linking two languages. Any errors that may occur in both PHP and R must be caught and managed.
    5. Memory usage: R is a fairly memory-intensive language. If PHP and R processes run simultaneously, memory usage can increase significantly.
    6. Version compatibility: Over time, R libraries or PHP packages are updated, which can cause compatibility issues.

    2. Execute R code in PHP using the exec function

    Executing R code using PHP's exec() function is a simple way to run external programs in PHP. This method generally requires R to be installed on the server running PHP, and make sure the exec() function is not disabled in the server settings.

    1) Concept

    The exec() function is used to run external programs in PHP. To run an R script using this function, simply pass as an argument the command to run the R script on the command line. Typically this command is Rscript.

    exec("Rscript [R example example]", $output);

    2) Example 1

    Example 1 is a very simple example of executing the R script simple_example.R using PHP's exec() function.

    R script ( simple_example.R ):

    result <- 1 + 1
    cat(result)

    PHP code:

    <?php
    exec("Rscript simple_example.R", $output);
    echo "R output: " . implode("\n", $output);
    ?>

    2) Example 2

    Example 2 shows the process of running an R script in PHP to fit a linear model, summarizing the results, and saving them to a text file. The PHP code then reads this text file and outputs it.

    R script ( linear_model.R ):

    x <- c(1, 2, 3, 4, 5)
    y <- c(2, 4, 1, 8, 7)
    fit <- lm(y ~ x)
    summary_str <- capture.output(summary(fit))
    write(summary_str, "summary.txt")

    PHP code:

    <?php
    exec("Rscript linear_model.R");
    
    // Rexample example summary.txt example example
    $summary = file_get_contents("summary.txt");
    echo "R Summary:\n$summary";
    ?>

    3) Example 3

    Example 3 demonstrates how an R script and PHP interact by reading and writing a CSV file. This example specifically demonstrates how an R script can take command line arguments and process them dynamically.

    R script ( data_processing.R ):

    args <- commandArgs(trailingOnly = TRUE)
    input_file <- args[1]
    output_file <- args[2]
    
    data <- read.csv(input_file)
    data$sum <- rowSums(data[, c("col1", "col2")])
    
    write.csv(data, output_file)

    PHP code:

    <?php
    $input_file = "input.csv";
    $output_file = "output.csv";
    
    exec("Rscript data_processing.R $input_file $output_file");
    
    // Rexample example output.csv example example
    $output_data = file_get_contents("output.csv");
    echo "R Output Data:\n$output_data";
    ?>

    caution

    • Make sure the exec() function is not disabled on the server.
    • Security issue: Be careful with the exec() function as it can cause server security issues if used incorrectly.
    • Error handling: The exec() function does not output a PHP warning on failure by default, so you may need to implement separate error handling logic.

    3. Run R code in PHP using the Rserve package

    1) Rserve concept

    Rserve is one of the packages provided by R that allows R to operate as a server. Typically R is used as a tool for interactive statistical calculations, but Rserve allows you to operate R as a server and run R code from other applications (e.g. PHP, Java, Python, etc.). This is accomplished using the TCP/IP protocol.

    • TCP/IP protocol support: Easy to communicate with other languages ​​or frameworks.
    • Multi-session support: Multiple users can use R’s services at the same time.
    • Platform independence: Can be used on a variety of operating systems and languages.
    • Low barrier to entry: Users familiar with R can use Rserve with relative ease.

    2) Rhythm

    1. Integration between languages: R's special data analysis libraries and functions can be easily used in other languages.
    2. Performance optimization: R tasks are processed on a separate server, reducing the load on the web server.
    3. Code reusability: The same R code can be reused in multiple applications.
    4. Concurrency: Multiple users can perform R analysis simultaneously.

    3) Disadvantages

    1. Setup complexity: There can be complexity in setting up interfaces between R, Rserve, and other programming languages.
    2. Debugging Difficulty: Interoperability between R and other languages ​​can complicate debugging.
    3. Security vulnerabilities: Because they communicate over TCP/IP, misconfiguration can lead to security vulnerabilities.

    4) Basic use

    1. Install Rserve in R: install.packages("Rserve")
    2. Start Rserve in R: library(Rserve) Rserve()
    3. Install the PHP Rserve client. Create a composer.json file and add the content below. { "require": { "cturbelin/rserve-php": "^2.1" } }
    4. When you run composer instll in the terminal, a vendor folder will be created and contain related files.
    5. Call an R function from your PHP code: <?php require './vendor/autoload.php'; define('RSERVE_HOST', 'localhost'); use Sentiweb\Rserve\Connection; use Sentiweb\Rserve\Parser\NativeArray; $cnx = new Connection(RSERVE_HOST); $r = $cnx->evalString('2+2' ); echo $r; ?>
    6. If an error occurs when running, check whether php-mbstring is installed correctly.

    5) Use multiple lines

    You can run multiple lines of R code inside $r->evalString(). You can write your R code as a string over multiple lines.

    <?php
    require './vendor/autoload.php';
    define('RSERVE_HOST', 'localhost');
    use Sentiweb\Rserve\Connection;
    use Sentiweb\Rserve\Parser\NativeArray;
    $cnx = new Connection(RSERVE_HOST);
    
    // example example R example
    $script = <<<RSCRIPT
    x <- c(1, 2, 3, 4, 5)
    y <- c(2, 4, 1, 8, 7)
    fit <- lm(y ~ x)
    summary_fit <- summary(fit)
    RSCRIPT;
    
    $r->evalString($script);
    
    $summary = $cnx->evalString('capture.output(summary_fit)');
    foreach($summary as $line) {
        echo $line . "\n";
    }
    ?>

    In this example, we included multiple lines of R code by writing it in the format HereDoc ( <<<RSCRIPT … RSCRIPT; ). After that, I am executing these multiple lines of code at once by calling the evalString() method.

    This allows even complex R scripts to run in PHP.

    3. Run R code in PHP using the Rserve package

    Rserve can be useful when web servers or other applications require complex statistical analysis or data processing. However, you should carefully consider the pros and cons mentioned above before using it.

    To download the R program, you can click the download link on the R program's official website (https://www.r-project.org/).

    Good article to read together

    • Install PHP 8 (ubuntu)
    • Setting up Nginx + Php8
    • Install memory caching APCu, Redis, Memcached
    • Install Centos 8
    • Linux user management useradd usermod userdel

    Related Reading

    Continue with these related Thinknote English articles in the Data Analysis cluster.

    FAQ

    What is this article about?

    This article is part of Thinknote’s English R statistics and data-analysis archive. It explains research, measurement, text processing, or tidyverse-style workflow concepts in practical language.

    How should I use this guide?

    Use it as a learning note and starter reference. When applying code, adjust package versions, object names, and dataset structure to your own R environment.

    Where can I read the original Korean article?

    The original Korean article is available here: Original Korean article.

  • Install OpenSSL

    1. OpenSSL functions and features

    OpenSSL is a collection of libraries and tools that implement encryption and security protocols. It is primarily used on Linux operating systems, but can be used on other operating systems as well. OpenSSL included with Ubuntu Server has the following functions and features:

    Original Korean article: Install OpenSSL

    1. Encryption and hash functions: OpenSSL supports a variety of encryption algorithms (AES, DES, RSA, etc.) and hash functions (MD5, SHA-1, SHA-256, etc.). This allows users to store or transmit data securely to ensure data security.
    2. SSL/TLS Protocol: OpenSSL is used to implement the Secure Sockets Layer (SSL) and Transport Layer Security (TLS) protocols. It encrypts communications between servers and clients, ensuring the confidentiality and integrity of data and protecting against man-in-the-middle attacks.
    3. Certificate Management: OpenSSL supports the X.509 certificate format, allowing you to create, manage, and verify public key certificates. It establishes a trust relationship between a server and a client and is used in e-commerce and authentication systems.
    4. Encryption key and CSR generation: OpenSSL can generate symmetric and asymmetric encryption keys. Additionally, you can generate a certificate signing request (CSR) and send it to the certification authority. CSR is used to request issuance of a server certificate.
    5. Command-line tools: OpenSSL includes command-line tools, allowing developers and system administrators to perform tasks such as encryption, hashing, certificate management, and more.
    6. Open Source: OpenSSL is an open source project, with many developers contributing to it. This helps verify source code and strengthen security, and has the advantage of receiving community support and updates.

    2. Install OpenSSL

    1) Install OpenSSL

    OpenSSL on Ubuntu Server is used to implement secure network communication by providing the above functions and features.

    sudo apt install openssl
    Article image 1
    Article image 1

    Install OpenSSL by running the following command:

    1) Check OpenSSL version

    openssl version

    3. Create param key

    1) param necessity

    The reason for using Openssl to generate param keys is mainly to generate parameters needed for security algorithms such as:

    1. Diffie-Hellman (DH) Key Exchange: DH is a type of public key cryptosystem that allows secure communication without sharing two different private keys. DH requires shared parameters p and g, which can be generated using OpenSSL.
    2. RSA encryption: RSA is a public key encryption method that uses prime numbers p and q to generate private and public keys. You can generate these prime numbers using OpenSSL.

    2) Create DH parameters

    Create DH parameters with size 2048 bits

    sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048

    Create DH parameters of size 4096 bits

    sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 4096

    You can change the size and file name according to your needs. There is also a way to use OpenSSL to generate the parameters needed for other encryption algorithms. The param key is used to generate parameters required for the Diffie-Hellman (DH) key exchange protocol used by OpenSSL. The DH protocol is a type of public key cryptosystem, used to generate a secret shared key between two entities. These secret keys can be used for secure communications.

    4. Additional study material

    If you would like to learn more about OpenSSL, you can refer to the following resources:

    • OpenSSL official documentation
    • OpenSSL page on Ubuntu Wiki

    Thinknote

    Good article to read together

    • Installing and managing MariaDB (MySQL)
    • Activate Centos 8 RHEL & REMI Repository
    • Linux administrator tool – enable cockpit
    • Install Ubuntu 22.04.1 (VirtualBox)
    • VirtualBox Virtual Box virtual machine download and installation

    Related Reading

    Continue with these related Thinknote English articles in the Server & Infrastructure cluster.

    FAQ

    What is this article about?

    This article is an English translation and global-reader adaptation of the original Thinknote article “Install OpenSSL.” It preserves the original article’s main explanation, examples, and practical context.

    Why is it translated into English?

    The English version helps global readers access Thinknote articles through English search keywords while keeping the Korean source available as the original reference.

    Where can I read the original Korean version?

    The original Korean article is available here: Original Korean article.

  • Installing and managing MariaDB (MySQL)

    1. Database package that can be installed on Ubuntu

    1. MySQL: A popular open source relational database management system (RDBMS).
    2. PostgreSQL: A powerful open source RDBMS that offers many features and extensibility.
    3. MongoDB: A document-oriented NoSQL database.
    4. SQLite: A lightweight, open source relational database engine suitable for embedded systems and small-scale applications.
    5. Redis: A high-performance key-value store and memory-based data structure server.
    6. MariaDB: A fork of MySQL that offers improved performance and stability while maintaining compatibility.

    2. Understanding MariaDB

    MariaDB is an open source relational database management system (RDBMS) that began as a fork of MySQL. It is fully compatible with MySQL, and users can easily migrate from their existing MySQL environment to MariaDB. MariaDB is used as the basic RDBMS in many Linux distributions.

    Original Korean article: Installing and managing MariaDB (MySQL)

    1) MariaDB Features

    • Performance: MariaDB provides excellent performance for processing large amounts of data. Performance improvements have been achieved through optimizations such as query optimization, indexing functions, and parallel processing.
    • Scalability: MariaDB can scale your database servers horizontally and vertically. Scalability can be achieved in a variety of ways, such as master-slave replication and clustering.
    • Security: MariaDB places a strong emphasis on database security. It provides features such as SSL/TLS encryption, access control, and data masking to ensure the safety of your data.
    • Openness: MariaDB was developed as open source, allowing users to access the source code to modify and improve it. It also offers a variety of plugins and extensions to customize it to suit your needs.

    2) MariaDB Advantages

    • MariaDB is fully compatible with MySQL, so existing MySQL users can easily migrate.
    • MariaDB delivers fast performance with optimized query processing and indexing features.
    • MariaDB allows you to flexibly scale your database servers.
    • MariaDB is stable in many Linux distributions and provides features for error recovery and fault tolerance.

    3) Disadvantages of MariaDB

    • Because it has a relatively small community compared to MySQL, troubleshooting and support can be difficult to find.
    • MariaDB is compatible with MySQL, but differences may occur in some specific features, which may cause some applications to not work properly.

    3. Install MariaDB

    1) Preparing MariaDB in advance

    Update and upgrade Ubuntu packages.

    sudo apt update
    sudo apt upgrade
    Article image 1
    Article image 1

    2) Install MariaDB

    sudo apt install mariadb-server
    Article image 2
    Article image 2

    2) Check MariaDB service

    Once installation is complete, the MariaDB service will start automatically. To check the service status, run the following command:

    sudo systemctl status mariadb
    Article image 3
    Article image 3

    3) Run MariaDB security script

    To configure MariaDB more easily, run the security script by running the following command:

    • Enter current password for root: During initial installation, there is no password, so press enter.
    • Change the root password: Enter Y and set the password.
    • Remove anonymou user: Type Y to remove an anonymous user.
    • Disallow root login remotely: Enter Y to enable remote login or N to disable it.
    • Remove test database and access to it: Type Y to delete the test database.
    • Reload privilege tables now: Type Y to save changes.
    sudo mysql_secure_installation
    Article image 4
    Article image 4

    The security settings for your MySQL server are now complete.

    Thinknote

    Good article to read together

    • Install OpenSSL
    • Activate Centos 8 RHEL & REMI Repository
    • Linux administrator tool – enable cockpit
    • Install Ubuntu 22.04.1 (VirtualBox)
    • VirtualBox Virtual Box virtual machine download and installation

    Related Reading

    Continue with these related Thinknote English articles in the Server & Infrastructure cluster.

    FAQ

    What is this article about?

    This article is part of Thinknote’s English server and infrastructure archive. It focuses on practical Linux, Ubuntu, web-server, database, SSH, firewall, or hosting operations that readers can adapt to their own environment.

    How should I use this guide?

    Use it as a practical checklist and concept guide. Before applying commands on a live server, verify package names, OS versions, ports, and backup requirements for your own setup.

    Where can I read the original Korean article?

    The original Korean article is available here: Original Korean article.