Write Bash Script Which Accepts Argument or User Input

Changing ownership of directories and files. Typing chown -R www-data:www-data * is annoying repetitive task I have to run.

I want a simple version of that command. Like chownership. Or chwn to be even shorter.

The fastest way to do it is by making alias inside .bashrc or .bash_profile

But today I’ll go the other way. Write it in a bash script.

To create a globally available executable bash script, I save the file in /usr/local/bin/. That is for saving even more time rather than typing the full path of the executable.

cd /usr/local/bin/
nano chwn
chmod +x chwn

#1 The First Way: Passing Argument(s)

Bash script executes by typing chwn username. This is the most similar method like the chown’s nature command.

#!/bin/bash

chown -R $1:$1 *

#2 The Other Way: Passing Input(s)

Bash script executes by typing chwn <enter> then the following prompt, asking what user and group I want to apply. Type username <enter>.

This other way uses dual steps. Impractical. Unnatural method as the chown command. I’m not using this. This is more suitable for a program with more complex routines. Like installing multiple packages in a one-go.

#!/bin/bash

echo "user?"
read user
chown -R $user:$user *
Install the Latest MariaDB Release with EasyEngine 3
dpkg: error processing archive /var/cache/apt/archives/mariadb-server-10.1_1%3a10.1.45+maria-1~bionic_amd64.deb (--unpack):
new mariadb-server-10.1 package pre-installation script subprocess returned error exit status 1
Errors were encountered while processing:
/var/cache/apt/archives/mariadb-server-10.1_1%3a10.1.45+maria-1~bionic_amd64.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)

Error di atas adalah problem yang muncul bersamaan dengan pandemi COVID-19 di tahun 2020. Ketika update Ubuntu via apt update -y, proses terhenti karena ada error saat memperbarui mariadb.

Solusi tercepat saat itu adalah dengan uninstall mariadb-server sama sekali dari server.

Loh kok dibuang? Iya, karena mariadb-server tidak terpakai sama sekali di local host, karena database pakai remote host.

Namanya quick fix jelas tidak mengobati penyakit sebenarnya. Hanya menutupi luka sementara, agar proses apt update tetap tereksekusi dengan sukses hingga selesai.

[email protected]:~# apt install mariadb-server -y
Reading package lists… Done
Building dependency tree
Reading state information… Done
Correcting dependencies… Done
The following additional packages will be installed:
mariadb-server-10.1
Suggested packages:
mailx mariadb-test tinyca
The following NEW packages will be installed:
mariadb-server-10.1
0 upgraded, 1 newly installed, 0 to remove and 108 not upgraded.
21 not fully installed or removed.
Need to get 0 B/7,234 kB of archives.
After this operation, 89.5 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Preconfiguring packages …
(Reading database … 102169 files and directories currently installed.)
Preparing to unpack …/mariadb-server-10.1_1%3a10.1.45+maria-1~bionic_amd64.deb …
Failed to stop mysql.service: Unit mysql.service not loaded.
invoke-rc.d: initscript mysql, action "stop" failed.
invoke-rc.d returned 5
There is a MySQL server running, but we failed in our attempts to stop it.
Stop it yourself and try again!
dpkg: error processing archive /var/cache/apt/archives/mariadb-server-10.1_1%3a10.1.45+maria-1~bionic_amd64.deb (--unpack):
new mariadb-server-10.1 package pre-installation script subprocess returned error exit status 1
Errors were encountered while processing:
/var/cache/apt/archives/mariadb-server-10.1_1%3a10.1.45+maria-1~bionic_amd64.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)
[email protected]:~# cd

Solusinya? Baru dapat bulan Agustus ini: menambahkan repository MariaDB 10.2.

curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup \ | sudo bash -s -- --mariadb-server-version=10.3 --skip-maxscale

Setelah itu baru kita bisa install mariadb-server dengan selamat dan sukses.

sudo apt update && sudo apt install mariadb-server

Gitu aja ternyata. Asv sekali …

Referensi hasil googling dari sini dan sini.

Nginx Block Countries Using GeoIP Modules

Nginx block countries using GeoIP modules. Fortunately using EasyEngine installation, nginx is already installed with -with-http_geoip_module option. You can check with this command

nginx -V 2>&1 | grep -- 'http_geoip_module'

The IP database is also already downloaded on /usr/share/GeoIP/GeoIP.dat

1. Map and Declare $allowed_country Variable

This means the only steps we need to do is create a conf file inside /etc/nginx/conf.d/ directory. For a reminder, creating a conf file inside the conf.d directory will be auto-included from nginx.conf file. The configuration content below is meant to be put inside http block. Let’s name it badcountries.conf.

geoip_country /usr/share/GeoIP/GeoIP.dat;
map $geoip_country_code $allowed_country {
    default yes;
    CN no;
    ID no;
    IN no;
    RU no;
}
geo $exclusions {
    default 0;
    10.8.0.0/24 1;
}

2. Check $allowed_country Variable with Active IP Visitor

Next step is check the $allowed_country variable against the current visitor’s IP address. Put this code inside server block.

if ($allowed_country = "no") { 
  return 444; 
}

Restart nginx after editing is done.

service nginx restart

Now, every visitors coming from countries you defined as no inside badcountries.conf will be served with 444 error code.

For the list of ISO 3166 country codes you can put inside badcountries.conf file, follow this link.

MySQL Cannot Connect to MySQL 4.1+ Using the Old Insecure Authentication

Install phpMyAdmin di localhost AppServ namun kemudian tidak bisa diakses karena alasan old password? Begini caranya

Database connection failed: mysqlnd cannot connect to MySQL 4.1+ using the old
insecure authentication. Please use an administration tool to reset your
password with the command SET PASSWORD = PASSWORD(‘your_existing_password’).

login via Run -> cmd
login ke mysql dgn user root

SET old_passwords = 0;
SET PASSWORD FOR 'root'@'localhost' = PASSWORD ('admin');
SET PASSWORD FOR 'root'@'127.0.0.1' = PASSWORD ('admin');
FLUSH PRIVILEGES;
Essential Apache httpd Cheat Sheet

Apache httpd punya banyak setting daripada sekedar VirtualHost dan .htaccess. Buat Anda yang suka manajemen server kosongan, tanpa cPanel, mungkin Cheat Sheet Apache ini bisa sangat membantu pekerjaan.

http://refcardz.dzone.com/refcardz/essential-apache-http-server

MySQLdump Got Error 1016: Can’t Open File When Using LOCK TABLES

mysqldump got error 1016

mysqldump got error 1016. Pesan di atas saya temui barusan ketika mengekspor database WordPress, yang ternyata baru saya ketahui jumlah tabelnya ada 1719 buah. Error tersebut adalah kemungkinan yang akan muncul manakala kamu ingin mengekspor satu buah database dengan jumlah tabel hingga ribuan. Salah satu aplikasi PHP MySQL populer yang memungkinkan populasi tabel hingga empat digit adalah WordPress Multisite (dulu WordPress Multi User).

Instalasi WordPress single user, biasanya hanya membuat 11 buah tabel default. Menambahkan plugin ke instalasi WordPress adakalanya akan menambah tabel lagi, tergantung dengan plugin yang dipasang.

WordPress Multisite memungkinkan satu buah instalasi WordPress diisi dengan dua, tiga, hingga ribuan blog yang berbeda. Contoh paling mudah adalah WordPress.com yang merupakan satu buah instalasi WordPress dengan ratusan ribu blog di dalamnya. Satu buah blog memiliki 11 tabel MySQL, jadi bisa dihitung berapa juta jumlah tabel yang ada di situs WordPress.com.

Jadi, solusinya apa?

Server Management: Mengaktifkan Apache SuExec pada WHM/cPanel

Bagi para penyewa Dedicated Server/VPS dengan control panel WHM/cPanel, maka sudah bisa dipastikan bahwa berbagai akun website yang berada di dalamnya, masing-masing dapat menjalankan aplikasi PHP berdasarkan nama user-nya. Sebagai misal sebuah web dengan domain akuganteng.com akan mendapatkan lokasi webroot berada di /home/akugante/public_html/. Semua file dan direktori yang berada di webroot tersebut akan memiliki nama user 'akugante' dengan nama group 'akugante'.

Hal ini dimungkinkan karena WHM sudah mengintegrasikan aplikasi suExec, yang bisa mengganti nama user default ‘apache’ dengan nama akun situs masing2. SuExec berfungsi untuk meningkatkan keamanan tiap-tiap akun situs, yang mana satu akun hanya dapat mengeksekusi file-file PHP dengan nama user dan group yang sama.

Bayangkan dengan keadaan default di mana nama user hanya satu yaitu 'apache' atau 'wwwrun', maka bilamana server tersebut dijual kembali sebagai reseller, seorang penyewa hosting yang mengerti tentang kode bisa mengunggah skrip PHP-nya ke server dan menggunakan skrip tersebut untuk mengakses direktori milik penyewa server lainnya.

Pertanyaan serius: Saya nggak bisa ngedit file template WordPress via wp-admin, kenapa?

Kembali ke WHM, ternyata pada keadaan default baru diinstall WHM/cPanel, PHP dijalankan sebagai DSO (Dynamic Shared Object) dan memiliki nama user ‘nobody’. Akibatnya, beberapa fungsi yang bisa dijalankan oleh PHP yang berkaitan dengan file system akan mengalami kendala.

Seperti contoh, pada CMS WordPress, di dalam panel admin dashboard, user bisa mengedit template dan plugin yang mereka punya. Nah, jika PHP masih berjalan sebagai DSO, maka otomatis user tidak akan bisa menyimpan hasil editan mereka (tombol “Update File” tidak muncul), karena user ‘nobody’ tidak bisa mengedit file dengan ownership ‘akugante’. Ini adalah contoh dari kasus yang lumayan sering muncul dari teman-teman yang baru saja memigrasikan situs-situs mereka ke server VPS/DS yang baru.

How To Enable WHM Apache PHP SuExec

Lalu, bagaimana cara mengaktifkan SuExec agar PHP dapat berjalan sesuai nama akun?
Ketik ‘suex’ di search box kiri atas, maka menu yang bejibun di panel kiri akan otomatis terfilter yang salah satunya adalah “Configure PHP and SuExec”. Klik, lalu ganti nilai dropdown dso menjadi cgi, lalu “Save new configuration”.

Sesederhana itu ternyata. Untuk mengetes, cobalah buka WordPress admin Anda, lalu edit salah satu template. Jika tombol “Update File” muncul berarti “Voila!”, Anda sakses!!

suexec activate on whm cpanel

WORKS