Showing posts with label Ubuntu. Show all posts
Showing posts with label Ubuntu. Show all posts

Wednesday, April 13, 2016

Configure Apache to enable cgi-bin user directories to run cgi, perl and python in UBUNTU

1. First enable cgi module using the below lines.
sudo a2enmod cgi
sudo service apache2 restart
After latest apache with cgi mod enabled. 
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

  Options +ExecCGI
  AddHandler cgi-script .cgi .pl .py
  Options FollowSymLinks
  Require all granted
3. Create CGI script CGI works with multiple languages, but for now we start with bash shell. We will show examples for some other languages later. Here is a simples version of the CGI bash example:
#!/bin/bash
echo "Content-type: text/html"
echo ''
echo 'CGI Bash Example'
Copy the above code and paste it to a new file in /usr/lib/cgi-bin/ called example-bash.sh. Once done make the file executable using thechmod command:
$ sudo chmod 755 /usr/lib/cgi-bin/example-bash.sh
4. View CGI script
All what remains is to navigate with your browser to host-name or IP address of your web server. In our case the URL will be: http://cgi-example.local/cgi-bin/example-bash.sh
CGI bash example
You can edit this example to display a disk usage of server's root partition "/". You are only limited by your imagination:
#!/bin/bash
echo "Content-type: text/html"
echo ''
echo 'CGI Bash example
'
echo `df -h / | grep -v Filesystem`
The above code will check for free disk space for a root partition and produce the following page:
CGI bash example 2

5. More CGI examples

As promised, here are more CGI examples for a few more programming languages to get you started.

5.1. Perl

Create and make executable the following /usr/lib/cgi-bin/example-perl.pl with a content:
#!/usr/bin/perl

print "Content-type: text/html\n\n";
print <

CGI Perl Example

CGI Perl Example

CGI Perl Example htmlcode

5.2. Python

Create and make executable the following /usr/lib/cgi-bin/example-python.py with a content:


CGI Python Example

CGI Python Example

CGI Python Example """

5.3. C

For C and C++ to work we will need to have a compiler installed. First, install compiler with:
$ sudo apt-get install build-essential
Once installed create a file example-c.c with the following code:
#include 
int main(void)
{
printf("Content-Type: text/plain \n\n");
printf("CGI C Example \n");
}
save the content of example-c.c file and compile it with the following command:
$ sudo gcc -o /usr/lib/cgi-bin/example-c example-c.c
now you should be able to access your C compiled CGI script with: http://cgi-example.local/cgi-bin/example-c

5.4. C++

For C and C++ to work we will need to have a compiler installed. First, install compiler with:
$ sudo apt-get install build-essential
Once installed create a file example-cpp.c with a following code:
#include 
using namespace std;

int main()
{
        cout << "content-type: text/html" << endl << endl;
        cout << "

CGI C++ example

" << endl; return 0; }
save the content of example-cpp.c file and compile it with the following command:
$ sudo g++ -o /usr/lib/cgi-bin/example-cpp example-cpp.c
now you should be able to access your C compiled CGI script with: http://cgi-example.local/cgi-bin/example-cpp

6. Conclusion

As mentioned earlier the CGI is quite old and was largely superseded by different programing languages such as PHP, etc. However, as you can see, it is still relatively simple tool to use to automate your Linux administration tasks such as a remote custom monitoring for your Linux servers using a web browser.

https://www.youtube.com/watch?v=ELFdP7eEZ5w

Monday, March 9, 2015

Re-enable graphical root-login on 12.04 LTS

sudo passwd root
sudo sh -c 'echo "greeter-show-manual-login=true" >> /etc/lightdm/lightdm.conf'
Root won't show up as a user, but "Login" will, which is how you manually log in with users not shown in the greeter.
Rebooted and then you should be able to login as root.


OR

First of all, you must set root 's password.
sudo passwd root
Enter new UNIX password:
Now you have root password. Now activate the root account:
sudo usermod -U root
Then you should allow lightdm, gdm or kdm to allow logging in as root.
To enable this:

In GDM

Edit /etc/gdm/custom.conf file and include AllowRoot=true.

In KDM

Edit /etc/kde4/kdm/kdmrc file and change AllowRootLogin=false for AllowRootLogin=true.

In LightDM

This procedure enables the "Other" menu, so you can type the username root and login. You must edit/etc/lightdm/lightdm.conf and add greeter-show-manual-login=true. Then reboot.
Others session managers have other methods to accomplish this.
As you have noticed I gave the instructions but by any circumstance you should not run the GUI as root. Murphy's Law says that it is likely that you mess up somehow and will be annoyed starting again.

I played around enough, how do I go back?

To disable root login just type:
sudo passwd -dl root

Thursday, August 5, 2010

Overwriting the Master Boot Record after installing windows

Overwriting the Master Boot Record

IconsPage/IconWarning3.png
Existing MBR will be lost. In order to dual-boot your computer, we will be creating a new Master Boot Record to include all operating systems, including Ubuntu Linux and Microsoft Windows. This is to be expected.
Your Ubuntu partition is now mounted. You will need to continue Using the Terminal.
Verify if your partition is correct.
mount | tail -1 
You should see output similar to this:
/dev/sda2 on /media/0d104aff-ec8c-44c8-b811-92b993823444 type ext4 (rw,nosuid,nodev,uhelper=devkit)
Note the designation for the disk /dev/sda which you will be using later, and the directory in /media.
IconsPage/IconHint2.png Use Tab Completion in Terminal to complete the path. Hitting the key will automatically finish file names, directory locations, and other long or hard to type file names.
To make sure this is indeed the Ubuntu boot partition, run ls /media/0d104aff-ec8c-44c8-b811-92b993823444/boot, substituting 0d104aff-ec8c-44c8-b811-92b993823444 with your volume's UUID from before, which should output something like this:
config-2.6.18-3-686      initrd.img-2.6.18-3-686.bak  System.map-2.6.18-3-686
grub                     lost+found                   vmlinuz-2.6.18-3-686
initrd.img-2.6.18-3-686  memtest86+.bin
If what you have is not similar, unmount it and try another partition.
Now that everything is mounted, we just need to reinstall GRUB by specifying the correct directory and the correct drive name:
sudo grub-install --root-directory=/media/0d104aff-ec8c-44c8-b811-92b993823444 /dev/sda
If you get BIOS warnings try:
sudo grub-install --root-directory=/media/0d104aff-ec8c-44c8-b811-92b993823444 /dev/sda --recheck
IconsPage/warning.png Replace /dev/sda with the location you want to install GRUB on.
If all went well, you should see something like this:
Installation finished. No error reported.
This is the contents of the device map /boot/grub/device.map.
Check if this is correct or not. If any of the lines is incorrect,
fix it and re-run the script `grub-install'.

(hd0)   /dev/sda
Reboot, making sure to boot to your hard drive and not to the live CD. Grub should be installed and both Ubuntu and Windows should have been automatically detected and listed in the menu.
The Master Boot Record will execute Grub as the initial bootloader. The Windows bootloader is contained within the Windows partition and will then be chainloaded by the Grub bootloader.

For Grub Legacy

Please refer to the guide
https://help.ubuntu.com/community/DualBoot/Grub#Ubuntu%209.10%20&%20earlier

Wednesday, April 28, 2010

Increasing File upload Size in Moodle on Ubantu

By default the upload size is 2Mb to increase the File Size limit in moodle (eg. increase limit to 50Megabytes) you will need to make the following changes:
In your php folder edit the php.ini file and change the following values:

post_max_size = 50M
upload_max_filesize = 50M

Note: On windows I had to restart IIS but I am not sure if you will need to restart apache on windows or linux.
Next go into Moodle  (in this case it is version 1.9.5) as admin and change the drop down menu’s for the following to your required size:

Courses -> Course Default Settings -> Maximum Upload Size
Modules -> Activities -> Assignment -> Maximum Size

---------------------------------------------------------------------------------------------------------------------

Modifying the php.ini file

These instructions show you how to change the file upload size by editing your php.ini file.

Ubuntu Linux Instructions

These instructions assume that you have installed the standard Moodle package, PHP 5 and Apache 2 via apt-get and left it all as a default install. If you have compiled yourself I presume that you will know where your php.ini files are!
You need to edit the following three settings in your php.ini file located at: /etc/php5/apache2/
  • Type "sudo nano /etc/php5/apache2/php.ini"
  • Press Ctrl and W and type "post_max_size"
  • Change the value to the number of Mb you want your site to accept as uploads
  • Press Ctrl and W and type "upload_max_filesize"
  • Change the value to the number of Mb you want your site to accept as uploads
  • Press Ctrl and W and type "max_execution_time"
  • Change the value to 600
  • Press Ctrl and O
  • Press Ctrl and X
  • Type sudo /etc/init.d/apache2 restart
Your new file size limit should now appear in Administration > Security > Site Policies > Maximum uploaded file size 
-------------------------------------------------------------------------------------------------------------------------------

Modifying the apache config file

Ubuntu Linux Instructions

You may also need to edit the config.php file in the moodle directory:
  • Type "gksudo nautilus" to get root permissions
  • Navigate to /etc/moodle
  • Open apache.conf
  • Go to the "" section
  • Change "php_value upload_max_filesize = 2M" to a higher value
  • Change "php_value post_max_size = 2M" to a higher value
  • Go to the "" section
  • Change "php_value upload_max_filesize = 2M" to a higher value
  • Change "php_value post_max_size = 2M" to a higher value
  • Save file
  • Type sudo /etc/init.d/apache2 restart


Wednesday, April 21, 2010

"Hello world" in Python CGI Web Programming in 5 minutes

I have lotta Python programs for bioinformatics research. I wanted to put them onto the web. I only developed Web apps in PHP before. And it seemed to be a big pain for porting a Python program to the web. But, I figured out in 5 minutes.
First, you need an Apache server on your Linux/Mac OS/Windows box. If you are gonna use data base, you need a database server. There are tons of blogs addressing these issues. So I won't be gossipy here. Suppose everything we mention below happens on your server-even web browsing.
Second, configure cgi in Apache. There are many ways to run a Python program on a web/http interface. I think CGI is the easiest. Assume you are on your own server and using all default settings. On Ubuntu Linux 9.04, the default configuration file for your default website is /etc/apache2/sites-available/default Open it, find the part for cgi directory, and make it like this
ScriptAlias /cgi-bin/ /var/www/cgi-bin/
        
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
                AddHandler cgi-script .py                       # tell Apache to handle every file with .py suffix as a cgi program
                AddHandler default-handler .html .htm  # tell Apache to handle HTML files in regular way
        
The line
ScriptAlias /cgi-bin/ /var/www/cgi-bin/
specifies the path of cgi codes and the actual directory of your program. So when you type http://127.0.0.1/cgi-bin, the Apache will look into the directory /var/www/cgi-bin/ of your localhost.
Now restart your apache. On Ubuntu Linux, by default installation and configuration, it is
sudo /etc/init.d/apache2 restart
Third, write up a Hello, World! program. Save it as hello.py and give it execution privilege for anyone.
#!/usr/bin/env python
print "Content-Type: text/html"
print
print """\

First Python HTTP Programming 




Hello World!

"""
Now open http://127.0.0.1/cgi-bin/hello.py in your web browser and you shall see a hello world in it.
If you have any error, e.g., 500 error, please check your Apache log file. It shall tell you something. You can find errors like what you have seen from a standard Python interpreter.
Reference: http://webpython.codepoint.net/cgi_tutorial

Tuesday, April 20, 2010

How to:python programming under Ubuntu | Onecore

If you are new to python programming & you’re having ubuntu linux as your operating system then you can start programming easily as Ubuntu comes up with the command line version of python pre-installed.Ubuntu community develops its own tools and scripts under python,so you can learn a lot from the community.Community also offers bounties on some of its python projects.So in that way you can also get in touch with the python developers.
In python you have two choices to learn how to program,one is with command line version and another is IDLE.I’ll explain both these methods one by one.
Command Line

Command line version of python is already installed in ubuntu.To get into python’s interactive mode,Click on Appliations>Accesorries>Terminal.Then type ‘python’ without quotes,that will open up the interactive mode of the python.Now you can test your python scripts.

You can also write the program with help of text editor like gedit or emacs.Then save it with extension .py.Now if you want to execute that file,then open the terminal,move yourself to the directory where that .py file exists.Then type the following commands in terminal.

python hello.py

this will execute the program.Once program is executed you’ll return back to the command prompt.To exit from the python interpreter type the following:


import sys;sys.exit();
or you can use exit(); or CTRL-D

This will exit from the interpreter and return back to the terminal.

IDLE
If you don’t want to use the command line version of python then there is another choice “IDLE”.IDLE means interactive developement envirement.IDLE is a Python IDE that includes a GUI debugger based on pdb.IDLE is not installed in ubuntu by default,you have to install it from DVD.To install the IDLE,follow these steps.

Click on Application>Add-remove applications.


In Add-remove applcations select categary > Programming >then Select “IDLE” check-box point.Click OK.Then it will ask for the install disk and then installs it from there.

Now you can write your program with help of text editor like emacs/gedit.Once you’re done with it,save it with extension .py.Now right click “Open with” thenĂ‚ select >IDLE. OR you can open the file from IDLE.IDLE shows the colored syntax of your program.Now to run your program,follow these steps:


Click>RUN>Run Module.

This will execute the program in the IDLE.
(Graphical user interface-GUI for Python)
Once you learn the techniques of the text processing in python.Then you need to learn how to program applications in GUI.To program GUI based applcations you have many option like Wxpython,Tkinter,Pyqt etc.
Tkinter is a GUI widget set for Python.There are many introductory tutorials for the Tkinter on the web. Some of them are :
[1] Introduction to python

[2] Python and Tkinter Programming by John Grayson (Manning, 2000, ISBN 1-884777-81-3)
[3] Tkinter reference [Here]
WxPython is another GUI toolkit for the Python programming language which allows easy creation of graphical user interfaces.You can obtain more information about Wxpython here.http://wxpython.org
You can learn more from python community so check out the activestate’s “CookBook” where you can learn a lot about the python coding.
I hope above information helps.If you have any quesstion & suggestion or errors.Then please don’t hesitate to post it over here.