<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: HakamRaza</title>
    <description>The latest articles on Forem by HakamRaza (@hakamraza).</description>
    <link>https://forem.com/hakamraza</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F419295%2F571f0e50-6dea-4790-bfdf-060819f5a8a8.jpg</url>
      <title>Forem: HakamRaza</title>
      <link>https://forem.com/hakamraza</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/hakamraza"/>
    <language>en</language>
    <item>
      <title>[Laravel] Solving Bottleneck in Laravel.</title>
      <dc:creator>HakamRaza</dc:creator>
      <pubDate>Mon, 18 Dec 2023 08:03:28 +0000</pubDate>
      <link>https://forem.com/hakamraza/laravel-solving-bottleneck-in-laravel-1mea</link>
      <guid>https://forem.com/hakamraza/laravel-solving-bottleneck-in-laravel-1mea</guid>
      <description>&lt;p&gt;&lt;strong&gt;Intro&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;this is based on my experience in solving bottlenecks in Laravel projects hosted in AWS EC2. Most of this is simple steps that can be taken on the dot.&lt;/li&gt;
&lt;li&gt;this will not cover everything, but at least give you some solutions that maybe useful.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;Memory Bottleneck&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Scaling your server vertically may not be the solution.&lt;/li&gt;
&lt;li&gt;There maybe a situation when your project running slow, but the EC2 CPU% chart is still showing below 50%. Then, it is time to check your server memory. You see, EC2 does not monitor your memory (RAM) usage by default.&lt;/li&gt;
&lt;li&gt;EC2 also does not 'shared' your memory to another EC2 although behind the same load balancer. i.e, a process can only be compute in one server that execute it. This is an issue if you load everything (eg: &lt;code&gt;User::all()&lt;/code&gt;) into memory.&lt;/li&gt;
&lt;li&gt;you usually only able to check memory available by using command inside the ec2 itself:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cat /proc/meminfo | grep Mem
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Try to estimate the memory you needed and scale appropriately:&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;1 php-fpm process size: ~ 40mb - max 80mb RAM&lt;br&gt;
1 queue worker running: ~ 140mb RAM&lt;br&gt;
Standard laravel process: ~ 2mb - 16mb RAM&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;&lt;strong&gt;Database Bottleneck&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;although there is automated server auto-scaling, there is none for AWS RDS, which require manual scaling/upgrade.&lt;/li&gt;
&lt;li&gt;new spawn server means additional connection to your DB.&lt;/li&gt;
&lt;li&gt;Amount of connection a DB can handle mainly depend on the amount of RAM the DB has.&lt;/li&gt;
&lt;li&gt;Any RDS upgrade/scaling operations require 30 mins DB down time. Which means you cannot access your DB during this time.&lt;/li&gt;
&lt;li&gt;Estimate appropriately, for example a &lt;code&gt;db.t3.large (8GB)&lt;/code&gt; can handle up to 600 connections&lt;/li&gt;
&lt;li&gt;RDS Monitoring tools can be use to monitor CPU usage, query depth and so on.&lt;/li&gt;
&lt;li&gt;Other steps that can be taken includes:&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Index columns with frequent filter / querying / joining&lt;/li&gt;
&lt;li&gt;Using aggregate tables (for aggregate summary) instead of query multiple tables for multiple aggregate metrics&lt;/li&gt;
&lt;li&gt;Using aggregate function such as &lt;code&gt;withCount&lt;/code&gt; to get directly the count instead of loading the whole relationship children. &lt;/li&gt;
&lt;li&gt;Read and write using separate database that syncing
&lt;/li&gt;
&lt;li&gt;Cache common and frequent queries such as list of countries, phone codes, etc.&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;&lt;strong&gt;Application Improvement&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Select only columns required to reduce memory consumption&lt;/li&gt;
&lt;li&gt;Use eager load moderately as not everything are actually needed on the same time. Utilize lazy loading instead, where for additional information, forward user to another page (new request) instead of eager loading all at once.&lt;/li&gt;
&lt;li&gt;Using raw queries instead of eloquent maybe a hassle but that depending on what information you want to fetch. Raw queries use less memory due to no need to load additional methods or properties come with eloquent&lt;/li&gt;
&lt;li&gt;Utilize jobs for faster response (depend on your design) and control your application compute and memory consumption when executing something.&lt;/li&gt;
&lt;/ol&gt;




</description>
    </item>
    <item>
      <title>[Linux] Setting Up MailHog</title>
      <dc:creator>HakamRaza</dc:creator>
      <pubDate>Fri, 08 Sep 2023 03:52:44 +0000</pubDate>
      <link>https://forem.com/hakamraza/linux-setting-up-mailhog-3o5f</link>
      <guid>https://forem.com/hakamraza/linux-setting-up-mailhog-3o5f</guid>
      <description>&lt;p&gt;&lt;a href="https://github.com/mailhog/MailHog/tree/master"&gt;MailHog&lt;/a&gt; is an email testing tool for developers. It almost like &lt;a href="https://mailtrap.io/home"&gt;Mailtrap&lt;/a&gt; but this one is hosted in your own server. This give more privacy and freedom when you are testing your mail notifications.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Caution : This project has been abandoned but it's okay, we have alternative, &lt;a href="https://github.com/axllent/mailpit"&gt;MailPit &lt;/a&gt; which are forked from MailHog and the installation should be almost similar.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This installation is based on following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Linux ubuntu&lt;/strong&gt; server to host your mailserver&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Laravel&lt;/strong&gt; to test send your mail&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Nginx&lt;/strong&gt; to create webproxy to MailHog port&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Okay, lets go !&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Installing MailHog into your ubuntu server.
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;
&lt;span class="c"&gt;# Golang is require to install MailHog&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;apt-get &lt;span class="nt"&gt;-y&lt;/span&gt; &lt;span class="nb"&gt;install &lt;/span&gt;golang-go

&lt;span class="c"&gt;# Downloading MailHog binary to current directory&lt;/span&gt;
go &lt;span class="nb"&gt;install &lt;/span&gt;github.com/mailhog/MailHog

&lt;span class="c"&gt;# moves downloaded binary to '/usr/local/bin/mailhog' directory&lt;/span&gt;
&lt;span class="nb"&gt;sudo mv&lt;/span&gt; ~/go/bin/MailHog /usr/local/bin/mailhog/MailHog

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Configure MailHog using JSON file
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# we will store related mailhog file here&lt;/span&gt;
&lt;span class="nb"&gt;cd&lt;/span&gt; /usr/local/bin/mailhog

&lt;span class="c"&gt;# create a json file 'mailhog-outgoing.json'&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;nano mailhog-outgoing.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and paste the following json :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"server name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"development mail"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"host"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"..."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"port"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1025"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"email"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"..."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"username"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"password"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"test123"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"mechanism"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"PLAIN"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;here you can set up your password, username, etc. for access by laravel application which you can refer &lt;a href="https://github.com/mailhog/MailHog/blob/master/docs/CONFIG.md"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Running MailHog
&lt;/h3&gt;

&lt;p&gt;You can running mailhog manually by CLI provided but since we want to run it 'forever', we can create a linux service for that utilising linux 'systemctl'&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# create a service file in this case, i named it as MailHog.service (capital 'M, H' here)&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;nano /etc/systemd/system/MailHog.service

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

&lt;/div&gt;



&lt;p&gt;and paste the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[Unit]
Description=Mailhog Local Mail SMTP
After=network.target

[Service]
Type=simple
User=ubuntu
ExecStart=/usr/bin/env /usr/local/bin/mailhog/MailHog -api-bind-addr 0.0.0.0:8025 -ui-bind-addr 0.0.0.0:8025 -smtp-bind-addr 0.0.0.0:1025 -storage maildir -maildir-path /usr/local/bin/mailhog/mails/ -outgoing-smtp /usr/local/bin/mailhog/mailhog-outgoing.json &amp;gt; /dev/null 2&amp;gt;&amp;amp;1 &amp;amp;

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

&lt;/div&gt;



&lt;p&gt;the important part here is which &lt;code&gt;user&lt;/code&gt; you want to run this service and the command &lt;code&gt;execStart&lt;/code&gt; which calling Mailhog CLI with the mailhog-outgoing.json configuration. Other flag are kept default and can be refer &lt;a href="https://github.com/mailhog/MailHog/blob/master/docs/CONFIG.md"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Start the service by:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# the name of service mail is sensitive&lt;/span&gt;

&lt;span class="c"&gt;# check does systemctl detect the new service file&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl status MailHog

&lt;span class="c"&gt;# if yes, run the new service&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl start MailHog
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Sending a Mail
&lt;/h3&gt;

&lt;p&gt;By now, the MailHog should be running and can be test. Since I'm already has a Laravel project hosted using mailtrap beforehand, I just need to update my .env to use the local mailhog mail server like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MAIL_DRIVER=smtp
# localhost
MAIL_HOST=0.0.0.0
# depending on value you set up json
MAIL_PORT=1025
MAIL_USERNAME=test
MAIL_PASSWORD=test123
MAIL_ENCRYPTION=null
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then try to send an email.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Opening MailHog Inbox
&lt;/h3&gt;

&lt;p&gt;MailHog inbox is hosted on port 8025 (default). To open the inbox by browser, we can directly go to this port using server-ip:8025. But since my server is behind a domain, I need to proxy through port 80 to port 8025. To do this, use this nginx server block template file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;server {
    server_name mailtrap.mydomain.com;
    listen 80;
    listen [::]:80;

    if ($host != "mailtrap.mydomain.com") {
       return 404;
    }

    location / {
       proxy_pass      &amp;lt;http://localhost:8025&amp;gt;;
       proxy_set_header    Host             $host;
       proxy_set_header    X-Real-IP        $remote_addr;
       proxy_set_header    X-Forwarded-For  $proxy_add_x_forwarded_for;
       proxy_set_header    X-Client-Verify  SUCCESS;
       proxy_set_header    X-Client-DN      $ssl_client_s_dn;
       proxy_set_header    X-SSL-Subject    $ssl_client_s_dn;
       proxy_set_header    X-SSL-Issuer     $ssl_client_i_dn;
       proxy_read_timeout 1800;
       proxy_connect_timeout 1800;
       chunked_transfer_encoding on;
       proxy_set_header X-NginX-Proxy true;
       proxy_set_header Upgrade $http_upgrade;
       proxy_set_header Connection "upgrade";
       proxy_http_version 1.1;
       proxy_redirect off;
       proxy_buffering off;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;where &lt;code&gt;mailtrap.mydomain.com&lt;/code&gt; is the subdomain you set for mailhog inbox.&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>linux</category>
      <category>testing</category>
    </item>
    <item>
      <title>[Linux] SSH to GitHub</title>
      <dc:creator>HakamRaza</dc:creator>
      <pubDate>Sun, 21 May 2023 14:05:02 +0000</pubDate>
      <link>https://forem.com/hakamraza/ssh-to-github-4p5</link>
      <guid>https://forem.com/hakamraza/ssh-to-github-4p5</guid>
      <description>&lt;p&gt;&lt;strong&gt;Some intro&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;GitHub do provide option to use SSH to connect and do many things same as using the standard CLI.&lt;/li&gt;
&lt;li&gt;And there are some functionalities offered by using SSH that make life easier, for example no need for token to pull your branch.&lt;/li&gt;
&lt;li&gt;This is how to change your repo connection to use SSH. This is done in linux ubuntu OS.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pCCaZsJc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d3dnunm6ymjuawe4kjqv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pCCaZsJc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d3dnunm6ymjuawe4kjqv.png" alt="GitHub connection types" width="737" height="250"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Setting Up SSH keys and Permission&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For this, you can use existing user or create a new user specifically for deployment/pull/push through SSH.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd ~

# create .ssh folder inside user home directory
mkdir .ssh

# generate key private and public (.pub) using ssh-keygen, and give name like "github_dev".
# Optional, dont assign passphrase if you want to use it for CLI.
ssh-keygen -t ed25519-sk -C "your_email@example.com"

# check ssh agent running
eval $(ssh-agent -s)

# register only private key (no .pub) generated to ssh agent
ssh-add ~/.ssh/&amp;lt;private_key_file&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;ul&gt;
&lt;li&gt;for secure linux and current user to read private key to execute GitHub CLI, update the permission for access.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# change permission key file for read-only
cd ~/.ssh
chmod 644 &amp;lt;private_key&amp;gt;
sudo chgrp &amp;lt;username&amp;gt; &amp;lt;private_key&amp;gt;

# change permission to folder .ssh
cd ~
chmod 700 .ssh
sudo chgrp &amp;lt;username&amp;gt; .ssh

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

&lt;/div&gt;






&lt;ul&gt;
&lt;li&gt;you also can directly define any SSH connection to GitHub to use the private key generated by creating 'config' file
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# config connection using ssh and correct key file
nano .ssh/config

# paste the following into the 'config' file
Host github.com
Hostname ssh.github.com
Port 443
User git
IdentityFile ~/.ssh/&amp;lt;private_key&amp;gt; #generated private key location
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Register public key generated to GitHub account.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Go to intended &lt;a href="https://github.com/settings/keys"&gt;GitHub account setting&lt;/a&gt; to register this public SSH key.&lt;/li&gt;
&lt;li&gt;Give any name you want.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tpddey34--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ixdc5thz3pfyl9vdxsje.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tpddey34--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ixdc5thz3pfyl9vdxsje.png" alt="Register public key at github account" width="800" height="432"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Check Signature Github.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;use the command below to cross check signature of the ssh agent is the same as displayed by GitHub
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ssh-add -l -E sha256
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Yta74EGW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cf05alr8lrwhpk0xrc92.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Yta74EGW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cf05alr8lrwhpk0xrc92.png" alt="checking key's hash is the same" width="800" height="288"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Testing GitHub SSH Connection&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;test successful connection through SSH to GitHub by:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# testing connection to github
ssh -T git@github.com

# testing with more details for troubleshooting
ssh -vT git@github.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Update Repository Remote URL&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;After successful connection through SSH, change current local repository remote to use SSH&lt;/li&gt;
&lt;li&gt;usually the remote url is like this:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Check connection profile setup
git remote -v

# result 
# origin  https://github.com/****.git (fetch)
# origin  https://github.com/****.git (push)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;you can either update original 'origin' or add new one like 'myssh'&lt;/li&gt;
&lt;li&gt;the address can be refer back here:
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pCCaZsJc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d3dnunm6ymjuawe4kjqv.png" alt="GitHub connection types" width="737" height="250"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# add ssh connection profile 'myssh'
git remote add myssh ssh://git@****.git

# result 
# origin  https://github.com/****.git (fetch)
# origin  https://github.com/****.git (push)
# myssh     git@github.com:****.git (fetch)
# myssh     git@github.com:****.git (push)


# update existing connection profile 'origin' to use SSH
git remote set-url origin git@github.com:****.git

# result 
# origin     git@github.com:****.git (fetch)
# origin     git@github.com:****.git (push)

# update back to use HTTP
git remote set-url origin https://github.com/****.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;example using GitHub CLI through SSH connection profile different than default 'origin'
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# using 'myssh' connection profile
git checkout myssh/&amp;lt;branch name&amp;gt; -b &amp;lt;new branch name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;User Connection Issue&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;There will be an issue sometimes with linux system user especially when using 'sudo'. In this case, the one executing CLI is not the current user but by the 'root' system user.&lt;/li&gt;
&lt;li&gt;In this case, to maintain user profile, use '-E' flag:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo -E git fetch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Bash Script&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Example a bash script to pull the 'develop' branch.&lt;/li&gt;
&lt;li&gt;The ssh-agent may need to be started again in linux.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash

# start ssh-agent and register back private key
eval $(ssh-agent -s)
ssh-add ~/.ssh/&amp;lt;private_key_file&amp;gt;

# Go to project epo
cd /var/www/&amp;lt;local repository directory&amp;gt;

# Checkout 'develop' branch
git checkout develop

# Update connection profile
# git remote set-url origin git@github.com:*****.git

# Pull latest changes
git pull

# Checkout by latest tag
# git fetch --tags
# tag=$(git describe --tags `git rev-list --tags --max-count=1`)
# echo $tag
# git checkout $tag -b latest

# Set back to HTTP
# git remote set-url origin https://github.com/*****.git

#
# Additional steps
#

echo Done
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>github</category>
      <category>ssh</category>
    </item>
    <item>
      <title>[Linux] Setting Up Nginx Block</title>
      <dc:creator>HakamRaza</dc:creator>
      <pubDate>Wed, 17 May 2023 07:25:35 +0000</pubDate>
      <link>https://forem.com/hakamraza/setting-up-nginx-block-3adf</link>
      <guid>https://forem.com/hakamraza/setting-up-nginx-block-3adf</guid>
      <description>&lt;p&gt;&lt;strong&gt;What is nginx block ?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;is a config file on how to serve your app. It is part of Nginx webserver settings file. They are located at &lt;code&gt;/etc/nginx/sites-available/&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Same as Apache which is another webserver, it also has its own config 'block' to serve your app.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;Example a Standard Block for PHP project&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;server {
    # listen to port 80 for IPv4
    listen 80;
    # listen to port 80 for IPv6
    listen [::]:80;
    # the address domain of your app 
    server_name dev.mydomain.com;`

    # location of your SSL certificates if in use
    #ssl_certificate /ssl/crt/file.crt;
    #ssl_certificate_key /ssl/key/file.key;

    # location of 'myproject' folder where index.php located
    root /var/www/development/myproject/public;
    # name of index file entry point
    index index.php index.html index.htm index.nginx-debian.html;

    # add additional header before request reach your app
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    charset utf-8;

    # optional condition to block request coming from other address
    if ($host != "dev.mydomain.com") {
       return 404;
    }

    # Optional, prevent Direct Access To Protected Files
    location ~ \.(env|log) {
        deny all;
    }

    # Optional, prevent Direct Access To Protected Folders
    location ~ ^/(^app$|bootstrap|config|database|overrides|resources|routes|storage|tests|artisan) {
        deny all;
    }

    # Optional, prevent Direct Access To modules/vendor Folders Except Assets
    location ~ ^/(modules|vendor)\/(.*)\.((?!ico|gif|jpg|jpeg|png|js\b|css|less|sass|font|woff|woff2|eot|ttf|svg|xls|xlsx).)*$ {
        deny all;
    }

    # passing address parameters from request
    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;

    # Pass PHP Scripts To FastCGI Server
    location ~ \.php$ {
        # passing address parameters from request
        try_files $uri $uri/ /index.php?$query_string;

        # set version of php fpm to serve the php project
        fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    # deny all other requests that not match
    location ~ /\.(?!well-known).* {
        deny all;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Example a Standard Block for PHP project with multiple subdomains&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;server {
    listen 80;
    listen [::]:80;
    server_name dev.mydomain.my dev2.mydomain.my;

    root /var/www/production/myproject/public;
    index index.php index.html index.htm index.nginx-debian.html;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    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; }

    location ~ \.php$ {
        try_files $uri $uri/ /index.php?$query_string;
        fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
        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;p&gt;&lt;strong&gt;Example a Standard Block for Reverse-Proxy to Port&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;this is usually use to serve Node projects
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;server {
    listen 80;
    listen [::]:80;
    server_name dev.mydomain.com;

    if ($host != "dev.mydomain.com") {
       return 404;
    }

    location / {
       # example proxy-ing port 80 to port 1337 in server localhost
       proxy_pass      http://localhost:1337;

       # set up headers towards proxy
       proxy_set_header    Host             $host;
       proxy_set_header    X-Real-IP        $remote_addr;
       proxy_set_header    X-Forwarded-For  $proxy_add_x_forwarded_for;
       proxy_set_header    X-Client-Verify  SUCCESS;
       proxy_set_header    X-Client-DN      $ssl_client_s_dn;
       proxy_set_header    X-SSL-Subject    $ssl_client_s_dn;
       proxy_set_header    X-SSL-Issuer     $ssl_client_i_dn;
       proxy_read_timeout 1800;
       proxy_connect_timeout 1800;
       chunked_transfer_encoding on;
       proxy_set_header X-NginX-Proxy true;
       proxy_set_header Upgrade $http_upgrade;
       proxy_set_header Connection "upgrade";
       proxy_http_version 1.1;
       proxy_redirect off;
       proxy_buffering off;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>backend</category>
      <category>nginx</category>
    </item>
  </channel>
</rss>
