Saturday 26 December 2020

Deploying Django application on Fedora 33

Deploying a "Hello, World" Django app on Fedora 33
Requirement
1) Fedora server
2) NGINX web server
3) Django application framework
4) Gunicorn

A "Hello, World" application using Django

Django framework based upon Python. Current Python version is 3.7.
First check if python is installed. If not install it.

$ sudo dnf install python3


once Python is installed using pip3 django should be installed

$ pip3 install django~=3.1.2


Any version of django is good but it is personal preference.

next Gunicorn should be installed.

$ pip3 install gunicorn


We are almost ready for Django application development.

Soiling your hand
To start our development, we need to create a directory and put all our requirement there

$ mkdir myapp && cd myapp


I choose myapp but any thing is ok

$ django-admin startproject mapp


mapp is project name. Django will create some files for us and lessen our job. It will create
mapp directory and few files

.
└── mapp
    ├── manage.py
    └── mapp
        ├── asgi.py
        ├── __init__.py
        ├── settings.py
        ├── urls.py
        └── wsgi.py

Next we create a small application to run on top of it.

$ cd mapp
$ django-admin startapp hello


.
├── hello
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── manage.py
└── mapp
    ├── asgi.py
    ├── __init__.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

now open the mapp/settings.py

$ vi mapp/settings.py


add following lines

INSTALLED_APPS = [
    ...
    ...
    'hello',
]



next open views.py
$ vi hello/views.py


...
...
from django.http import HttpResponse
def homePageView(request):
    return HttpResponse('Hello, World!')



create a file urls.py under hello and open it

$ vi hello/urls.py


add the following code to it.


from django.urls import path
from .views import homePageView
urlpatterns = [
    path('', homePageView, name='home')
]



Update mapp/urls.py as bellow


from django.contrib import admin
from django.urls import path, include
urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('hello.urls')),
]



Now test our app by running

$ python3 manage.py runserver


In browser, open http://127.0.0.1:8000
and enjoy your app

Ok you might have seen some warnings here. So please don't worry. Execute

$ python3 manage.py migrate


some messages will be displayed and now again execute

$ python3 manage.py runserver


This time there are no warnings, right? Ok good...

Deployment part

Open settings.py

$ vi mapp/settings.py


and change following

...
ALLOWED_HOSTS = [ '*' ]
...
...
STATIC_ROOT = '/var/www/static'
MEDIA_ROOT = '/var/www/media'


and make sure you created those directories

$ sudo mkdir /var/www/static
$ sudo mkdir /var/www/media


Next run

$ sudo python3 manage.py collectstatic


we are almost done.

Running guincorn

First copy mapp/wsgi.py as mapp/mapp.wsgi

$ cp mapp/wsgi.py mapp/mapp.wsgi
$ sudo gunicorn mapp.wsgi --workers=5 --bind=127.0.0.1:8888


now gunicorn is running. you can check with http://127.0.0.1:8888 and it will show you hello world page.

But we need nginx to serve for us on internet so we have to tell nginx to serve for us.

use following code as a .conf file in /etc/nginx/conf.d/ directory
we assume it as myapp.conf
server {
    # the port your site will be served on
    listen      80;
    # the domain name it will serve for
    server_name 192.168.1.3;   # substitute by your FQDN and machine's IP address
    charset     utf-8;

    #Max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django static files. You should run 'django manage.py collectstatic'
    location /static  {
        alias /var/www/static;      # your Django project's media files
    }
    location /media {
    alias /var/www/media;
    }
    # Finally, send all non-media requests to the Django server.
    location / {
        proxy_pass http://127.0.0.1:8888;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}



restart nginx server by using

$ sudo systemctl restart nginx


now check from a remote machine using http://IP.address.of.server/

do you get it properly??? if not then check firewall settings on server. next check access on files etc...


goodluck.

Friday 24 July 2020

Fedora 32 on Google Cloud



Google doesn't provide a Fedora image for its cloud platform neither Fedora provides one. but this wont stop us. It is a nice learning and adventure for orthodox people like me.

Those who have started there career in Computer in late 80's and early 90's can understand my pain 😬

Well during this time of pandemic COVID-19 I thought of taking an adventure ride into the world of Virtualization. Though Virtualization is not new to me, as during my days at RedHat I did some virtualization stuff by porting drivers, but that is a very short lived assignment for me. Even I was fascinated by Vmware's ESX server Community edition. Even to tryout my toy Operating System NanoOS i need some platform to try out so Was using qemu and BOCHS.
But setting up a VM in a cloud is just a new thing for me. If you like to learn along and how to do it then read along.

Prerequisites 

As I don't know much about Windowz, will restrict to Google's cloud console as much as possible and at some times Linux desktop.
  • Google account
  • Machine with Fedora Desktop(any Linux distro will do but I like Fedora)
  • Google SDK for Linux installed on your machine.
Deploying a VM needs availability of an Image. Though there is no image for Fedora 32 but similar images are available for CentOS and RHEL. We could choose any but I would prefer CentOS over RHEL.

Plan of Action 
  • We will create a VM using CentOS image. This will be a very minimum one as we will use it for creating Fedora 32 image.
  • Download Fedora cloud image from Fedora download site
  • Tweek Fedora image to make it suitable for Google.
  • Upload the image
  • Create an instance of it 
Creating a VM 
Here you need a google account your Gmail Account wont work here. You need to have cloud permission in your account. Please refer to google site for procedure. its not difficult but its not relevant here hence omitted.

Similarly install Google cloud SDK (gcloud and gsutil). Information about the procedure is available on Google SDK site.
  • Login to Google cloud, the bellow screen will be displayed
  • Click on menu (3 horizontal line)

  • from drop down select Compute Engine

  • Next it will display a page with all vm instances if available. for us it should be empty. Next on the top there is an icon with a plus


  • Now a form will be displayed, it will ask to enter a name for your VM instance . We will put boiler-plate there. Dont worry about other options except Boot Disk.

  • On Boot Disk option by default it will be debian. There is a button change next to it. Clicking it will display several options.


  • On clicking change something like this will appear. We will select CentOS. On next screen it will ask which version to use. we will use CentOS 8 as it has dnf command. But If you prefere yum can choose other older versions too.





  • Allow it for some time say 5 Minutes. then on VM Instance page our new VM will be displayed.


Ok we have successfully created a VM on Google Cloud now we can enjoy it for some timeπŸ™† .Relax get a coffee and think what we have achieved.
After this point our journey will be some what tough as we will be doing some sysadmin job (yes SysAdmins are really great people They do a lot of hectic work to ease our life).

Click on ssh button at the end of the line of our newly created VM instance. It will open a newbrowser window with shell prompt of our VM. Check if all your commands are accepted or not.

Download Fedora cloud image

As we know there is no Google compatible image for Fedora available but Cloud images are available. We can download it and adjust  for Google compatibility. 
On the ssh terminal shell prompt check if wget is available or not. Else install it by using dnf or yum.

~]$ sudo dnf install wget

So go ahead and download  Fedora Cloud image. Choose raw image for convenience. Remember we are downloading into VM on google not onto our Desktop.

~]$ wget -c"https://download.fedoraproject.org/pub/fedora/linux/releases/32/Cloud/x86_64/images/Fedora-Cloud-Base-32-1.6.x86_64.raw.xz"

it will be downloaded to the home folder in VM. Give it some time as it is a long file , but don't worry it will be downloaded in few minutes say 2 to 3 minutes.

Preparing the image
Now the raw file is downloaded as *.tar.xz format. It has to be extracted  first 

~]$ tar -xvf Fedora-Cloud-Base-32-1.6.x86_64.raw.xz

It is better to copy this file to a smaller name like disk.raw and work on that.
  • mount the image

~]$ sudo losetup  /dev/loop0 disk.raw
~]$ kpartx -a /dev/loop0

mount the disk
$ sudo mkdir /mnt/disk
$ sudo mount /dev/mapper/loop0p1 /mnt/disk


  • Bind mount some filesytem

~]$ sudo mount --bind /dev /mnt/disk/dev
~]$ sudo mount --bind /sys /mnt/disk/sys
~]$ sudo mount --bind /proc /mnt/disk/proc

besides this another file has to be created that is /etc/resolv.conf content of that file is very small and as follows.

search domain.name
nameserver 8.8.8.8

Its now time to go into chroot
$ sudo chroot /mnt/disk
After chrooting check if ping works, that is if network works else adjust your network to work in chroot environment.

First thing to do is update the image. by
~]$ sudo dnf update

Next we have to install 
~]$ sudo dnf -y remove cloud-init
~]$ sudo dnf -y install google-compute-engine-tools
~]$ sudo dnf clean all

Now we are almost done. Google need some service to be enabled.
~]$ sudo systemctl enable google-accounts-daemon google-clock-skew-daemon \
    google-instance-setup google-network-daemon \
    google-shutdown-scripts google-startup-scripts

Well our Image is almost ready. Now we should leave chroot environment unbind and unmount the filesystems we have mounted earlier.

~]$ sudo umount /mnt/disk/dev /mnt/disk/sys /mnt/disk/proc
~]$ sudo umount /mnt/disk
~]$ sudo losetup -d /dev/loop0

In above steps, unmounting disk will throw "resource busy error" dont worry. Give it some time to finish writing stuffs, 5 to 10 minutes maximum. then unmount the disk and delete the loop0. We are done.
Google need its image to be in *.tar.gz format so we will compres the image in tar.gz format.

~]$ tar cvzf fedora-32-google-cloud.tar.gz disk.raw

Now we will upload the image just created "fedora-32-google-cloud.tar.gz"

We have created in side Fedora VM. We have to put it into Google cloud bucket. Using Web console
  • Goto Storage
  • Create a bucket
remeber its name. Now from within VM we can copy this image to bucket.
~]$ gsutil cp "fedora-32-google-cloud.tar.gz" "gs://[name of the bucket]/"
Replace with your bucket name in above comand. Well it will be uploaded to the bucket in a while. It can be seen listed under the bucket created.

From your desktop fedora fire this command.
~]$ gcloud compute images create --source-uri \
    gs://[your bucket]/fedora-32-google-cloud.tar.gz \
    --licenses "https://compute.googleapis.com/compute/v1/projects/vm-options/global/licenses/enable-vmx" \
    fedora-32-google-cloud-image
Wait for few seconds, an image will be created in images section of console. it can be seen under Images section of Web console. Note the --licenses calause. it is optional. you can omit it. but if you omit it then the created image wont be able to run virtual machines under it. Yes you read it correctly a Nested VM.

I think you enjoied your second coffee ... Jocking...

Now the image is ready and we can create an instance out of it as we created our boiler-plate only difference will be in case of Boot Image we will choose our image and we will choose a multi CPU configuration. After the instance is created check it using SSH and enjoy your Multi CPU Fedora Server on Google cloud.

Well This is enough for Now. Please contact me if you need any help.

73's
DE VU3VFU
Ashok.

Sources :
1) major io
2) linuxmint
3) media com

Wednesday 6 May 2020

Covid-19 Lockdown Update

Hi friends, in this time of global pandemic, all are forced to be inside. To avoid the boredom of self exile some thing extra has to be done to dissipate stored energy. I adapted few of my hobbies to do the same. Today I want to share how and what I was doing during past few weeks.

2m Flower pot Antenna

For my VHF activity I made VK2ZOI design flowerpot antenna. The first one i made is a described here. This one worked exceptionally well it outperformed my existing (refurbished) 5/8 GP. i could reach 30 km with only 5 watts with 59 report from 2 stations and 57 from other stations. Earlier it was not possible to reach them from my QTH (location). After tremendous success, i decided to make its big brother hi-gain dual band antenna described here. I tried it 2 times but i failed miserably. this designed didn't work for me (Might be i missed something). So i kept experimenting on that design on hold 🌝🌝🌝.


SDR on Raspberry PI 3B

SDR is my passion. I am a fan of this Software Defined Radio(SDR) technology. During this boring lock-down, I thought to put SDR softwares on a RPI-3B. Well yes there images available to do SDR on RPI. But they are all based on Raspbian. Though i can work on it as I worked on Ubuntu and Armbian. But Fedora is my choice as I have worked with Redhat earlier.

For Experimenting I need to change image often hence first I enabled USB booting on RPI-3B. Then  Tried some images. If any of you trying or replicating then do remember fedora is a heavier OS for RPI.

Installing Fedora.

REMEMBER  Never jump to load a GUI image directly by downloading.
You know Why I am telling πŸ˜… ......... Yes you are right. So download Fedora minimal image for armhfp from here.  Now either use "dd" or use "disk image writer" to create a USB. USB pendrive should be more than or equals to 8GB. 
(I downloaded Fedora 31 but you can go for latest Fedora 32 also.)
After making the disk boot from it. Next is to enable wifi. the key is you have only nmcli to configure and setup your network. After you are connected to Internet you need to update the system.

sudo dnf update

next is installing SDR software. basic thing is to install RTL-SDR driver. Its easy just use dnf to install if you are ok with 28MHz and above reception. If you need  HF reception without any up-converter , then stock RTL-SDR from fedora repo wont work. For that purpose I will recomend AB9IL . down load it and compile and install it. After installation try rtl_test and rtl_tcp. if you want to use this as remote then you can stop here. Execute 

rtl_tcp -a <ip address of RPI> -p <port default is 1234> -s12000 

Yes keep sampling rate small as RPI doesn't have much power and wifi is unreliable πŸ˜›πŸ˜›πŸ˜›. From another machine on the same network use gqrx to connect to your RPI and tune FM stations and of course tune  HF to see if you get any... I am sure you will get plenty of Chinese and Arabic stations. I could receive our Local Medium wave station on 972.0KHz only. so you can try your luck.( Note: the sensitivity and frequency range varies from Dongle to Dongle)

Well if you want to venture in to a standalone SDR, then stay with me. The Next step is to put a GUI Desktop environment. One must choose as per his/her personal choice but my experience is bitter with LXQt Gnome and XFCE on RPI-3B. Only LXDE worked smoothly and without much problem. Though i prefere LXQt on my Desktop. 

Next is you have to install SDR Radio software like gqrx or CubicSDR. For any SDR software One need a DSP library. liquidDSP andGnuradio are best in my view. GNURadio available from Fedora repo installs Python-2. But GNURadio 3.8 from github uses Pythom-3 hence no old dependencies. So be prepare to compile GNURadio by yourself. 

How to compile for RPI? 

Well You can compile it on RPI itself but it is never intended. So 
1- copy everything to a folder on your desktop PC 
2- install qemu-user-static on your pc
3- locate qemu-arm-static
4- copy it in to the folder in point 1
5- use chroot and compile on your PC

Though it is standard method this is slow in comparison to compiling directly on RPI.( Atleast on my Intel(R)Core(TM)2Duo CPU E6750@2.66GHz )

using this process GNURadio, gqrx, LiquidDSP, WxWidget, SoapySDR, SoapySDRserver, SoapySDRremote,soapy-rtlsdr and CubicSDR are compiled.
Please refer to individual compilation process described for respective  packages

Now Copy back the compiled apps to your Pen drive. Boot from it and try executing gqrx and CubicSDR. 

Note: May complain about Library not found even after executing ldconfig so adjust /etc/ld.so.conf to include /usr/local/lib  
now everything will be fine.

Executing gqrx

Try finding optimum sample rate, band width. Next adjust FFT buffer and speed. Reasonable sound quality will be heard.

Executing CubicSDR

Here multiple settings at multiple areas should be adjusted. Sampling rate, Audo sampling rate and gain to get a good sound quality.





 Cheers 73's
DE VU3VFU 

Monday 9 March 2020

How to setup LAMP using Fedora 31

There are plenty of documents available on internet and you may be a pro in this field. Well I am new and its for my references and someone like me πŸ˜ƒ trying to learn by doing myself. 
Here I will put the key points to setup a basic LAMP (Linux Apache Mariadb PHP) server all by yourself on your hardware πŸ˜ƒ.

Begin 

To start with we have to have a Fedora server or Worksation  ready. To achieve this one can use Live ISO files from Fedora site. Please refer to Documentation on how to do so. Here I assume knowledge of fedora server installation and setup.
The system may be installed on real hardware or on VM(Virtual Machine).

Basic LAMP packages need to be installed

After the bare minimum Fedora server installation, it is required to install the following packages.

php php-cli php-php-gettext php-mbstring php-mcrypt php-mysqlnd php-pear
php-curl php-gd php-xml php-bcmath php-zip mariadb-server httpd.

Use dnf install as root to install the packages and its dependencies. 

Configuration

It is assumed that the network is configured properly and it is accessible from outside world. For Virtual machine, Network setup is bit involved but Internet has many resource. ( Hint: Install a Bridge and go on)

1) SELinux should be set to permissive. (Please do not dissable it)

  • sudo setenforce 0
  • open /etc/selinux/config as root
  • sudo nano /etc/selinux/config
  • change SELINUX=enforcing to SELINUX=permissive
  • save the file

2) PHP setup

  • set timezone in /etc/php.ini 
  • date.timezone = Asia/Kolkata

3) MySQL configuration

  • open /etc/my.cnf.d/mariadb-server.cnf as root
  • in [mysqld] section change characterset to utf8 
character-set-server=utf8
  • start mariadb service
  • sudo systemctl strat mariadb
  • enable the service to run automatically when system boots
  • sudo systemctl enable mariadb
  • setup root password for mariadb by using mysql_secure_installation
  • remove anonymous user
  • disallow remote root login
  • remove test databases and access to it
  • reload previledge tables

4) Firewall setup

  • allow access for HTTP/HTTPS connections
    sudo firewall-cmd --add-service={http,https} --permanent
  • allow mysql access through firewall
    sudo firewall-cmd --add-service=mysql --permanent
  • reload firewall
    sudo firewall-cmd --reload

now you are done. Enjoy your adventure.

Neural Network from Scratch Using C (Part-3)

  XOR Problem In part 2, AND gate and OR gate could be trained using a single perceptron. But XOR gate could not be as it is not linearly...