<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Iulian Popa - Software Engineer Consultant</title>
    <description>I love writing code and working on innovative projects.
 
</description>
    <link>https://iulianpopa.ro/</link>
    <atom:link href="https://iulianpopa.ro/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Sun, 15 Dec 2024 17:30:52 +0200</pubDate>
    <lastBuildDate>Sun, 15 Dec 2024 17:30:52 +0200</lastBuildDate>
    <generator>Jekyll v3.10.0</generator>
    
      <item>
        <title>Install laravel project in a subdirectory with nginx</title>
        <description>&lt;p&gt;We will go over how to install your laravel application under a subdirectory of another website.&lt;/p&gt;

&lt;p&gt;For instance, we might require a second application for a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;blog&lt;/code&gt; to run at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;iulianpopa.ro/blog&lt;/code&gt; if we already have one running at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;iulianpopa.ro&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Although it seems like it should be straightforward, this is actually more complicated and full of perplexing Nginx configurations! An immediate Google search turns up a TON of unclear, ineffective examples.&lt;/p&gt;

&lt;h2 id=&quot;basic-nginx-setup-for-laravel&quot;&gt;Basic nginx setup for Laravel&lt;/h2&gt;

&lt;p&gt;Here is how to set up two Laravel apps to run together when one of them is in a subdirectory of another.&lt;/p&gt;

&lt;p&gt;First let’s take a look on the default laravel nginx config.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;server {
    listen 80;
    server_name main.com;
    root /var/www/main/public;

    add_header X-Frame-Options &quot;SAMEORIGIN&quot;;
    add_header X-XSS-Protection &quot;1; mode=block&quot;;
    add_header X-Content-Type-Options &quot;nosniff&quot;;

    index index.html index.htm index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;what-we-want-to-achieve&quot;&gt;What we want to achieve&lt;/h2&gt;

&lt;p&gt;Let’s say we don’t want to install the blog code inside of our main laravel application. The “blog” application will be at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/var/www/blog&lt;/code&gt;, while the main application is located in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/var/www/main&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This means will start from the basic configuration and extend it to support our second application which lives outside the main directory.&lt;/p&gt;

&lt;p&gt;If we remove the boilerplate from the default nginx vhost configuration, we can better see how nginx and php-fpm pair spin a laravel project.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;server {
    listen 80;
    server_name main.com;
    root /var/www/main/public;

    index index.html index.htm index.php;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This basic nginx configuration doesn’t include everything you may want in a production app, such as cache headers for static assets, but it’s sufficient to get a PHP application up and running.&lt;/p&gt;

&lt;p&gt;These sections of the manual are most important:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;root&lt;/code&gt; - we define the web root that is utilized the main application&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;index&lt;/code&gt; - if no file is mentioned in the URI, we define &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;index.php&lt;/code&gt; as one of the default files to seek for.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;location { try_files ... }&lt;/code&gt; - In a block that captures all URIs, we utilize the tried-and-true try files command. This checks the root directory to determine if a file or directory that was sent in the URI already exists. The index.php file, which is unquestionably located in the root directory, is the next step if it doesn’t already exist.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;adding-an-app-in-a-directory-outside-our-main-project&quot;&gt;Adding an App in a directory outside our main project&lt;/h2&gt;

&lt;p&gt;When attempting to run a second application within a subdirectory outside our main project, we encountered some issues. Let’s go over them. Initially, we have identified that the desired subdirectory for the second application is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/blog&lt;/code&gt; so we create a new location block inside the nginx vhost.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;location /blog {

}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We are aware that the blog project has a distinct web root. Although you might assume that pointing Nginx to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/var/www/blog/public&lt;/code&gt; would work, it actually does not. Instead, we must use an alias.
We are aware that the blog project has a distinct web root. Although you might assume that pointing Nginx to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/var/www/blog/public&lt;/code&gt; would work, it actually does not. Instead, we must use an alias.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;location /blog {
    alias /var/www/blog/public;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To locate a file on the disk drive, Nginx combines the given URI with the root. For instance, if the URI is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/laravel/test&lt;/code&gt;, Nginx will seek to serve &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/var/www/main/public/laravel/test&lt;/code&gt;. As this directory is not present, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;try_files&lt;/code&gt; segment directs Nginx to forward the request to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/var/www/top/public/index.php&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The behavior of an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;alias&lt;/code&gt; is not identical to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;root&lt;/code&gt;. In our case, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/blog&lt;/code&gt; URI is linked to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/var/www/blog/public&lt;/code&gt;. Therefore, a URI of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/blog&lt;/code&gt; will search for a file to serve in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/var/www/blog/public&lt;/code&gt;. On the other hand, a URI of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/blog/laravel&lt;/code&gt; will direct Nginx to search in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/var/www/blog/public/laravel&lt;/code&gt;, without including the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/blog&lt;/code&gt; part.&lt;/p&gt;

&lt;p&gt;Finally we have to add the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;try_files&lt;/code&gt; in order to serve URI like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/blog/laravel&lt;/code&gt; from file &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/var/www/blog/public/index.php&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;location /blog {
    alias /var/www/blog/public;
    try_files $uri $uri/ /index.php$is_args$args;

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We have nested the PHP handler location block inside the location &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/blog {...}&lt;/code&gt; block to allow for some customization.&lt;/p&gt;

&lt;p&gt;It’s worth noting that I included something different in the PHP location block. I adjusted the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SCRIPT_FILENAME&lt;/code&gt; FastCGI parameter to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$request_filename&lt;/code&gt;. By default, this parameter is set to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$document_root$fastcgi_script_name&lt;/code&gt;, which would always point to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/var/www/main/public/index.php&lt;/code&gt; in our configuration - which is not the correct application!&lt;/p&gt;

&lt;p&gt;Therefore, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$request_filename&lt;/code&gt; considers the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;alias&lt;/code&gt; accurately and will transmit the appropriate path to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;index.php&lt;/code&gt; file within our nested application.&lt;/p&gt;

&lt;p&gt;Nevertheless, if our application has a specific route, such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;laravel&lt;/code&gt;, and we attempt to access it via URI &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/blog/laravel&lt;/code&gt;, we encounter a 404 error.&lt;/p&gt;

&lt;p&gt;To get around this issue, we should rewrite the URI like so:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;location @blog {
    rewrite /blog/(.*)$ /blog/index.php last;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;P.S. In some other posts I found around the internet the rewrite block looked something like this:&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;location @blog {
    rewrite /blog/(.*)$ /blog/index.php?/$1 last;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This statement will forward the matched path in the regex to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;index.php&lt;/code&gt; as a query argument.
However, with Laravel, this will append the path as a query string in the following manner: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/blog/laravel/test&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;// laravel
url()-&amp;gt;full()

// returns 
/blog/laravel/test?%2Flaravel%2Ftest
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The Laravel &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;index.php&lt;/code&gt; does not require the path as an argument. Instead, it searches the Server &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Request_URI&lt;/code&gt; for path information. Therefore, a routing like the following should suffice:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;// when fastcgi_params is included;
rewrite ^/demo1/(.*)$ /demo1/index.php last;

// or when don&apos;t have fastcgi_params included
rewrite ^/demo1/(.*)$ /demo1/index.php?$query_string last;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In my view, this is the optimal setup for this scenario since the majority of PHP applications employ a public directory as their web root. This can make it quite complicated to layer application files, where one project’s content is placed inside another. 
Naturally, depending on the application, your situation may differ slightly, but this guide should require minimal modification.&lt;/p&gt;

&lt;p&gt;The final result:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;server {
    listen 80;
    server_name main.com;
    root /var/www/main/public;

    index index.html index.htm index.php;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location /blog {
        alias /var/www/blog/public;
        try_files $uri $uri/ @blog;

        location  ~* \.php$ {
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        }
    }

    location @blog {
        rewrite /blog/(.*)$ /blog/index.php last;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</description>
        <pubDate>Tue, 20 Sep 2022 12:11:06 +0300</pubDate>
        <link>https://iulianpopa.ro/php/2022/09/20/install-laravel-project-in-subdirectory-with-nginx/</link>
        <guid isPermaLink="true">https://iulianpopa.ro/php/2022/09/20/install-laravel-project-in-subdirectory-with-nginx/</guid>
        
        <category>laravel</category>
        
        <category>nginx</category>
        
        <category>subdirectory</category>
        
        
        <category>php</category>
        
      </item>
    
      <item>
        <title>Runing Node JS Server In Background</title>
        <description>&lt;p&gt;When you use Node JS to implement an HTTP / HTTPS web server, in general for development you’re using the basic node or nodemon command to run the webserver. But if the terminal closes or the node process gets killed, the HTTP web server will stop also. 
This article will show a few options we can use in order to avoid such an issue and make the node HTTP web server run in background even if we close the terminal.&lt;/p&gt;

&lt;h4 id=&quot;create-a-server-file-to-start-the-http-web-server&quot;&gt;Create a server file to start the HTTP web server&lt;/h4&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;var http = require(&apos;http&apos;);
var httpServer = http.createServer(function (req, resp) {
    resp.writeHead(200, {&apos;Access-Control-Allow-Origin&apos;:&apos;*&apos;,&apos;Content-Type&apos;: &apos;text/plain&apos;});
    resp.write(&quot;Welcome to www.emag.ro&quot;);
    resp.end();
});

// Start http server listen on port 3000.
httpServer.listen(3000);
console.log(&quot;Use browser to get url &apos;http://localhost:3000/server.js&apos;&quot;);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;open-a-terminal-and-start-the-http-web-server&quot;&gt;Open a terminal and start the HTTP web server&lt;/h4&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;node server.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Open a web browser and browse URL http://localhost:3000, that means the HTTP web server has been started successfully.&lt;/p&gt;

&lt;h3 id=&quot;run-node-in-background---nohup-command&quot;&gt;Run Node In Background - Nohup Command&lt;/h3&gt;

&lt;p&gt;In the above method, the Node JS server will always occupy the terminal until the terminal is closed. But when you close the terminal, the Node JS server will also stop. To fix this issue, you can start the Node JS server with nohup command as below.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;nohup node server.js &amp;gt; prod.log &amp;amp;

ps aux | grep node
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nohup&lt;/code&gt; command will run commands in background, in this case, it will start our Node JS HTTP web server in background and display the node server process id.&lt;/p&gt;

&lt;p&gt;All the server-side output will be saved in prod.log file, but when the node process gets killed, the HTTP server stops also.&lt;/p&gt;

&lt;h3 id=&quot;run-node-in-background---use-node-forever-package&quot;&gt;Run Node In Background - Use Node Forever Package&lt;/h3&gt;

&lt;p&gt;Forever is a Node JS package that can make a node script execute forever even after the node script process is killed. It will execute the node js script in a new process when the old process has been stopped suddenly.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Install Node forever package. Below command will install the forever package globally.&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;npm install forever -g
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ol&gt;
  &lt;li&gt;After installation, run npm list command to see the forever package installation path. Generally npm package is installed in /usr/local/lib/node_modules folder.&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;npm list forever -g
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ol&gt;
  &lt;li&gt;Start node js HTTP web server with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forever start&lt;/code&gt; command. Forever starts the HTTP web server in new process with an id, but if the process gets killed, forever start another process automatically to run the node HTTP web server for us. So the HTTP web server will run in background “forever”.&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;forever start server.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;You can use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forever list&lt;/code&gt; command to list all forever running processes.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forever stop&lt;/code&gt; command can stop the running node script.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;forever stop server.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You can find all the other options forever has on it’s official &lt;a href=&quot;https://github.com/foreversd/forever&quot;&gt;github page&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;run-node-in-background---use-linux-systemd&quot;&gt;Run Node In Background - Use Linux Systemd&lt;/h3&gt;

&lt;p&gt;All the Linux OS contains the systemd daemon which is running in the background when Linux OS startup.
First, go to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/systemd/system&lt;/code&gt; directory and create a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;node-app-service.service&lt;/code&gt; file in this directory.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[Unit]
Description=Run my node js app in background

[Service]

# The path to node app file that will be executed in background when system startup.
ExecStart=/var/www/node-app/app.js
Restart=always
User=nobody

# For Debian/Ubuntu Linux OS uses &apos;nogroup&apos;, for RHEL/Fedora Linux OS uses &apos;nobody&apos; 
Group=nogroup
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/var/www/node-app

[Install]
WantedBy=multi-user.target
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Edit the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/var/www/node-app/app.js&lt;/code&gt; file in vim, add text &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;#!/usr/bin/env node&lt;/code&gt; at the beginning of the file content.&lt;/p&gt;

&lt;p&gt;Add execute permission to the target node js app file &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chmod +x /var/www/node-app/app.js&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Execute &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;systemctl daemon-reload&lt;/code&gt; in a terminal to let systemd load our new service &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;node-app-service.service&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Start the service &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;systemctl start node-app-service&lt;/code&gt;, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;node-app-service&lt;/code&gt; is the service file name as above.&lt;/p&gt;

&lt;p&gt;We can use the command &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;systemctl enable node-app-service&lt;/code&gt; in order to run node in background at system startup.&lt;/p&gt;

&lt;p&gt;The execution logs can be seen running &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;journalctl -u node-app-service&lt;/code&gt; for our node js systemd service.&lt;/p&gt;

&lt;h3 id=&quot;run-node-in-background---use-pm2&quot;&gt;Run Node In Background - Use PM2&lt;/h3&gt;

&lt;p&gt;PM2 is an open source and free to use process manager / monitoring tool. You can run PM2 on all Linux, Windows &amp;amp; macOS.&lt;/p&gt;

&lt;p&gt;This process manager has a built-in load balancer which allow the node js app keep alive forever without downtime even if the server restart.&lt;/p&gt;

&lt;p&gt;To install PM2 globally run the command &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;npm install pm2 -g&lt;/code&gt; in a terminal.&lt;/p&gt;

&lt;p&gt;If you did not install Node js on your server, you can find &lt;a href=&quot;https://www.geeksforgeeks.org/installation-of-node-js-on-linux/&quot;&gt;here&lt;/a&gt; more detailed.&lt;/p&gt;

&lt;p&gt;We can start the nodejs application as a deamon service with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pm2 start app.js&lt;/code&gt;, and in order to monitor logs you cam use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pm2 monit&lt;/code&gt;.&lt;/p&gt;

&lt;h5 id=&quot;other-usefull-commands&quot;&gt;Other usefull commands&lt;/h5&gt;

&lt;p&gt;The pm2 command provides several options for you to manage the processes. The most popular are&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pm2 list
pm2 stop
pm2 restart
pm2 delete
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You can visit the pm2 &lt;a href=&quot;https://pm2.keymetrics.io/&quot;&gt;official website&lt;/a&gt; if you want to learn more about it.&lt;/p&gt;
</description>
        <pubDate>Fri, 04 Jun 2021 12:01:06 +0300</pubDate>
        <link>https://iulianpopa.ro/nodejs/2021/06/04/running-node-js-server-in-background/</link>
        <guid isPermaLink="true">https://iulianpopa.ro/nodejs/2021/06/04/running-node-js-server-in-background/</guid>
        
        <category>nodejs</category>
        
        <category>pm2</category>
        
        <category>systemd</category>
        
        <category>server</category>
        
        
        <category>nodejs</category>
        
      </item>
    
      <item>
        <title>Delete All Data from Redis</title>
        <description>&lt;p&gt;Some advantages of Redis being a NoSQL and in-memory system is that you can perform some tasks very easily compared with relational database systems.&lt;/p&gt;

&lt;p&gt;One such capability is &lt;strong&gt;deleting everything&lt;/strong&gt; in your database or even all databases!&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;NOTE! DO NOT TRY THIS IN PRODUCTION ENV.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;start-redis-instance&quot;&gt;Start Redis instance&lt;/h2&gt;

&lt;p&gt;Using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;redis-server&lt;/code&gt; command from your shell prompt manually start your Redis server.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$  redis-server
14382:C 10 Jan 2021 18:25:12.578 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
14382:C 10 Jan 2021 18:25:12.578 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=14382, just started
14382:C 10 Jan 2021 18:25:12.578 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
14382:M 25 Jun 2021 18:25:12.579 * Increased maximum number of open files to 10032 (it was originally set to 256).
                _._
           _.-``__ &apos;&apos;-._
      _.-``    `.  `_.  &apos;&apos;-._           Redis 5.0.4 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ &apos;&apos;-._
 (    &apos;      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|&apos;` _.-&apos;|     Port: 6379
 |    `-._   `._    /     _.-&apos;    |     PID: 14382
  `-._    `-._  `-./  _.-&apos;    _.-&apos;
 |`-._`-._    `-.__.-&apos;    _.-&apos;_.-&apos;|
 |    `-._`-._        _.-&apos;_.-&apos;    |           https://redis.io
  `-._    `-._`-.__.-&apos;_.-&apos;    _.-&apos;
 |`-._`-._    `-.__.-&apos;    _.-&apos;_.-&apos;|
 |    `-._`-._        _.-&apos;_.-&apos;    |
  `-._    `-._`-.__.-&apos;_.-&apos;    _.-&apos;
      `-._    `-.__.-&apos;    _.-&apos;
          `-._        _.-&apos;
              `-.__.-&apos;

14382:M 25 Jun 2021 18:25:12.581 # Server initialized
14382:M 25 Jun 2021 18:25:12.582 * Ready to accept connections
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;You should see an output from Redis similar with the one above, indicating the server is running and which port it is attached to.&lt;/p&gt;

&lt;h3 id=&quot;accessing-the-redis-command-line-interface&quot;&gt;Accessing the Redis Command Line Interface&lt;/h3&gt;

&lt;p&gt;Redis come with the Redis Command Line Interface, which can be accessed simply by running from the command line the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;redis-cli&lt;/code&gt; command.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ redis-cli
127.0.0.1:6379&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If you were able to connect, you should see the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;redis-cli&lt;/code&gt; prompt with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;host&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;port&lt;/code&gt; specified, as seen above.&lt;/p&gt;

&lt;h3 id=&quot;deleting-a-single-database-from-redis&quot;&gt;Deleting a single database from Redis&lt;/h3&gt;

&lt;p&gt;In Redis if you have multiple databases, you can access them by their unique &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;index&lt;/code&gt; number.&lt;/p&gt;

&lt;p&gt;You may connect to a different database by entering the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;select index&lt;/code&gt; command:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;127.0.0.1:6379&amp;gt; select 1
OK
127.0.0.1:6379[1]&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Notice that the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;redis-cli&lt;/code&gt; prompt now indicates you are connected to database &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[1]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now in order to destroy a the database, we can issue the FLUSHDB command:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;127.0.0.1:6379&amp;gt; select 1
OK
127.0.0.1:6379[1]&amp;gt; FLUSHDB
OK
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;deleting-all-databases-from-redis&quot;&gt;Deleting ALL Databases from Redis&lt;/h3&gt;

&lt;p&gt;If you just played around with Redis and wish to destroy everything in the end, you can use the FLUSHALL command:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;127.0.0.1:6379&amp;gt; FLUSHALL
OK
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Very simple, but that is how you can quickly delete everything in Redis.&lt;/p&gt;
</description>
        <pubDate>Sun, 10 Jan 2021 11:31:04 +0200</pubDate>
        <link>https://iulianpopa.ro/redis/2021/01/10/delete-all-data-from-redis/</link>
        <guid isPermaLink="true">https://iulianpopa.ro/redis/2021/01/10/delete-all-data-from-redis/</guid>
        
        <category>redis</category>
        
        <category>redis-cli</category>
        
        <category>data</category>
        
        
        <category>redis</category>
        
      </item>
    
      <item>
        <title>Optimize memory usage of Redis instance</title>
        <description>&lt;p&gt;Sometimes &lt;strong&gt;Redis keys&lt;/strong&gt; can increase the memory consumption for our Redis instances.
Usually, we should prefer descriptive keys while using Redis, but if we have a very big dataset with millions of long keys then these keys will take a lot of money from our pocket.&lt;/p&gt;

&lt;h2 id=&quot;use-smaller-keys-with-redis&quot;&gt;Use Smaller Keys with Redis&lt;/h2&gt;

&lt;p&gt;If the application was well written, switching to smaller keys usually involves updating a few configurations in the application code.&lt;/p&gt;

&lt;p&gt;First we will have to identify all the big keys in our Redis instance and shorten it by removing extra characters from it.&lt;/p&gt;

&lt;p&gt;The easyest way to find what keys are very big in our application is to run the command &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;redis-cli --bigkeys&lt;/code&gt;. This command will scan a certain set of records and return the big keys from that set.&lt;/p&gt;

&lt;h2 id=&quot;advantages&quot;&gt;Advantages&lt;/h2&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;Let’s say we have &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;100,000,000 keys&lt;/code&gt; named like this:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;some-descriptive-large-keyname (30 characters)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If we change the key name into a smaller one we will have something like this:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;some-keyname (12 characters)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Just by doing this we save &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;16 chars&lt;/code&gt; which means &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;16 bytes&lt;/code&gt;. If we make a small calculation with this values we end up by saving &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;100,000,000*16 = 1.6GB&lt;/code&gt; of RAM!&lt;/p&gt;

&lt;h2 id=&quot;disadvantages&quot;&gt;Disadvantages&lt;/h2&gt;

&lt;p&gt;Because we made the key smaller it’s possible that it becomes less descriptive, but in some situations this isn’t a very big issue comparing to your cost savings. 
Also you could have a readable mapping for each key in your source code and this issue would be solved.&lt;/p&gt;
</description>
        <pubDate>Mon, 04 Jan 2021 09:30:00 +0200</pubDate>
        <link>https://iulianpopa.ro/redis/2021/01/04/optimize-memory-usage-of-redis-instance/</link>
        <guid isPermaLink="true">https://iulianpopa.ro/redis/2021/01/04/optimize-memory-usage-of-redis-instance/</guid>
        
        <category>redis</category>
        
        <category>redis-cli</category>
        
        <category>memory</category>
        
        
        <category>redis</category>
        
      </item>
    
      <item>
        <title>How can you create a superuser in MySQL?</title>
        <description>&lt;p&gt;This tutorial will show you how to set up a new MySQL account as a super user with privileged access to the databases.&lt;/p&gt;

&lt;h3 id=&quot;you-must-first-log-in-as-the-root-user-who-has-access-to-the-create-user-capability&quot;&gt;You must first log in as the root user, who has access to the CREATE USER capability.&lt;/h3&gt;

&lt;p&gt;To create a new user with a password, enter the following command:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;CREATE USER &apos;johndoe&apos;@&apos;localhost&apos; IDENTIFIED BY &apos;secret_pass&apos;;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The new user currently has no access rights to the databases. The new user’s privileges must then be granted, which comes next. The following privileges are available to users:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;ALL PRIVILEGES - gives complete root access to the databases, if no database is specified, will have global access to it.&lt;/li&gt;
  &lt;li&gt;CREATE – build new databases or tables&lt;/li&gt;
  &lt;li&gt;DROP - remove databases or tables&lt;/li&gt;
  &lt;li&gt;DELETE - remove rows from tables&lt;/li&gt;
  &lt;li&gt;INSERT - adding rows to tables&lt;/li&gt;
  &lt;li&gt;SELECT - to browse databases, use the SELECT command.&lt;/li&gt;
  &lt;li&gt;UPDATE - update table rows using UPDATE&lt;/li&gt;
  &lt;li&gt;GRANT OPTION: Add or remove rights for other users.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;create-a-superuser&quot;&gt;Create a superuser.&lt;/h3&gt;

&lt;p&gt;We must provide this new user complete root access to the whole database in order to transform it into a superuser, which entails GRANTING ALL PRIVILEGES:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;GRANT ALL PRIVILEGES ON *.* TO &apos;johndoe&apos;@&apos;localhost&apos; WITH GRANT OPTION;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The new user now has root-like permission, therefore the work is complete.&lt;/p&gt;

&lt;h3 id=&quot;then-make-a-second-account-with-an-identical-username-and-grant-it-full-root-access&quot;&gt;Then make a second account with an identical username and grant it full root access:&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;CREATE USER &apos;johndoe&apos;@&apos;%&apos; IDENTIFIED BY &apos;secret_pass&apos;;

GRANT ALL PRIVILEGES ON *.* TO &apos;johndoe&apos;@&apos;%&apos; WITH GRANT OPTION;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Both superuser accounts “johndoe”@”localhost” and “johndoe”@”%” have full control over everything.&lt;/p&gt;

&lt;p&gt;It is only possible to connect using the ‘johndoe’@’localhost’ account from a local host. The account “johndoe”@”%” employs the wildcard “%” in the host part, allowing connections from any host.&lt;/p&gt;

&lt;h3 id=&quot;use-the-show-grants-command-to-confirm-the-permissions-granted-to-the-new-user&quot;&gt;Use the SHOW GRANTS command to confirm the permissions granted to the new user:&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;SHOW GRANTS FOR johndoe;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;reload-all-the-privileges-once-everything-is-in-order-and-each-adjustment-to-the-users-will-become-effective-right-away&quot;&gt;Reload all the privileges once everything is in order and each adjustment to the users will become effective right away.&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;FLUSH PRIVILEGES;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</description>
        <pubDate>Sat, 11 Jul 2020 08:02:00 +0300</pubDate>
        <link>https://iulianpopa.ro/database/2020/07/11/how-can-you-create-a-super-user-in-mysql/</link>
        <guid isPermaLink="true">https://iulianpopa.ro/database/2020/07/11/how-can-you-create-a-super-user-in-mysql/</guid>
        
        <category>mysql</category>
        
        <category>create</category>
        
        <category>user</category>
        
        
        <category>database</category>
        
      </item>
    
      <item>
        <title>Import Excel data into MySQL</title>
        <description>&lt;p&gt;Recently I had to import an Excel document into a MySQL database so I want to show you how I did this.&lt;/p&gt;

&lt;p&gt;We will do this in 4 steps:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;Open your file and click Save As. Choose to save it as a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.csv&lt;/code&gt; (Comma Separated) file. 
If you are running Excel &lt;strong&gt;on a Mac&lt;/strong&gt;, in order to maintain the correct formatting, you have to save the file as a &lt;strong&gt;Windows Comma Separated (.csv)&lt;/strong&gt; or &lt;strong&gt;CSV (Windows)&lt;/strong&gt;.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;After you saved it as a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.csv&lt;/code&gt; file log into your MySQL and create a brand new database.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
  &lt;p&gt;Note! The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--local-infile&lt;/code&gt; option is needed by some MySQL versions for the data loading we’ll do in the following steps.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ mysql -u root -p --local-infile
mysql&amp;gt; create database books;
mysql&amp;gt; use books;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ol&gt;
  &lt;li&gt;Next define the schema for our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;books&lt;/code&gt; table using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CREATE TABLE&lt;/code&gt; command. For more details, see the (MySQL documentation)[https://dev.mysql.com/doc/refman/8.0/en/creating-tables.html].&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;CREATE TABLE books (
    id INT NOT NULL PRIMARY KEY,
    title VARCHAR(40),
    author_id INT,
    created_at DATE
    price FLOAT
);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ol&gt;
  &lt;li&gt;Now that your table is created, the data from your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;csv&lt;/code&gt; can be imported using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LOAD DATA&lt;/code&gt; command.&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;LOAD DATA LOCAL INFILE &quot;/home/books.csv&quot; INTO TABLE library_db.books
    FIELDS TERMINATED BY &apos;,&apos;
    LINES TERMINATED BY &apos;\n&apos;
    IGNORE 1 LINES 
    (id, title, author_id, @created_date, price)
    set created_at = STR_TO_DATE(@created_date,&apos;%m/%d/%Y&apos;);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</description>
        <pubDate>Sun, 14 Jun 2020 12:02:00 +0300</pubDate>
        <link>https://iulianpopa.ro/database/2020/06/14/import-excel-data-into-mysql/</link>
        <guid isPermaLink="true">https://iulianpopa.ro/database/2020/06/14/import-excel-data-into-mysql/</guid>
        
        <category>mysql</category>
        
        <category>csv</category>
        
        <category>excel</category>
        
        <category>import data</category>
        
        
        <category>database</category>
        
      </item>
    
      <item>
        <title>Find the table size in MySQL</title>
        <description>&lt;p&gt;MySQL provides useful metadata about your database itself. While most other databases refer to this information as a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;catalog&lt;/code&gt;, the (official MySQL documentation)[https://dev.mysql.com/doc/refman/8.0/en/information-schema.html] refers to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INFORMATION_SCHEMA&lt;/code&gt; metadata as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tables&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The important information provided by these &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INFORMATION_SCHEMA&lt;/code&gt; tables can help us determine how much disk space takes a specific table from our database.&lt;/p&gt;

&lt;h2 id=&quot;list-database-all-tables-sizes&quot;&gt;List Database All Tables Sizes&lt;/h2&gt;

&lt;p&gt;If we take a look in the documentation the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INFORMATION_SCHEMA.TABLES&lt;/code&gt; table contains around 20 columns, but for the purpose of determining the amount of disk space used by tables, we’ll focus on two columns in particular: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DATA_LENGTH&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INDEX_LENGTH&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DATA_LENGTH&lt;/code&gt; is the size in bytes of all data in the table.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INDEX_LENGTH&lt;/code&gt; is the size in bytes of the index file for the table.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Having this information in mind, we can create a query that will list all tables in a specific database along with the disk space of each.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;SELECT
  TABLE_NAME AS `Table`,
  ROUND((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024) AS `Size (MB)`
FROM
  information_schema.TABLES
WHERE
  TABLE_SCHEMA = &quot;library&quot;
ORDER BY
  (DATA_LENGTH + INDEX_LENGTH)
DESC;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Because the size it is stored in bytes I convert it into something more useful and understandable: &lt;strong&gt;megabytes&lt;/strong&gt; (MB).&lt;/p&gt;

&lt;p&gt;In this example we are using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;library&lt;/code&gt; database by combining the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DATA_LENGTH&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INDEX_LENGTH&lt;/code&gt; as bytes, then dividing it by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1024&lt;/code&gt; twice to convert into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kilobytes&lt;/code&gt; and then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;megabytes&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The result will look something like this:&lt;/p&gt;

&lt;p&gt;+————————————-+———–+
| Table                               | Size (MB) |
+————————————-+———–+
| books                               |       546 |
| authors                             |       123 |
| clients                             |        56 |
…&lt;/p&gt;

&lt;h2 id=&quot;list-database-table-size&quot;&gt;List Database Table Size&lt;/h2&gt;

&lt;p&gt;If you don’t care about all tables in the database and only want the size of a particular table, you can simply add &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AND TABLE_NAME = &quot;table_name&quot;&lt;/code&gt; to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;WHERE clause&lt;/code&gt;. Here we only want information about the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;authors&lt;/code&gt; table:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;SELECT
  TABLE_NAME AS `Table`,
  ROUND((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024) AS `Size (MB)`
FROM
  information_schema.TABLES
WHERE
    TABLE_SCHEMA = &quot;library&quot;
  AND
    TABLE_NAME = &quot;authors&quot;
ORDER BY
  (DATA_LENGTH + INDEX_LENGTH)
DESC;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The result will be:&lt;/p&gt;

&lt;p&gt;+———-+———–+
| Table    | Size (MB) |
+———-+———–+
| authors  |       123 |
+———-+———–+
1 row in set (0.00 sec)&lt;/p&gt;

&lt;p&gt;If you have multiple databases and you want to see which table from which database is the biggest in size, you will find it useful to query for the size of all tables within all databases from your system. 
This can be easily done with another similar query:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;SELECT
  TABLE_SCHEMA AS `Database`,
  TABLE_NAME AS `Table`,
  ROUND((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024) AS `Size (MB)`
FROM
  information_schema.TABLES
ORDER BY
  (DATA_LENGTH + INDEX_LENGTH)
DESC;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This will return the database name in which the table is, the table name and the size of it.&lt;/p&gt;
</description>
        <pubDate>Fri, 12 Jun 2020 09:46:00 +0300</pubDate>
        <link>https://iulianpopa.ro/database/2020/06/12/find-the-table-size-in-mysql/</link>
        <guid isPermaLink="true">https://iulianpopa.ro/database/2020/06/12/find-the-table-size-in-mysql/</guid>
        
        <category>mysql</category>
        
        <category>tables</category>
        
        <category>metadata</category>
        
        <category>information schema</category>
        
        
        <category>database</category>
        
      </item>
    
      <item>
        <title>Backup your MySQL database</title>
        <description>&lt;p&gt;I recommend that you set up a backup for any database that you have. This guarantees your production data will be safe and you don’t loose data in case of a desaster. Below, I’ve sketched out easy process of setting up a backup with regular data dumps.&lt;/p&gt;

&lt;h2 id=&quot;create-the-backup&quot;&gt;Create the backup&lt;/h2&gt;

&lt;p&gt;I advice you to always prefix your backups in some meaningfull way, for example you could sufix it with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_backup_&lt;/code&gt; followed by the current date and time.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ mysql -u root -p
mysql&amp;gt; CREATE DATABASE db_name_backup_06_10_2020;
mysql&amp;gt; exit
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;load-data-from-old-database-into-new-database&quot;&gt;Load data from old database into new database&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ mysqldump -u root -password=secure_password db_name | mysql -u root -password=my_password db_name_backup_2020_10_06
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;automate-the-backup&quot;&gt;Automate the backup&lt;/h2&gt;

&lt;p&gt;If you want to get it to the next step you could also create a shell script to do the job for you. Shell scripts allow you to easily automate tasks in Linux.&lt;/p&gt;

&lt;h3 id=&quot;create-shell-script&quot;&gt;Create Shell Script&lt;/h3&gt;

&lt;p&gt;Open terminal and run the following command to create an empty shell script file.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ vim /home/db_backup.sh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;add-shell-script-to-backup-mysql&quot;&gt;Add shell script to backup MySQL&lt;/h3&gt;

&lt;p&gt;Add the following code to your shell script file. Replace dbname, dbuser and dbpass with your database name, username and password respectively.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;#!/bin/sh

echo &quot;Backup starting...&quot;
sufix=&quot;backup_$(date +&apos;%m-%d-%Y&apos;)&quot;
db_backup=&quot;db_name_${sufix}.sql&quot;
sudo mysqldump  -u db_user -p db_pass --no-tablespaces db_name  &amp;gt;/home/${db_backup}
echo &quot;Backup complete!&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;First define the execution environment for shell script and echo a message that indicates the backup has started. 
Then create a sufix variable to store current date &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;backup_06_10_2020&lt;/code&gt;.
Use this variable to define the backup file name (.sql) for the database backup. 
Finally, run the MySQL command to backup database, store it in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/home&lt;/code&gt; folder and echo the message that database backup is complete.&lt;/p&gt;

&lt;p&gt;If you want to compress the backup data, you can modify the mysqldump command and basically pipe the output to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gzip&lt;/code&gt; command to create the gzip file. Also you have to modify the final file name to be a .gz file instead of a .sql file.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sufix=&quot;_backup_$(date +&apos;%m-%d-%Y&apos;)&quot;
db_backup=&quot;db_name_${sufix}.gz&quot;
sudo mysqldump  -u db_user -p db_pass --no-tablespaces db_name | gzip -c &amp;gt;/home/${db_backup}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;make-the-script-executable&quot;&gt;Make the script executable&lt;/h3&gt;

&lt;p&gt;To make your shell script executable run the following command.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ sudo chmod +x /home/db_backup.sh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;test-the-script&quot;&gt;Test the script&lt;/h3&gt;

&lt;p&gt;Execute the shell script using the following command.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ sudo /home/db_backup.sh
Backup starting...
Backup complete!
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;schedule-it-as-a-cron-comand&quot;&gt;Schedule it as a cron comand&lt;/h3&gt;

&lt;p&gt;Now we can add the above command as a cronjob to regularly take database backup.&lt;/p&gt;

&lt;p&gt;Open crontab file with the following command.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ sudo crontab -e
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Add the following line to it. It will run your db_backup.sh script everyday at 10.a.m. You can change it as per your requirement.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0 2 * * * sudo /home/db_backup.sh &amp;gt;/dev/null 2&amp;gt;&amp;amp;1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Save and close the file by pressing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esc + :wq&lt;/code&gt;. You can check if you correctly saved the file with the command.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ sudo crontab -l
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In the above code “0 2 * * *” indicates that the cron job should be run every day at 2:00AM.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sudo /home/db_backup.sh&lt;/code&gt; is your command.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;gt; /dev/null 2&amp;gt;&amp;amp;1&lt;/code&gt; indicates that all standard output and error messages must be sent to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/null&lt;/code&gt;, meaning that should not be displayed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To avoid &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file not found&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;permission denied&lt;/code&gt; errors make sure you write the full path to your shell script and that you use sudo to schedule the cronjob.&lt;/p&gt;

&lt;p&gt;That’s all, you have learnt how to create a shell script to backup MySQL database. Also you automated the process by creating a cronjob to run the shell script every day. 
If you run the above shell script everyday as a cronjob, it will create different backup files, one for each day of the month.&lt;/p&gt;
</description>
        <pubDate>Wed, 10 Jun 2020 09:30:00 +0300</pubDate>
        <link>https://iulianpopa.ro/database/2020/06/10/bacup-your-mysql-database/</link>
        <guid isPermaLink="true">https://iulianpopa.ro/database/2020/06/10/bacup-your-mysql-database/</guid>
        
        <category>mysql</category>
        
        <category>backup</category>
        
        <category>cronjob</category>
        
        
        <category>database</category>
        
      </item>
    
      <item>
        <title>Use firewalld-cmd to manage your firewall</title>
        <description>&lt;p&gt;Firewalld is a firewall management solution available for many Linux distributions. It is a nice tool that offers a friendly interface for the iptables packet filtering system provided by the Linux kernel.&lt;/p&gt;

&lt;p&gt;In this post, we will cover how to set up a firewall for your server and show you the basics of managing the firewall with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;firewall-cmd&lt;/code&gt; administrative tool.&lt;/p&gt;

&lt;p&gt;The command-line tool &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;firewall-cmd&lt;/code&gt; is part of the firewalld application. It can be used to make permanent and non-permanent runtime changes.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;### Installing firewalld
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;By default, firewalld is included, but in case it is not installed, you can always install it using yum or apt package managers.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# yum install -y firewalld
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To enable the firewalld you should run:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# systemctl enable firewalld
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As you already anticipating you can restart the firewalld service like so:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# systemctl restart firewalld
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;A list of all the available options with firewall-cmd command could be found by running the help command:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# firewall-cmd --help

Usage: firewall-cmd [OPTIONS...]

General Options
  -h, --help           Prints a short help text and exists
  -V, --version        Print the version string of firewalld
  -q, --quiet          Do not print status messages

Status Options
  --state                  Return and print firewalld state
  --reload                 Reload firewall and keep state information
  --complete-reload        Reload firewall and lose state information
  --runtime-to-permanent   Create permanent from runtime configuration

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The firewall-cmd command has a lot of options that you could use, such as General, Status, Permanent, Zone, IcmpType, Service, Adapt and Query Zones, Direct, Lockdown, Lockdown Whitelist, and Panic. For more information about each option you could refer to the firewall-cmd man page.&lt;/p&gt;

&lt;p&gt;The more important and used commands are the following.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;List all zones&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Use the following command to list information for all zones.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# firewall-cmd --list-all-zones

block
  target: %%REJECT%%
  icmp-block-inversion: no
  interfaces:
  sources:
  services:
  ports:
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

drop
  target: DROP
  icmp-block-inversion: no
  interfaces:
  sources:
  services:
  ports:
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

internal (active)
  target: default
  icmp-block-inversion: no
  interfaces:
  sources: 192.168.XXX.XXX
  services: dhcpv6-client mdns samba-client ssh
  ports: 4730/tcp
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth0
  sources:
  services: dhcpv6-client http https ssh
  ports:
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Public is the default zone set, if you do not change it. To check the currently set default zone use the below command:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# firewall-cmd --get-default-zone

public
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ol&gt;
  &lt;li&gt;List allowed service and ports on the system&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To show currently allowed service on your system use the below command.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# firewall-cmd --list-services

dhcpv6-client https ssh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You can check the list of ports that are open on your system.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# firewall-cmd --list-ports
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ol&gt;
  &lt;li&gt;To Enable all the incoming ports for a service&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You can also open the required ports for a service by using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-–add-service option&lt;/code&gt;. 
Let’s give access by HTTP clients for the public zone.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# firewall-cmd --zone=public --add-service=http

success
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To list services that are allowed for the public zone:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# firewall-cmd --zone=public --list-services

dhcpv6-client http https ssh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Using this command only changes the Runtime configuration and does not update the configuration files. 
If we restart the firewalld service we can notice that the configuration changes made in Runtime configuration mode are lost.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# systemctl restart firewalld
# firewall-cmd --zone=public --list-services

dhcpv6-client ssh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To make changes permanent, use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-–permanent&lt;/code&gt; option. Example:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# firewall-cmd --permanent --zone=public --add-service=http

success
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Changes made in Permanent configuration mode are not implemented immediately. Example:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# firewall-cmd --zone=public --list-services

dhcpv6-client ssh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;However, changes made in a Permanent configuration are written to configuration files which means are persistent and 
even if we restart the firewalld service, it will read the configuration files and implements the changes.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# systemctl restart firewalld
# firewall-cmd --zone=public --list-services

dhcpv6-client http ssh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ol&gt;
  &lt;li&gt;Allow traffic on an incoming port&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The command below will open the port 2222 effective immediately:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# firewall-cmd --add-port=[PORT]/tcp
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;But as with the services, the changes will not persist across reboots&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# firewall-cmd --add-port=2222/tcp
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To create a persistent rule use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--permanent&lt;/code&gt; flag again, but will not take effect immediately:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# firewall-cmd --permanent --add-port=2222/tcp
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You can list the open ports like so:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# firewall-cmd –-list-ports

2222/tcp
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ol&gt;
  &lt;li&gt;Start and stop firewalld service&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;As with any other linux service, in order to start/stop/status firewalld service use the following commands:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# systemctl start firewalld.service
# systemctl stop firewalld.service
# systemctl status firewalld.service
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</description>
        <pubDate>Fri, 14 Jun 2019 12:01:06 +0300</pubDate>
        <link>https://iulianpopa.ro/linux/2019/06/14/firewalld-cmd/</link>
        <guid isPermaLink="true">https://iulianpopa.ro/linux/2019/06/14/firewalld-cmd/</guid>
        
        <category>linux</category>
        
        <category>firewall</category>
        
        <category>firewalld</category>
        
        <category>iptables</category>
        
        
        <category>linux</category>
        
      </item>
    
      <item>
        <title>Understanding shell script redirect output</title>
        <description>&lt;p&gt;The redirection capabilities built into Linux environment provides a robust set of tools used to make all sorts of 
tasks easier to accomplish. Knowing how they work and how they can help to manipulate the I/O (Input/Output) streams 
will greatly increase your productivity.&lt;/p&gt;

&lt;h3 id=&quot;standard-streams&quot;&gt;Standard streams&lt;/h3&gt;

&lt;p&gt;Input and output in the Linux environment is distributed across three streams. These streams are:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;stdin (the keyboard)&lt;/li&gt;
  &lt;li&gt;stdout (the screen)&lt;/li&gt;
  &lt;li&gt;stderr (error messages output to the screen)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The streams are also numbered:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;stdin (0)&lt;/li&gt;
  &lt;li&gt;stdout (1)&lt;/li&gt;
  &lt;li&gt;stderr (2)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;introduction-to-stream-redirection&quot;&gt;Introduction to stream redirection&lt;/h3&gt;

&lt;p&gt;Redirection simply means capturing output from a file, command, program, script, or even code block within a script
and sending it to another place being it a file, a command or a script.&lt;/p&gt;

&lt;h4 id=&quot;overwrite-the-destinations-existing-contents&quot;&gt;Overwrite the destination’s existing contents.&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;&lt;/code&gt; - standard input&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;gt;&lt;/code&gt; - standard output&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&amp;gt;&lt;/code&gt; - standard error&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;append-to-the-destinations-existing-contents&quot;&gt;Append to the destination’s existing contents.&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;gt;&amp;gt;&lt;/code&gt; - standard output&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;&amp;lt;&lt;/code&gt; - standard input&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&amp;gt;&amp;gt;&lt;/code&gt; - standard error&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;Note! If you want to try the examples by yourself first create a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frameworks.txt&lt;/code&gt; file with your favourite frameworks.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;By default when we use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cat&lt;/code&gt; command to look inside of a file, the output will be printed on the screen:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ cat frameworks.txt
Symfony
Laravel
Zend
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;But sometimes is useful to redirect this output somewhere else, like a different file, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;output.txt&lt;/code&gt; in our short example:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ cat frameworks.txt &amp;gt; output.txt

$ cat output.txt
Symfony
Laravel
Zend
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As you can see when the first command is executed there is no output on the screen. That’s because we redirected the 
standard output (stdout) to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;output.txt&lt;/code&gt; file, so it is not printed on the screen anymore.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Note that even that the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;output.txt&lt;/code&gt; file didn’t existed, it was created prior to writing, exactly like you’ll do 
it with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;touch&lt;/code&gt; command.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Another important stream called standard error (stderr), is the place to where programs can send their error 
messages. To easily see this, let’s cat my personal &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;passwords.txt&lt;/code&gt; file and redirect the output to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;output.txt&lt;/code&gt;
file exactly like before:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ cat passwords.txt &amp;gt; output.txt
cat: passwords.txt: No such file or directory
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Even if we redirect the &lt;em&gt;stdout&lt;/em&gt; to a file, we can see the error output on the screen, because we are redirecting 
just the standard output, not the stderr.&lt;/p&gt;

&lt;p&gt;To accomplish the exact same thing for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stderr&lt;/code&gt; as with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stdout&lt;/code&gt; we have to use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&amp;gt;&lt;/code&gt; instead of simple &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ cat passworkds.txt 2&amp;gt; output.txt
$ cat output.txt
cat: passworkds.txt: No such file or directory
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;file-descriptors&quot;&gt;File descriptors&lt;/h3&gt;

&lt;p&gt;In order to understand why errors are redirected with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&amp;gt;&lt;/code&gt;, like in our previous example, we have to know about 
descriptors.&lt;/p&gt;

&lt;p&gt;A file descriptor is nothing more than a positive integer that represents an open file. 
The Standard Input, Standard Output and Standard Error are the default file descriptors where a process can write output.&lt;/p&gt;

&lt;p&gt;Shortly, we can identify those locations with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ids&lt;/code&gt;, and it will always be 0 for stdin, 1 for stdout and 2 for stderr.&lt;/p&gt;

&lt;h4 id=&quot;making-use-of-descriptors&quot;&gt;Making use of descriptors&lt;/h4&gt;

&lt;p&gt;Going back to our first example, when we redirected the output of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cat frameworks.txt&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;output.txt&lt;/code&gt;, we could 
rewrite the command with descriptors:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ cat frameworks.txt 1&amp;gt; output.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt; is just the file descriptor for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stdout&lt;/code&gt;. The syntax for redirecting is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[FILE_DESCRIPTOR]&amp;gt;&lt;/code&gt;. Omitting the file 
descriptor from the command is just a nice shortcut to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now that everything is more clear, you now understand why earlier the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&amp;gt;&lt;/code&gt; worked to redirect the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stderr&lt;/code&gt; to the file.&lt;/p&gt;

&lt;h3 id=&quot;21-meaning&quot;&gt;2&amp;gt;&amp;amp;1 meaning&lt;/h3&gt;

&lt;p&gt;By now you already know half of the command &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&amp;gt;&amp;amp;1&lt;/code&gt;. We are trying to say redirect the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stderr&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;amp;1&lt;/code&gt;. Finally, the 
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;amp;1&lt;/code&gt; is used to reference the value of the file descriptor 1 (stdout). 
That means when we’re using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&amp;gt;&amp;amp;1&lt;/code&gt; we are basically redirecting the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stderr&lt;/code&gt; to the same place as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stdout&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ cat frameworks.txt &amp;gt; output.txt 2&amp;gt;&amp;amp;1
$ cat output.txt
Symfony
Laravel
Zend
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ cat passwords.txt &amp;gt; output.txt 2&amp;gt;&amp;amp;1
$ cat output.txt
cat: passwords.txt: No such file or directory
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;conclusions&quot;&gt;Conclusions&lt;/h3&gt;

&lt;p&gt;In Linux there are two streams where programs send output to: Standard output (stdout) and Standard Error (stderr). 
We can redirect these outputs to a different location, a file, a command or a script.&lt;/p&gt;

&lt;p&gt;We can use file descriptors to identify stdin (0), stdout (1) and stderr (2) and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;amp;[FILE_DESCRIPTOR]&lt;/code&gt; to 
reference a file descriptor value.&lt;/p&gt;

&lt;p&gt;Finally using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&amp;gt;&amp;amp;1&lt;/code&gt; will redirect stderr to exact same place stdout is being sent.&lt;/p&gt;
</description>
        <pubDate>Thu, 06 Jun 2019 12:01:06 +0300</pubDate>
        <link>https://iulianpopa.ro/linux/2019/06/06/understanding-shell-script-redirect-output/</link>
        <guid isPermaLink="true">https://iulianpopa.ro/linux/2019/06/06/understanding-shell-script-redirect-output/</guid>
        
        <category>linux</category>
        
        <category>shell</category>
        
        <category>logs</category>
        
        <category>redirect</category>
        
        <category>output</category>
        
        
        <category>linux</category>
        
      </item>
    
  </channel>
</rss>
