Ubuntu,Linux,Apache,MySQL,PHP,PHPMYADMIN,Java Servlets,JSP,Jakarta-Tomcat,oracle

Installing LAMP on Ubuntu 7.10 (Linux,Apache,MySQL,PHP,PHPMYADMIN)

Build Your Own Web Server ~
Quick & Easy Do it Yourself Installation ~ All within 24 hours !!!!

Apache 2 - Linux Web server

MySQL 5 - MySQL Database Server

PHP4/5 - PHP Scripting Language

phpMyAdmin - Web-based database admin software.

Webalizer - Website Traffic Analyzer

Mail Server - Postfix (MTA) with Dovecot IMAP/POP3 + Sasl Authentication

Squirrelmail - A web based email

VSFTP - A fast ftp server to upload files

Webmin - A freely available server control panel

ClamAV - Antivirus software.

A Firewall using IPtables.



Note: Linux + Apache + MySQL + PHP/Perl together commonly known as LAMP Server.





First, let us prepare a system that has a minimum requirement of Debian/Ubuntu version of linux with atleast 256MB of RAM available. Anything less than this minimum ram will cause lot of problems since we are running a server along especially mysql and webmin requires lot of RAM to run properly. Mysql will give you this nasty error "cannot connect to mysql.sock" if you dont have enough memory in your server.

I love debian/ubuntu based linux because of my enormous affinity towards this command apt-get. As a starter knowing this one command, It is so easy to install packages and you dont need to worry about package dependency and configuration. You need to buy a dedicated server or a VPS package if you want to setup your own server. If you want to experiment with the server and installation it is recommended to buy a vps package from various hosts. I prefer vpslink because of their pricing. Believe it or not it is so easy to install and configure your server yourself eventhough you are new are to linux and dedicated/vps hosting.

First download PuTTy if you are accessing your server through SSH. Just enter the IP of your server with root login to access your host. As you probably know, Webmin is a freely available server control panel and we will setup this once we have completed the LAMP server and Mail Server. Webmin makes more easier for us to fine tune our linux box.

Before proceeding to install, update the necessary packages with debian with this command.

apt-get install update

1. Installing Apache + PHP

Apache is one of the most famous web server which runs on most linux based servers. With just few commands you can configure apache to run with PHP 4 or PHP 5.

If you want to install PHP 4, just apt-get

apt-get install apache2 php4 libapache2-mod-php4

To install PHP5, just run the following on linux shell. Note that if you dont specify packages with '4', PHP5 will be automatically installed.

apt-get install apache2 php5 libapache2-mod-php5

Apache configuration file is located at: /etc/apache2/apache2.conf and your web folder is /var/www

To check whether php is installed and running properly, just create a test.php in your /var/www folder with phpinfo() function exactly as shown below.

nano /var/www/test.php

# test.php

</?php phpinfo(); ?/>

Point your browser to http://ip.address/test.php or http://domain/test.php and this should show all your php configuration and default settings.

You can edit necessary values or setup virtual domains using apache configuration file.

Enabling GD Library with PHP

If you want to use CAPTCHA or for dynamic image generation with php scripts for image verification to stop SPAM or automated robots, then it is absolutely necessary to get php gd library installed with php. Here is the command

apt-get install php5-gd

Thats it!! Point your browser to http://domain/test.php and the php configuration settings will show GD library will be enabled for PNG, GIF, JPG etc.

Enabling Mod Rewrite with .htaccess

Do you use mod-rewrite from apache to rewrite friendly URLs ?? This must be absolutely necessary for the rewrite module to get enabled in your apache, especially if your blog, forum script uses rewriting engine to generate friendly URLs in your site. Note that default apache2 installation does not come with mod-rewrite. Here is how you enable it. Issue the following command

# a2enmod rewrite

Once you run this command, apache will tell you that this rewrite module is enabled. You can find mod_rewrite enabled and show up in your test.php file.

I often experienced page not found 404 error with debian/ubuntu versions eventhough your apache runs with mod-rewrite. To fix this, you will need to edit the following file to make some changes.

nano /etc/apache2/sites-enabled/000-default

Find the following and change AllowOverride from None to All


Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
# Uncomment this directive is you want to see apache2's
# default start page (in /apache2-default) when you go to /
#RedirectMatch ^/$ /apache2-default/


Upload the .htaccess file to your server and restart apache. /etc/init.d/apache2 restart

Make sure your .htaccess file has 644 permission as otherwise you get permission denied error.

NOTE: I have often experienced .htaccess remaining invisible or disappearing problem when i uploaded any dotted files in the server (like .htaccess .ftpquota). Actually FTP clients do write .htaccess file but they do not show up when you upload. This happened with me using VSFTP. Here is how you fix

2. Installing MySQL Database Server

Installing mysql database server is always necessary if you are running a database driven ecommerce site. Remember running mysql server to a fair extend requires atleast 256mb of RAM in your server. So unless you are running database driven sites you dont absolutely need mysql. The following commands will install mysql 5 server and mysql 5 client.

apt-get install mysql-server mysql-client php5-mysql

Note: If you have already installed php4, you should make a slight change like this.

apt-get install mysql-server mysql-client php4-mysql

The configuration file of mysql is located at: /etc/mysql/my.cnf

Creating users to use MySQL and Changing Root Password

By default mysql creates user as root and runs with no passport. You might need to change the root password.

To change Root Password

mysql> USE mysql;
mysql> UPDATE user SET Password=PASSWORD('new-password') WHERE user='root';
mysql> FLUSH PRIVILEGES;


To Create User

You must never use root password, so you might need to create a user to connect to mysql database for a PHP script. Alternatively you can add users to mysql database by using a control panel like webmin or phpMyAdmin to easily create or assign database permission to users. We will install Webmin and phpmyadmin during later once we complete basic mail LAMP installation.

PhpMyAdmin Installation

PhpMyAdmin is a nice web based database management and administration software and easy to install and configure under apache. Managing databases with tables couldnt be much simpler by using phpmyadmin.

All you need to do is:

apt-get install phpmyadmin

The phpmyadmin configuration file is located at: /etc/phpmyadmin folder.

To setup under apache all you need to do is include the following line in /etc/apache2/apache2.conf

Include /etc/phpmyadmin/apache.conf

Now restart apache: /etc/init.d/apache2 restart

Point your browser to: http://domain/phpmyadmin

Thats it!! MySQL and phpMyAdmin is Ready !!! Login with your mysql root password and create users to connect to database from your php script.



References : http://www.mysql-apache-php.com

Linux Tutorial: Java Servlets, JSP, Jakarta-Tomcat, a Database (PostgreSQL or MySQL), Apache and Linux







    JAVA Installation/Configuration:

    In order to write and compile a JAVA programs, applets or servlets one must download the JAVA Development Kit (JDK) which provides a compiler, class libraries and tools to compile and run JAVA code. In this tutorial we will use the Sun JDK but I'm sure any will do. See YoLinux JAVA for a list of available JDK's for Linux.


    Download the Sun JDK:



    Note: The Java Runtime Environment (JRE) will be adequate to configure the server environment but the Software Development Kit (SDK) is required if one wants to write and compile JAVA programs. The "Forte SDK" is a full blown interactive GUI driven development environment. The SDK is available in RPM and tar format.

    • Download j2sdk-1_4_0-linux-i386-rpm.bin

    • chmod +x j2sdk-1_4_0-linux-i386-rpm.bin

    • ./j2sdk-1_4_0-linux-i386-rpm.bin

    • rpm -ivh j2sdk-1_4_0-fcs-linux-i386.rpm


    SDK installed in /usr/java/j2sdk1.4.0/.


    Configuration:



    Set the environment variable PATH. Add statement to $HOME/.bash_profile or $HOME/.bashrc or shell script which controls the environment.
    PATH=/usr/java/j2sdk1.4.0/bin:$PATH
    export PATH
    export JAVA_HOME=/usr/java/j2sdk1.4.0
    export CLASSPATH=/usr/java/j2sdk1.4.0/lib/tools.jar:/usr/java/j2sdk1.4.0/jre/lib/rt.jar


    The shell script may be re-executed with the command: . .bashrc

    [Potential Pitfall]: Java SDK 1.4.0_03 now requires the current working directory to be listed to find files. Add reference to current directory ":./" to CLASSPATH.


       export CLASSPATH=/usr/java/j2sdk1.4.0_03/lib/tools.jar:/usr/java/j2sdk1.4.0_03/jre/lib/rt.jar:./




    Test:



    Use the following test program: Test.java


    public class Test
    {
    public static void main(String[] args)
    {
    System.out.println("Hello world");
    }
    }




    Compile: javac Test.java

    (or /usr/java/j2sdk1.4.0/bin/javac Test.java)

    Note that the file name and the class name are the same. This became a requirement in JDK 1.4. The result of the compile is the file: Test.class


    Run:




    [prompt]$ java Test
    Hello world


    (or /usr/java/j2sdk1.4.0/bin/java Test)




    Links:












    Jakarta Project - Tomcat:

    Tomcat is the JAVA "container" or processor for Java Servlets and Java Server Pages (JSP). Note also that JAVA must be installed in order for Tomcat to operate. (See previous section above) This tutorial will focus on the use of Tomcat with Apache but it should be noted that the default Tomcat installation enables Tomcat to be a stand-alone http web server and servlet container.


    The Jakarta Project: Apache Tomcat JAVA Servlet and JSP container home page


    Download: Tomcat 4.0 RPMs (Release: Catalina. Servlet 2.3 and JSP 1.2 specifications.)



    • regexp-1.2-1.noarch.rpm

    • servletapi4-4.0.3-1.noarch.rpm - servlet.jar files

    • xerces-j-1.4.4-1.noarch.rpm - Xerces is an XML parser available from the Apache Software Foundation.

    • tomcat4-webapps-4.0.3-1.noarch.rpm

    • tomcat4-4.0.3-1.noarch.rpm


    Install: rpm -ivh regexp-1.2-1.noarch.rpm servletapi4-4.0.3-1.noarch.rpm tomcat4-webapps-4.0.3-1.noarch.rpm tomcat4-4.0.3-1.noarch.rpm xerces-j-1.4.4-2.noarch.rpm

    Symlinks generated from /usr/share/java/... to /var/tomcat4/


    Documentation:



    Config file: /etc/tomcat4/conf/tomcat4.conf



    Change the following reference to JAVA_HOME to:


    JAVA_HOME=/usr/java/j2sdk1.4.0




    Notes:

    • JAVA environment variable seem to be set here rather than in a start script.

    • Environment variable options:

      • CATALINA_HOME: Tomcat installation directory

      • CATALINA_OPTS example: export CATALINA_OPTS="-Djava.ext.dirs=/dummy_dir"

        Command line switches to pass to the Java Runtime Environment

      • CLASSPATH: Optional - Runs without this but I used it when adding custom JAR files (CLASSPATH=$CLASSPATH:/var/tomcat4/lib:/usr/share/pgsql:/usr/java/jre/lib:/usr/java/j2sdk1.4.0/lib:)



    • Tomcat will execute as user tomcat4




    Config files:



    • /var/tomcat4/conf/server.xml - Servlet container configuration: Defines services offered, TCP port numbers for services, SSL, JDBC configuration for MySQL (user/passwd), PostgreSQL, Oracle, ODBC, etc. Later we will configure Apache to use one of these services.

    • /var/tomcat4/conf/web.xml - Tomcat's built-in http server global config file: Defines servlets to be processed by Tomcat. Some are predefined to perform pre-configured tasks like SSI, JSP, etc.

    • /var/tomcat4/conf/catalina.policy - Security policy permissions for Tomcat

    • /var/tomcat4/webapps/examples/WEB-INF/web.xml - Application specific config file.


    Later we will use Tomcat with Apache's http server.

    Start: service tomcat4 start (or: /etc/init.d/tomcat4 start)



    Or execute init script directly: /etc/rc.d/init.d/tomcat4 start

    To add to init boot process: /sbin/chkconfig --add tomcat4

    Tomcat (and listener socket waiting for requests) will start before Apache so that Apache will have a socket to which it can connect and communicate. Tomcat will also stop after Apache. Have patience as there is a lot of stuff going on. Use the command top to look at all the JAVA processes being initiated. When the top processes are no longer Java, then Tomcat is probably ready.

    [Potential Pitfall]: Note that the command service tomcat4 stop was not always successful in stopping all Java processes. Use the command ps -auwx grep tomcat to measure success.




    Test:




    Note:

    • Port 8180 is defined in /var/tomcat4/conf/server.xml.

      If changed you must restart Tomcat: service tomcat4 restart (or: /etc/init.d/tomcat4 restart)

      If using stand-alone and not with Apache, change this to port 80.


      <Connector className="org.apache.catalina.connector.http.HttpConnector"
      port="8180" minProcessors="5" maxProcessors="75"
      enableLookups="true" redirectPort="8543"
      acceptCount="10" debug="0" connectionTimeout="60000"/>





    • Tomcat includes an http server which this test is using. We will be integrating it into the Apache environment so that Apache will be the default server and Tomcat will be processing JSP's and Servlets.

    • Tomcat Log files: /var/tomcat4/logs/

    • Application directories:

      • Servlets and utility classes: /var/tomcat4/webapps/examples/WEB-INF/classes/

      • JAR files and JDBC drivers: /var/tomcat4/webapps/examples/WEB-INF/lib/






    Tomcat Manager:

    Tomcat comes with a web management interface. (It's weak, so don't expect much.)

    The access is controlled by the file /var/tomcat4/conf/tomcat-users.xml Add a manager definition in tomcat-users.xml:


    <tomcat-users>
    <user name="tomcat" password="tomcat" roles="tomcat" />
    <user name="role1" password="tomcat" roles="role1" />
    <user name="both" password="tomcat" roles="tomcat,role1" />
    <user name="admin1" password="supersecret" roles="standard,manager,tomcat,role1" /> - Added this line with "manager role" <user name="admin2" password="supersecret2" roles="admin,manager,provider" /> - See: Sun Java Getting Started Guide </tomcat-users>

    Then restart: service tomcat4 restart


    Use URL to list web applications: http://localhost:8180/manager/list


    Note:





    Notes:



    • The rpm tomcat4-webapps-4.0.3-1.noarch.rpm will install many examples (JAVA source .java and compiled .class) in /var/tomcat4/webapps/.

    • If all you are running are static web pages, JSP and servlets then all you may need is Tomcat as it is an http server. If you need the functionality of Apache to execute CGI, php and support for many of the Apache features then read ahead to learn how to use Apache to invoke Tomcat to support JSP and Java Servlets.











    JAVA Servlet Example:

    File: MyHelloWorld.java










    import java.io.*;
    import java.text.*;
    import java.util.*;
    import javax.servlet.*;
    import javax.servlet.http.*;

    public class MyHelloWorld extends HttpServlet {

    public void doGet(HttpServletRequest request,
    HttpServletResponse response)
    throws IOException, ServletException
    {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<body>");
    out.println("<head>");
    out.println("<title>Hello World!</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h1>Hello World!</h1>");
    out.println("</body>");
    out.println("</html>");
    }
    }




    Create Source File: /var/tomcat4/webapps/examples/WEB-INF/classes/MyHelloWorld.java

    Set CLASSPATH environment variable:


    export CLASSPATH=$CLASSPATH:/var/tomcat4/common/lib/servlet.jar

    OR export CLASSPATH=/usr/java/j2sdk1.4.0/lib/tools.jar:/usr/java/j2sdk1.4.0/jre/lib/rt.jar:/var/tomcat4/common/lib/servlet.jar

    Note: /var/tomcat4/common/lib/servlet.jar is soft linked to /usr/share/java/servlet-2.3.jar.

    Compile:




    [prompt]$ cd /var/tomcat4/webapps/examples/WEB-INF/classes/
    [prompt]$ javac MyHelloWorld.java

    (OR /usr/java/j2sdk1.4.0/bin/javac MyHelloWorld.java )




    This creates file: /var/tomcat4/webapps/examples/WEB-INF/classes/MyHelloWorld.class

    Note:



    • Java class com.mycompany.mypackage.MyServlet would be stored in /WEB-INF/classes/com/mycompany/mypackage/MyServlet.class

    • Libraries (JAR files, JDBC drivers, etc) held in /WEB-INF/lib/ if one follows standard configuration. (I took the easy path instead.)


    Tomcat Test: http://localhost:8180/examples/servlet/MyHelloWorld

    Don't expect a lot. It just generates a web page dynamically which states "Hello World".


    Note:



    • The mapping of path /var/tomcat4/webapps/examples/WEB-INF/classes/ to the URL /examples/servlet/ is defined in /var/tomcat4/conf/web.xml. Look for the XML tag "<servlet-mapping>".

    • Many examples with source code are included with Tomcat. See: http://localhost:8180/examples/servlets/index.html











    Apache - Tomcat Configuration:

    Apache is a fast and configurable web server. This section covers using Apache as the primary web server but using Tomcat to process JSP and Servlets. This configuration uses the AJP (Apache JServ Protocol) connector element (mod_jk) to perform this task. The Apache module mod_jk will send servlet requests to Tomcat using TCP/IP sockets for communications. Module mod_jk works with Tomcat versions 3.x and 4.x.


    There are numerous Apache connection modules:





































    Connector NameApache Module NameConnection ProtocolTomcat Version
    AJPmod_jkAJP1.2,AJP1.33, 4
    Coyote JK2mod_jkAJP/JK1.3,AJP/JK1.44.1
    WARPmod_webappWARP4
    JSERVmod_jserv??old





    AJP mod_jk Installation:



    • First check to see if Apache is compiled to support DSO: /usr/sbin/httpd -l

      If one of the lines contains the result mod_so.c then support is available.

    • Download mod_jk RPM: http://jakarta.apache.org/builds/jakarta-tomcat-4.0/release/v4.0.3/rpms/mod_jk-1.3-1.0-1.4.0.2.i386.rpm

      This module is dependent on the version of Apache one is using. For this tutorial I am using Apache 1.3.



    • Download EAPI version of mod_jk: http://jakarta.apache.org/builds/jakarta-tomcat/release/v3.3/bin/linux/i386/mod_jk-3.3-ap13-eapi.so

      The default Red Hat compiled Apache uses an API called EAPI which is incompatible with the version of mod_jk.so included in the RPM. Download and rename as /usr/lib/apache/mod_jk.so. This will overwrite the version installed by the RPM. The RPM is still required for the configuration files.



    • Or build mod_jk from source: http://jakarta.apache.org/builds/jakarta-tomcat-4.0/release/v4.0.3/src/jakarta-tomcat-connectors-4.0.2-01-src.tar.gz

      • Unpack: tar -xzf jakarta-tomcat-connectors-4.0.2-01-src.tar.gz

      • cd jakarta-tomcat-connectors-4.0.2-01-src/jk/native/apache-1.3/

      • Edit script: build-unix.sh

        Change lines:

        • JAVA_HOME=/usr/java/j2sdk1.4.0

        • APACHE_HOME=/etc/httpd

        • APXS=/usr/sbin/apxs

        • cp mod_jk.so /usr/lib/apache



      • Compile: . build-unix.sh




    Configuration File Modifications:


    The configuration file changes discussed below relate to those released with the RPM mod_jk-1.3-1.0-1.4.0.2.i386.rpm.

    • Define TCP/IP ports will be used for communications between Apache and Tomcat.

      • See Tomcat-Apache config file: /etc/httpd/conf/workers.properties

        (A separate host may also be specified.) (If you are defining multiple JVM's/servlet processors, used in virtual hosting, each must be assigned a different port.)

      • Tomcat services are defined in /var/tomcat4/conf/server.xml (See "Connector" definition) and in the Apache include file mod_jk.conf. Resource parameters may also be set to control the maximum number of threads which can be spawned.



    • Apache configuration to define:

      • module to load for communication with Tomcat. (Add to httpd.conf)

      • path (URL of servlets) and/or file types (JSP file types) which are destined for Tomcat processing. All requests in the given path (servlet location) or request of a given file type (i.e. *.jsp) will then be processed by Tomcat. (See JkMount directive in Apache include file mod_jk.conf) Everything else is served by Apache.




    Modify: /etc/httpd/conf/workers.properties










    workers.tomcat_home=/var/tomcat4             - Changed. Default was /var/tomcat3  workers.java_home=/usr/java/j2sdk1.4.0       - Changed to reflect location of JDK  ps=/  worker.list=ajp12, ajp13    # Define a worker named ajp12 and of type ajp12
    # Note that the name and the type do not have to match.
    #
    worker.ajp12.port=8007
    worker.ajp12.host=localhost
    worker.ajp12.type=ajp12
    worker.ajp12.lbfactor=1

    # Define a worker named Ajp13 # worker.ajp13.port=8009 worker.ajp13.host=localhost worker.ajp13.type=ajp13 worker.ajp13.lbfactor=1 worker.loadbalancer.type=lb worker.loadbalancer.balanced_workers=ajp12, ajp13 # Defining a worker named inprocess and of type jni worker.inprocess.type=jni # Additional class path components. worker.inprocess.class_path=$(workers.tomcat_home)$(ps)server$(ps)lib$(ps)tomcat-ajp.jar - Changed this line to match location of library. worker.inprocess.cmd_line=start worker.inprocess.class_path=$(workers.java_home)$(ps)lib$(ps)tools.jar - This line based on something I read. (Also modified to match installation) # Unix - Sun VM or blackdown worker.inprocess.jvm_lib=$(workers.java_home)$(ps)jre$(ps)lib$(ps)i386$(ps)server$(ps)libjvm.so - Changed this line to match location of library. # Setting the place for the stdout and stderr of tomcat worker.inprocess.stdout=$(workers.tomcat_home)$(ps)logs$(ps)inprocess.stdout worker.inprocess.stderr=$(workers.tomcat_home)$(ps)logs$(ps)inprocess.stderr

    The effect of this configuration is to define socket listeners and communication protocols between Apache and Tomcat.


    Add to: /var/tomcat4/conf/server.xml



    Within the XML tags:
    <Service name="Tomcat-Standalone">
    ...
    </Service>


    Define listener:






        <Connector className="org.apache.ajp.tomcat4.Ajp13Connector"
    port="8007" minProcessors="5" maxProcessors="75"
    acceptCount="10" debug="0"/>

    <Connector className="org.apache.ajp.tomcat4.Ajp13Connector"
    port="8009" minProcessors="5" maxProcessors="75"
    acceptCount="10" debug="0"/>


    Add definitions for connectors on ports 8007 and 8009 for workers.

    For some reason the listener definition has no effect within the "Service" tag identified as "Tomcat-Apache". (I don't know why)


    Add to: /etc/httpd/conf/httpd.conf










    LoadModule jk_module /usr/lib/apache/mod_jk.so  OR  LoadModule jk_module         modules/mod_jk.so  ...  ...  AddModule mod_jk.c  ...  ...  Include /etc/httpd/conf/mod_jk.conf                  

    Notes:

    • I added the "LoadModule" statement after the list of all the other "LoadModule" statements.

    • The statement "AddModule mod_jk.c" is placed after the line "ClearModuleList" and after all the "AddModule" declarations. Why? I don't know why position is important, but when listed as the first "AddModule" entry after "ClearModuleList", it would not work.

    • The statement modules/mod_jk.so is a relative reference from /etc/httpd/. The directory /etc/httpd/modules/ is soft linked to /usr/lib/apache/




    File: /etc/httpd/conf/mod_jk.conf (Change references of "tomcat" to "tomcat4" throughout the file, otherwise the default file is ok.)










    <IfModule mod_jk.c>

    JkWorkersFile /etc/httpd/conf/workers.properties
    JkLogFile /var/log/httpd/mod_jk.log
    JkLogLevel info

    # Root context mounts for Tomcat
    # Format: JkMount URL_PREFIX WORKER_NAME JkMount /*.jsp ajp13 JkMount /servlet/* ajp13 # The following line makes apache aware of the location of the /examples contextAlias /examples "/var/tomcat4/webapps/examples"
    <Directory "/var/tomcat4/webapps/examples">
    Options Indexes FollowSymLinks
    </Directory>

    # The following line mounts all JSP files and the /servlet/ uri to tomcat
    JkMount /examples/servlet/* ajp13
    JkMount /examples/*.jsp ajp13

    # The following line prohibits users from directly access WEB-INF
    <Location "/examples/WEB-INF/">
    AllowOverride None
    deny from all
    </Location>

    ...

    </IfModule>




    Virtual host example: (Just an FYI. I never used virtual hosting but wanted to let everyone know it is available.)



    Configure a new JVM definition to handle requests separately.
    # First Virtual Host.           - Virtual Host example not in default file    <VirtualHost 10.0.0.1:80>  DocumentRoot /web/host1  ServerName host1.yolinux.com  <IfModule mod_jk.c>  JkMount /*.jsp ajp13  JkMount /servlet/* ajp13  <Location "/examples/WEB-INF/">      AllowOverride None      deny from all  </Location>  </IfModule>  </VirtualHost>            

    Ajpv13 is the protocol used when Apache communicates with Tomcat. This file establishes the sockets and enables this communication process.

    If using a virtual host configuration then file /etc/httpd/conf/workers.properties must have the following entries added:


    ...
    <!-- Request Interceptor for jumpers -->
    <RequestInterceptor
    className="com.yolinux.servlet.jumpersInterceptor" />
    ...
    <Host name="host1.yolinux.com" >
    <Context path=""
    docBase="/web/host1/html" />
    </Host>




    Run:



    • Start Tomcat first: service tomcat4 start

    • Start Apache: service httpd start

      After Apache has started, one may perform a syntax check of the Apache configuration files with the following command:

      [root prompt]# /usr/sbin/apachectl configtest

      If sucessful it should return the statement: Syntax OK

      [Potential Pitfall]: If you get the following error:

      [error] Cannot remove module mod_jk.c: not found in module list

      It is most likely that the placement of the aditional lines in /etc/httpd/conf/httpd.conf is incorrect. The additional lines must be placed accoring to above directions.

    • Test with the URL:

      Note that we are no longer referencing port 8180 but are referencing a directory which Apache defers to Tomcat for processing but Apache handles the request from the browser.


    Log Files:



    • Apache:

      • /var/log/httpd/access_log

      • /var/log/httpd/error_log



    • Tomcat: /var/tomcat4/logs/...

    • Apache-Tomcat module mod_jk: /var/log/httpd/mod_jk.log


    Links:












    The Database:

    We will cover connectivity to two databases:



    1. PostgreSQL

    2. MySQL


    Note: If connecting to Oracle, use the JDBC driver: oracle.jdbc.driver.OracleDriver


    1) PostgreSQL:


    Install and configure a database. See: YoLinux Tutorial: PostgreSQL and Linux


    JDBC and PostgreSQL JAR files: The "CLASSPATH" variable can set the Java runtime environment so that it will find the appropriate Java libraries (JAR files). Environment variables for Tomcat can be set in /etc/tomcat4/conf/tomcat4.conf. One may also set the CLASSPATH variable to include PostgreSQL JDBC JAR files. I did not set the CLASSPATH environment variable in the configuration file but instead employed the default path by performing the following steps:


    List of PostgreSQL JDBC drivers:




    [prompt]# rpm -ql postgresql-jdbc-7.1.3-2
    /usr/share/pgsql/jdbc7.0-1.1.jar
    /usr/share/pgsql/jdbc7.1-1.2.jar




    Place JDBC JAR libraries in path where they can be found:



    cp /usr/share/pgsql/jdbc7.1-1.2.jar /var/tomcat4/lib


    JAVA Servlet run under Tomcat, accessing PostgreSQL using JDBC:


    Java Servlet source file:








    // File: ShowBedrock.java

    /* A servlet to display the contents of the PostgreSQL Bedrock database */

    import java.io.*;
    import java.sql.*;
    import java.text.*;
    import java.util.*;
    import javax.servlet.*;
    import javax.servlet.http.*;

    public class ShowBedrock extends HttpServlet
    {
    public String getServletInfo()
    {
    return "Servlet connects to PostgreSQL database and displays result of a SELECT";
    }

    private Connection dbcon; // Connection for scope of ShowBedrock

    // "init" sets up a database connection
    public void init(ServletConfig config) throws ServletException
    {
    String loginUser = "postgres";
    String loginPasswd = "supersecret";
    String loginUrl = "jdbc:postgresql://localhost/bedrock";

    // Load the PostgreSQL driver
    try
    {
    Class.forName("org.postgresql.Driver");
    dbcon = DriverManager.getConnection(loginUrl, loginUser, loginPasswd);
    }
    catch (ClassNotFoundException ex)
    {
    System.err.println("ClassNotFoundException: " + ex.getMessage());
    throw new ServletException("Class not found Error");
    }
    catch (SQLException ex)
    {
    System.err.println("SQLException: " + ex.getMessage());
    }
    }

    // Use http GET

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
    {
    response.setContentType("text/html"); // Response mime type

    // Output stream to STDOUT
    PrintWriter out = response.getWriter();

    out.println("<HTML><Head><Title>Bedrock</Title></Head>");
    out.println("<Body><H1>Bedrock</H1>");

    try
    {
    // Declare our statement
    Statement statement = dbcon.createStatement();

    String query = "SELECT name, dept, ";
    query += " jobtitle ";
    query += "FROM employee ";

    // Perform the query
    ResultSet rs = statement.executeQuery(query);

    out.println("<table border>");

    // Iterate through each row of rs
    while (rs.next())
    {
    String m_name = rs.getString("name");
    String m_dept = rs.getString("dept");
    String m_jobtitle = rs.getString("jobtitle");
    out.println("<tr>" +
    "<td>" + m_name + "</td>" +
    "<td>" + m_dept + "</td>" +
    "<td>" + m_jobtitle + "</td>" +
    "</tr>");
    }

    out.println("</table></body></html>");
    statement.close();
    }
    catch(Exception ex)
    {
    out.println("<HTML>" +
    "<Head><Title>" +
    "Bedrock: Error" +
    "</Title></Head>\n<Body>" +
    "<P>SQL error in doGet: " +
    ex.getMessage() + "</P></Body></HTML>");
    return;
    }
    out.close();
    }
    }




    Notes:



    • String loginUrl = "jdbc:postgresql://localhost/bedrock";

      The format for this is jdbc:postgresql:host-name-of-server:port/database-name

      Examples:

      • jdbc:postgresql:bedrock - bedrock is the PostgreSQL database name. See PostgreSQL tutorial.

      • jdbc:postgresql://localhost/bedrock

      • jdbc:postgresql://localhost:5432/bedrock - Default PostgreSQL standard port number is 5432



    • public void doGet(...

      Using http GET request. For http POST operations use doPost

    • String m_name = rs.getString("name");

      One may also use database field names or field numbers. i.e.

      String m_name = rs.getString(1);

      String m_dept = rs.getString(2);


    Set CLASSPATH environment variable:


    export CLASSPATH=$CLASSPATH:/var/tomcat4/lib/jdbc7.1-1.2.jar

    OR export CLASSPATH=/usr/java/j2sdk1.4.0/lib/tools.jar:/usr/java/j2sdk1.4.0/jre/lib/rt.jar:/var/tomcat4/common/lib/servlet.jar:/var/tomcat4/lib/jdbc7.1-1.2.jar

    Compile:




    [prompt]# cd /var/tomcat4/webapps/examples/WEB-INF/classes
    [prompt]# javac ShowBedrock.java

    (OR /usr/java/j2sdk1.4.0/bin/javac ShowBedrock.java )




    PostgreSQL Configuration: /var/lib/pgsql/data/postgresql.conf

    Set: tcpip_socket = true

    This allows JDBC to connect to PostgreSQL.


    Test:



    Results:




    Bedrock

























    Fred Flinstone Quarry Worker Rock Digger
    Wilma Flinstone Finance Analyst
    Barney Rubble Sales Neighbor
    Betty Rubble IT Neighbor





    JDBC/PostgreSQL Links:






    2) MySQL:


    Install and configure a database: YoLinux Tutorial: MySQL and Linux


    Download JDBC MM.MySQL JAR file:



    Un-JAR and place MM.MySQL JDBC JAR libraries in path where they can be found:




    [prompt]# /usr/java/j2sdk1.4.0/bin/jar xf mm.mysql-2.0.13-you-must-unjar-me.jar
    OR if your paths are set properly:
    [prompt]# jar xf mm.mysql-2.0.13-you-must-unjar-me.jar

    [prompt]# cd mm.mysql-2.0.13/
    [prompt]# cp mm.mysql-2.0.13-bin.jar /var/tomcat4/lib




    "GRANT ALL PRIVILEGES ON bedrock to 'user@hostname' identified by 'password';

    FLUSH PRIVILEGES

    where hostname is localhost.localdomain (not localhost on default Red Hat installation)


    JAVA Servlet run under Tomcat, accessing MySQL using JDBC:


    Java Servlet source file: (Note that it does the same thing as the PostgrSQL example above but it is written with a different style.)








    // File: ShowBedrock.java

    /* A servlet to display the contents of the MySQL Bedrock database */

    import java.io.*;
    import java.net.*;
    import java.sql.*;
    import java.text.*;
    import java.util.*;
    import javax.servlet.*;
    import javax.servlet.http.*;

    public class ShowBedrock extends HttpServlet
    {
    public String getServletInfo()
    {
    return "Servlet connects to MySQL database and displays result of a SELECT";
    }

    // Use http GET

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
    {
    String loginUser = "Dude1";
    String loginPasswd = "SuperSecret";
    String loginUrl = "jdbc:mysql://localhost:3306/bedrock";

    response.setContentType("text/html"); // Response mime type

    // Output stream to STDOUT
    PrintWriter out = response.getWriter();

    out.println("<HTML><HEAD><TITLE>Bedrock</TITLE></HEAD>");
    out.println("<BODY><H1>Bedrock</H1>");

    // Load the mm.MySQL driver
    try
    {
    Class.forName("org.gjt.mm.mysql.Driver");
    Connection dbcon = DriverManager.getConnection(loginUrl, loginUser, loginPasswd);
    // Declare our statement
    Statement statement = dbcon.createStatement();

    String query = "SELECT name, dept, ";
    query += " jobtitle ";
    query += "FROM employee ";

    // Perform the query
    ResultSet rs = statement.executeQuery(query);

    out.println("<TABLE border>");

    // Iterate through each row of rs
    while (rs.next())
    {
    String m_name = rs.getString("name");
    String m_dept = rs.getString("dept");
    String m_jobtitle = rs.getString("jobtitle");
    out.println("<tr>" +
    "<td>" + m_name + "</td>" +
    "<td>" + m_dept + "</td>" +
    "<td>" + m_jobtitle + "</td>" +
    "</tr>");
    }

    out.println("</TABLE>");

    rs.close();
    statement.close();
    dbcon.close();
    }
    catch (SQLException ex) {
    while (ex != null) {
    System.out.println ("SQL Exception: " + ex.getMessage ());
    ex = ex.getNextException ();
    } // end while
    } // end catch SQLException

    catch(java.lang.Exception ex)
    {
    out.println("<HTML>" +
    "<HEAD><TITLE>" +
    "Bedrock: Error" +
    "</TITLE></HEAD>\n<BODY>" +
    "<P>SQL error in doGet: " +
    ex.getMessage() + "</P></BODY></HTML>");
    return;
    }
    out.close();
    }
    }




    Compile:


    [prompt]# export CLASSPATH=/var/tomcat4/common/lib/mm.mysql-2.0.13-bin.jar:$CLASSPATH
    [prompt]# cd /var/tomcat4/webapps/examples/WEB-INF/classes
    [prompt]# javac ShowBedrock.java

    (ORexport CLASSPATH=/usr/java/j2sdk1.4.0/lib/tools.jar:/usr/java/j2sdk1.4.0/jre/lib/rt.jar:/var/tomcat4/common/lib/servlet.jar:/var/tomcat4/lib/mm.mysql-2.0.13-bin.jar )
    (OR /usr/java/j2sdk1.4.0/bin/javac ShowBedrock.java )




    Test:



    Notes:



    • [Potential Pitfall]: The MySQL "user" table will define users and their host. I made the mistake of using "localhost" when the host name returned by the Unix command "hostname" was different. When the servlet tried to connect to the database it was refused.


      [prompt]$ hostname
      superserver [prompt]$ mysql -h localhost -u root -p
      ...
      mysql> use mysql
      mysql> select user,host from user;
      +-------+------------+
      user host
      +-------+------------+
      root
      Dude1 localhost
      +-------+------------+
      mysql> update user set Host='superserver' where User='Dude1';
      Query OK, 1 row affected (0.00 sec)
      Rows matched: 1 Changed: 1 Warnings: 0








    Links:











    JAVA JDBC:

    JAVA JDBC programs require the package: java.sql. Contained within this package are:



    • Classes:

      • Date

      • DriverManager

      • DriverPropertyInfo

      • Time

      • Timestamp

      • Types



    • Interfaces:

      • CallableStatement

      • Connection

      • DatabaseMetaData

      • Driver

      • PreparedStatement

      • ResultSet

      • ResultMetaData

      • Statement




    JDBC Links:











    JAVA Server Pages (JSP):

    Basic JSP elements:



    • <jsp: ... %> : Define/execute Java statements.

      Example:
              Year: is  <jsp:getProperty name="clock" property="year"/>     <BR>     Month: is  <jsp:getProperty name="clock" property="month"/>      


    • <% ... %> : Execute Java statements. Nothing displayed.

      Example: <% numguess.reset(); %>

    • <%= ... %> : Execute Java expression and place results here.

      Example: Calendar:<%= table.getDate() %>

    • <%@ ... %> : Declare Java variable or method.

      Example:
         <%@ page language="java" import="cal.*" %>
      <jsp:useBean id="table" scope="session" class="cal.TableBean" />




    JSP's are rarely self contained. JSP's most often require use of classes and methods defined in Java programs.

    The samples delivered with Tomcat show numerous JSP examples and the source code:












    Links:


    References : http://www.yolinux.com/TUTORIALS/LinuxTutorialTomcat.html#JAVAJDK

    install Ubuntu on my PC and dual boot with Windows XP.

    About a month ago I decided to give Ubuntu ( a very popular distribution of Linux) a go and try it out to see what all the fuss is about. Since I am a big fan of virtualization I decided that I would use Microsoft Virtual PC 2007 to minimize the risk. I could then install it without any chance of messing up my main Windows machine. After a bit of work, I managed to create an Ubuntu Virtual PC and almost everything worked ok.


    The main issue I had was that it wouldn’t recognise my sound card on my home pc which was pretty annoying since I couldn’t listen to music or watch a dvd etc. I was convinced that the issue was with Ubuntu so I spent a while reinstalling ALSA drivers and all of sorts of Linux commands but to no avail.


    I also download a VMWare Ubuntu Image in order to test that but my sound card still was not detected. While I enjoyed working with Ubuntu I wasn’t completely happy with no sound so I investigated further. I ran the Ubuntu Live CD on my home pc and I was surprised to see that my sound card was actually recognised then. So, it turned out to be the virtualization technology that could not recognize my sound card and not Ubuntu. So this left me with only one solution, install Ubuntu on my PC and dual boot with Windows XP.


    I didn’t want to take the chance with the full Ubuntu install while trying to protect my Windows XP setup. The thought of partitioning my drive and messing with boot loaders didn’t really inspire me with confidence. I had come across Wubi a while back on Lifehacker and I decided to give it a try. Wubi allows you to install Ubuntu from within Windows using a familiar Windows installer interface and if you ever want to remove Ubuntu you can just run the uninstaller from within Windows. Pretty neat.


    Below I describe step by step how to install Ubuntu using Wubi:


    1. Make a backup of any important files on your PC. (Just in case)


    2. I recommend downloading the Ubuntu (Alternate) ISO file seperately, the Wubi installer will do this for you but that is just my preference. If you do download the ISO seperately, then just place it in the same folder as the Wubi installer and it will be detected automatically.

    Note: Wubi uses the Alternate ISO, available at
    http://ftp.ussg.iu.edu/linux/ubuntu-releases/feisty/


    3. Run the Wubi Installer, and you will see the following screen. (Don’t click Next yet)


    wubi-main


    4. Before you do anything, click on the Settings button and modify the settings based on your preferences. Since Wubi installs your Ubuntu system to a virtual disk file you can could which drive to install it on. Don’t worry it doesn’t do any partitioning or anything.


    wubi-advanced


    System Size: This will be size of your system disk where all Ubuntu applications will be installed to, if you plan of installing plenty of software then you will need to allow plenty of space for this drive.

    Home Size: This is the size of your local folder where you personal preferences are stored.

    Swap Size: This is the size that you wish to allocate to your swap file ( if you don’t know what this is then leave it at the default).

    Installtion Drive: This is the drive where you want to intall the Ubuntu virtual disk drives to, it shows the space that you will have remaining on that drive based on your settings.

    Keyboard: Your keyboard preference

    Desktop Environment: Choose Ubuntu


    Once you are happy with your settings, click the Next button.


    5. Enter your preferred language, username and password and click Next


    The installer will proceed to install the necessary support files prior to the Ubuntu installation.


    6. When prompted to reboot, then choose Reboot Now and click Finish.


    wubi-reboot


    7. Once you PC reboots, the actual Ubuntu installation will begin.


    8. Proceed with the installation selecting defaults where you are unsure of the selection.


    9. When you boot up your computer from now onwards you will be given the option to choose either Windows XP or Ubuntu.


    The installation went very smooth for me and now I am happily using Ubuntu for web browsing, watching DVDs, listening to music, blogging, etc.

    There is the odd time that I have to boot into Windows to use some application for which I don’t have an equivalent in Ubuntu.


    I plan to do a few more posts in the near future about Ubuntu v’s Windows XP (application by application) and also some posts about writing .NET code from within Ubuntu using the Mono Project.


    Tags: ,

    References : http://www.seandeasy.com/tag/ubuntu/

    คำสั่ง Unix ที่ใช้เป็นประจำ







    คำสั่ง Unix ที่ใช้เป็นประจำ


    Unix
    เป็นระบบปฏิบัติการที่นิยมใช้กับแพร่หลายบนระบบขนาดใหญ่
    และในปัจจุบันยังมีระบบปฏิบัติการในลักษณะของ Unix-like เกิดขึ้นมากมาย และ
    เริ่มเป็นที่นิยมใช้กันมากหลายยิ่งขึ้นบนเครื่องคอมพิวเตอร์ส่วนบุคคล
    เอกสารชุดนี้จึงสรุปคำสั่งบน Unix
    ที่มักใช้เป็นประจำโดยมีการยกตัวอย่างเปรียบเทียบกับ DOS/Windows
    พร้อมกับอธิบายถึงส่วนขยายเพิ่มเติมของคำสั่ง
    เพื่ออำนวยความสะดวกให้กับผู้เริ่มหัดใช้ Unix ทั่วไป


    ความรู้พื้นฐานเกี่ยวกับ Unixที่ควรทราบ



    1. Unix เป็นระบบปฏิบัติการแบบ Multi User และ Multi Tasking ซึ่งแตกต่างจาก
      Window ที่เป็นระบบปฏิบัติการแบบ Multi Tasking แต่ไม่เป็น Multi User กล่าวคือ ณ
      เวลาหนึ่งๆ บนระบบ Unix จะมีผู้ใช้งานเครื่องคอมพิวเตอร์ได้มากกว่า 1 คนพร้อมกัน
      ทำให้ Unix มีระบบการจัดการ Permission
      และระบบรักษาความปลอดภัยของข้อมูลดีกว่าและซับซ้อนกว่า DOS/Window

    2. ระบบ File System ของ Unix นั้นจะเป็นระบบ Single Root กล่าวคือจะมี Logical
      Driver เพียง Drive เดียวเท่านั้น และกรณีมี Harddisk หลายตัวหรือหลาย Partition
      แต่ละ Partition จะถูกกำหนดให้เป็นเพียง Directory ย่อยของระบบ ซึ่งจะต่างกับ
      DOS/Window ที่เป็นระบบ Multiple Root ที่จะแยก Drive / Parition ตามตัวอักษร
      เช่น A: , C: เป็นต้น

    3. เนื่องจาก Unix เป็นระบบปฏิบัติการที่พัฒนาด้วยภาษา C ดังนั้นชื่อต่างๆ บน
      Unix จึงมีลักษณะเป็น Case-sensitive เช่น กรณีแฟ้มข้อมูลชื่อ MyFile กับ myfile
      จะเป็นแฟ้มข้อมูลคนละชื่อกัน

    4. ระบบ Permission ของ Unix จะแบ่งเป็น 3 ระดับคือ ระดับเจ้าของ (User หรือ
      Owner) ระดับกลุ่ม (Group) และ ระดับบุคคลอื่น (Other)
      โดยในแต่ละระดับจะแบ่งออกเป็นสิทธิในการประมวลผล (execute) การอ่าน (read) และ
      การเขียน (write) ทั้งรายละเอียดเพิ่มเติมให้ดูจากคำสั่ง chmod

    5. กรณีที่ผู้ใช้กระทำคำสั่งใดผิดพลาดนั้น บน Unix เราสามารถที่จะ Interrupt
      เพื่อยกเลิกการทำงานของคำสั่งหรือโปรแกรมนั้นๆ ได้โดยการกด CTRL + C

    6. มาตรฐานของระบบ Keyboard บนเครื่อง Unix บางเครื่องอาจจะแตกต่างกับมาตรฐาน
      Keyboard บนเครื่องที่เราใช้อยู่ ดังนั้นในบางกรณี เช่น การ telnet
      จากเครื่องอื่นเข้าสู่ระบบ Unix เราจึงไม่อาจใช้ Key บางอันตามปกติได้ เช่น
      backspace ดังนั้นเพื่ออำนวยความสะดวกให้เราสามารถใช้ backspace
      ได้ตามปกติจึงต้องมีการ map key ใหม่ด้วยการเรียกคำสั่ง stty erase [backspace]


    คำสั่งเกี่ยวกับการจัดการแฟ้มข้อมูล


    ls


    เป็นคำสั่งที่ใช้สำหรับแสดงแฟ้มข้อมูล (ในทำนองเดียวกับ dir)
    มากจากคำว่า listโครงสร้างคำสั่ง
    ls [option]... [file]...

    โดย option ที่มักใช้กันใน ls คือ
    -l จะแสดงผลลัพธ์แบบ Long Format ซึ่งจะแสดง Permission
    ของแฟ้มด้วย-a จะแสดงแฟ้มข้อมูลทั้งหมด-F จะแสดง / หลัง Directory และ * หลังแฟ้มข้อมูลที่ execute
    ได้

    ตัวอย่าง
    ls -lls -alls -Fls /usr/bin

    แหล่งข้อมูลเพิ่มเติม : ls --help และ man ls

    cp
    เป็นคำสั่งที่ใช้สำหรับสำเนาแฟ้มข้อมูล (ในทำนองเดียวกับ copy)
    มาจากคำว่า copyโครงสร้างคำสั่ง
    cp source target

    ตัวอย่าง
    cp test.txt test1.bak

    แหล่งข้อมูลเพิ่มเติม : cp --help และ man cp

    mv
    เป็นคำสั่งที่ใช้สำหรับการย้ายแฟ้มข้อมูลและ Directory
    รวมถึงการเปลี่ยนชื่อด้วย (ในทำนองเดียวกับ move) มาจากคำว่า moveโครงสร้างคำสั่ง
    mv source target

    ตัวอย่าง
    mv *.tar /backupmv test.txt old.txtmv bin oldbin

    แหล่งข้อมูลเพิ่มเติม : mv --help และ man mv

    rm
    เป็นคำสั่งที่ใช้สำหรับลบแฟ้มข้อมูล (ในทำนองเดียวกับ del)
    มาจากคำว่า remove โครงสร้างคำสั่ง
    rm [option]... [file]...

    โดย option ที่มักใช้กันใน rm คือ-r ทำการลบข้อมูลใน directory ย่อยทั่งหมด-i โปรแกรมจะถามยืนยันก่อนทำการลบ-f โปรแกรมจะลบข้อมูลทันที โดยไม่ถามยืนยันก่อนตัวอย่าง
    rm -rf test/rm test.doc

    แหล่งข้อมูลเพิ่มเติม : rm --help และ man rm

    คำสั่งเกี่ยวกับการจัดการ Directory / Folder


    pwd


    เป็นคำสั่งที่ใช้สำหรับแสดง Directory ปัจจุบัน
    (ในทำนองเดียวกับการพิมพ์ cd บน DOS) มาจากคำว่า print work directoryโครงสร้างคำสั่ง / ตัวอย่าง
    pwd


    cd
    เป็นคำสั่งที่ใช้สำหรับเปลี่ยน directory ปัจจุบัน
    (ในทำนองเดียวกับ cd) มาจากคำว่า change directoryโครงสร้างคำสั่ง
    cd directory

    โดย directory ในที่นี้อาจเป็น relative หรือ absolute path
    ก็ได้ ตัวอย่าง
    cd /usrcd ~ (เป็นการเข้าสู่ home directory)cd - (เป็นการยกเลิกคำสั่ง cd ครั้งก่อน)cd .. (เป็นการออกจาก directory 1 ชั้น

    ข้อควรระวัง : คำสั่ง cd บน UNIX
    จะต้องมีเว้นวรรคเสมอ

    mkdir
    เป็นคำสั่งที่ใช้สำหรับการสร้าง
    directory (ในทำนองเดียวกับ dos) มาจากคำว่า make directoryโครงสร้างคำสั่ง
    mkdir [option]... [file]...

    โดย option ที่มักใช้กันใน mkdir คือ
    -m จะทำการกำหนด Permissioin (ให้ดูคำสั่ง chmod
    เพิ่มเติม)-p จะทำการสร้าง Parent Directory
    ให้ด้วยกรณีที่ยังไม่มีการระบุdirectory ในที่นี้อาจเป็น relative หรือ absolute path ก็ได้

    ตัวอย่าง
    mkdir /homemkdir -p -m755 ~/local/bin

    แหล่งข้อมูลเพิ่มเติม : mkdir --help และ man
    mkdir

    rmdir
    เป็นคำสั่งที่ใช้สำหรับการลบ
    directory (ในทำนองเดียวกับ dos) มาจากคำว่า remove directoryโครงสร้างคำสั่ง
    rmdir [option]... [file]...

    โดย option ที่มักใช้กันใน mkdir คือ
    -p จะทำการลบ Child และ Parent Directory ตามลำดับdirectory ในที่นี้อาจเป็น relative หรือ absolute path ก็ได้

    ตัวอย่าง
    rmdir /homemkdir -p /home/local/data

    แหล่งข้อมูลเพิ่มเติม : rmdir --help และ man
    rmdir

    คำสั่งเกี่ยวกับการค้นหาแฟ้มข้อมูล และ Permission


    file


    บนระบบ DOS/Windows นั้น
    ประเภทของแฟ้มข้อมูลจะถูกระบุด้วยนามสกุล แต่ใน UNIX
    จะไม่มีนามสกุลเพื่อใช้ระบุประเภทของแฟ้มข้อมูล
    ดังนั้นการหาประเภทของแฟ้มข้อมูลจะดูจาก Context ภายในของแฟ้ม ซึ่งคำสั่ง file
    จะทำการอ่าน Content และบอกประเภทของแฟ้มข้อมูลนั้นๆโครงสร้างคำสั่ง
    file [option]... file

    ตัวอย่าง
    file /bin/shfile report.doc

    แหล่งข้อมูลเพิ่มเติม : file --help และ man file

    find
    เป็นคำสั่งที่ใช้สำหรับค้นหาแฟ้มข้อมูลโครงสร้างคำสั่ง
    find [path].. expression

    ลักษณะของ expression เช่น
    -name [pattern] เพื่อใช้หาชื่อ file ตาม pattern ที่ระบุ-perm [+-] mode เพื่อใช้หา file ตาม mode ที่ต้องการ-user NAME หา file ที่เป็นของ user ชื่อ NAME-group NAME หา file ที่เป็นของ group ชื่อ
    NAME

    ตัวอย่าง
    find -name *.docfind /usr -perm +111 (หาแฟ้มที่มี Permission อย่างน้อยเป็น
    111)

    แหล่งข้อมูลเพิ่มเติม : file --help และ man file

    chown
    ใช้สำหรับเปลี่ยนเจ้าของแฟ้มข้อมูลหรือ Directoryโครงสร้างคำสั่ง
    chown [option]... owner[:group] file หรือchown [option]... :group file

    โดย option ที่มักใช้กันใน chown คือ
    -R เปลี่ยน Permission ของทุกๆ แฟ้มย่อยใน
    Directory

    ตัวอย่าง
    chown krerk:users /home/krerkchown nobody data.txt

    แหล่งข้อมูลเพิ่มเติม : chown --help และ man
    chown

    chgrp
    ใช้สำหรับเปลี่ยนกลุ่มเจ้าของแฟ้มข้อมูลหรือ Directoryโครงสร้างคำสั่ง
    chgrp [option]... group file

    โดย option ที่มักใช้กันใน chgrp คือ
    -R เปลี่ยน Permission ของทุกๆ แฟ้มย่อยใน
    Directory

    ตัวอย่าง
    chgrp users /home/krerkchown nobody data.txt

    แหล่งข้อมูลเพิ่มเติม : chgrp --help และ man
    chgrp

    chmod
    ใช้สำหรับเปลี่ยนเจ้าของแฟ้มข้อมูลหรือ Directoryโครงสร้างคำสั่ง
    chmod [option]... mode[mode] file หรือchmod [option]... octalmode file

    โดย option ที่มักใช้กันใน chown คือ
    -R เปลี่ยน Permission ของทุกๆ แฟ้มย่อยใน
    Directory

    และการอ้างอิง mode จะใช้ตัวอักษร u g o a + - r w x X s t u g o
    โดย
    u หมายถึง User ผู้เป็นเจ้าของแฟ้มg หมายถึง Group ผู้เป็นเจ้าของแฟ้มo หมายถึง บุคคลอื่นๆa หมายถึง ทุกๆ กลุ่มr หมายถึง สิทธิในการอ่านw หมายถึง สิทธิในการเขียน/แก้ไขw หมายถึง สิทธิในการ execute หรือ ค้นหา (ในกรณีของ
    Directory)ส่วน s t u g และ o นั้น จะขอกล่าวถึงในเอกสารเรื่อง Unix
    Permission ต่อไป

    เนื่องจากผลลัพธ์ของคำสั่ง ls -l จะแสดงเป็นลำดับ
    ดังตัวอย่างต่อไปนี้
    $ ls -l krerk.jpg

    -rw-r--r-- 1 pok pok 13201 เม.ย. 21 2000 krerk.jpg

    ดังนั้น การเขียน Permission อาจจะเขียนได้เป็นเลขฐาน 8 เช่น 644
    หมายถึง 110100100 ซึ่งจะตรงกับ rw-r--r- เป็นต้นตัวอย่าง
    chmod 750 /home/krerk (แก้ไขได้(เขียน)ได้เฉพาะเจ้าของแฟ้ม
    และสามารถ execute ได้เฉพาะกลุ่มและเจ้าของเท่านั้น)chmod 644 data.txt (rw-r--r-- เจ้าของแฟ้ม อ่านและเขียนได้
    กลุ่มเจ้าของแฟ้มและบุคคลอื่นๆ อ่านได้ )

    (เพื่อประกอบความเข้าใจ ให้ผู้ใช้ลองเปลี่ยน mode
    และดูผลลัพธ์ด้วย ls -l)แหล่งข้อมูลเพิ่มเติม : chmod --help และ man
    chmod

    คำสั่งเกี่ยวกับการดู และ แก้ไขข้อมูลในแฟ้มข้อมูล


    cat


    ใช้สำหรับดูข้อมูลภายในแฟ้มข้อมูล หรือ Standard Input
    และแสดงผลออกมาทาง Standard Output (ในทำนองเดียวกันกับคำสั่ง type) มาจากคำว่า
    concatinateโครงสร้างคำสั่ง
    cat [optioin]... [file]

    โดย option ที่มักใช้กันใน chown คือ
    -n เพื่อทำการแสดงเลขบรรทัด

    ตัวอย่าง
    cat data.txtcat file1.txt file2.txt > file3.txt (นำข้อมูลใน file1.txt
    และ file2.txt มาต่อกัน แล้วเก็บไว้ใน file3.txt)

    แหล่งข้อมูลเพิ่มเติม : cat --help และ man cat

    more
    สืบเนื่องจากคำสั่ง cat ไม่เหมาะกับการดูข้อมูลที่มีความยาวมากๆ
    ดังนั้น จึงได้มีการพัฒนา more ขึ้น
    เพื่อช่วยให้สามารถดูข้อมูลที่มีขนาดยาวได้เป็นช่วงๆโครงสร้างคำสั่ง
    more file

    ภายในโปรแกรม more จะมีคำสั่งเพื่อใช้งานคราวๆ ดังนี้
    = แสดงเลขบรรทัดq ออกจากโปรแกรม<space> เลื่อนไปยังหน้าถัดไป<enter> เลื่อนไปยังบรรทัดถัดไปh แสดง help

    ตัวอย่าง
    more data.txt

    แหล่งข้อมูลเพิ่มเติม : man more และ help ของ
    more

    less
    less เป็นการพัฒนาคำสั่ง more ให้มีประสิทธิภาพมากขึ้น เนื่องจาก
    more จะไม่สามารถดูข้อมูลย้อนหลังได้ less
    จึงเป็นปรับปรุงและเพิ่มเติมเงื่อนไขบางอย่างให้ more โครงสร้างคำสั่ง
    less file

    ตัวอย่าง
    less data.txt

    แหล่งข้อมูลเพิ่มเติม : man less และ help ของ
    less

    head
    จะแสดงส่วนหัวของแฟ้มข้อมูล ตามจำนวนบรรทัดที่ต้องการโครงสร้างคำสั่ง
    head [option] file
     


    โดย option ที่มักใช้กันใน chown คือ
    -n เพื่อทำการระบุบรรทัดที่ต้องการ (หากไม่ระบุจะเป็น 10
    บรรทัด)

    ตัวอย่าง
    head data.txthead -n 10 data.txt

    แหล่งข้อมูลเพิ่มเติม : head --help และ man head

    tail
    จะแสดงส่วนท้ายของแฟ้มข้อมูล ตามจำนวนบรรทัดที่ต้องการโครงสร้างคำสั่ง
    tail [option] file

    โดย option ที่มักใช้กันใน chown คือ
    -n เพื่อทำการระบุบรรทัดที่ต้องการ (หากไม่ระบุจะเป็น 10
    บรรทัด)-c เพื่อระบุจำนวน byte

    ตัวอย่าง
    tail data.txttail -n 10 data.txt

    แหล่งข้อมูลเพิ่มเติม : tail --help และ man tail

    คำสั่งเกี่ยวกับผู้ใช้ และ การสื่อสาร


    whoami


    ใช้เพื่อแสดงว่าผู้ใช้ซึ่ง login เข้าสู่ระบบนั้น (ตัวเราเอง)
    login ด้วยชื่ออะไร โครงสร้างคำสั่ง/ตัวอย่าง
    whoami หรือwho am i (บน SUN OS หรือ UNIX บางตัวเท่านั้น)


    who
    ใช้เพื่อแสดงว่ามีผู้ใช้ใดบ้างที่กำลังทำงานอยู่บนระบบโครงสร้างคำสั่ง/ตัวอย่าง
    who


    finger
    ใช้สำหรับแสดงรายละเอียดของผู้ใช้โครงสร้างคำสั่ง
    finger [user@host] หรือfinger [@host]

    กรณีไม่ระบุชื่อ finger จะแสดงรายละเอียดของ User ที่กำลัง logon
    อยู่บนเครื่องนั้นๆ ทั้งหมด ซึ่งหากไม่ระบุ host ด้วย
    โปรแกรมจะถือว่าหมายถึงเครื่องปัจจุบันตัวอย่าง
    fingerfinger krerk@vwin.co.thfinger krerkfinger @student.netserv.chula.ac.th

    แหล่งข้อมูลเพิ่มเติม : man finger
     


    talk
    ใช้สำหรับการพูดคุยระหว่างผู้ใช้ด้วยกันบนระบบ
    ซึ่งผู้ใช้ทั้งทั้ง 2 ฝ่ายจะต้องพิมพ์คำสั่ง Talk ถึงกันก่อน
    จึงจะเริ่มการสนทนาได้โครงสร้างคำสั่ง
    talk user[@host] [tty]

    กรณีไม่ระบุ host โปรแกรมจะถือว่าหมายถึงเครื่องปัจจุบัน
    (นอกจากนี้ยังมีคำสั่ง ytalk ซึ่งสามารถพูดคุยได้พร้อมกันมากกว่า 2 คน)
    ซึงบางกรณีเราอาจจะต้องระบุ tty ด้วยหากมีผู้ใช้ Log in
    เข้าสู่ระบบด้วยชื่อเดียวกันมากกว่า 1 หน้าจอตัวอย่าง
    talk krerk@vwin.co.th

    แหล่งข้อมูลเพิ่มเติม : man talk
     


    write
    จะใช้เพื่อการส่งข้อมูลทางเดียวจากผู้เขียนไปถึงผู้รับบนเครื่องเดียวกันเท่านั้นโครงสร้างคำสั่ง
    write user [tty]

    เมื่อมีการพิมพ์คำสั่ง write
    ผู้ใช้จะเห็นข้อความซึ่งจะแสดงว่าข้อความดังกล่าวถูกส่งมาโดยใคร
    ซึ่งหากผู้รับต้องการตอบกลับ ก็จะต้องใช้คำสั่ง write เช่นกัน
    เมื่อพิมพ์เสร็จแล้วให้พิมพ์ตัวอักษร EOF หรือ กด CTRL+C เพื่อเป็นการ interrupt
    ทั้งนี้ข้อความที่พิมพ์หลังจาก write จะถูกส่งหลังจากการกด Enter เท่านั้นตัวอย่าง
    write krerk

    แหล่งข้อมูลเพิ่มเติม : man write

    mesg
    จะใช้เพื่อควบคุมว่าผู้อื่นมีสิทธิที่จะส่งข้อความ write
    ถึงเราหรือไม่โครงสร้างคำสั่ง
    mesg [y | n]
     


    โดย option มีความหมายคือ
    y - หมายถึงผู้อื่นมีสิทธิที่จะส่งข้อความถึงเราn - หมายถึงผู้อื่นมีไม่สิทธิที่จะส่งข้อความถึงเรา

    ตัวอย่าง
    mesg ymesg n

    แหล่งข้อมูลเพิ่มเติม : man mesg

    คำสั่งทั่วไป / อื่นๆ


    man


    เพื่อใช้แสดงรายละเอียดข้อมูลของคำสั่ง หรือ
    วิธีการใช้แฟ้มข้อมูลต่างๆ มาจากคำว่า manualโครงสร้างคำสั่ง
    man [section]... manpage
     


    โดย section ต่างๆ ของ manpage คือ
    1 จะเป็น User Command2 จะเป็น System Calls3 จะเป็น Sub Routines4 จะเป็น Devices5 จะเป็น File Format

    ตัวอย่าง
    man printfman 1 ls

    แหล่งข้อมูลเพิ่มเติม : man man

    tar
    ใช่เพื่อการ backup และ restore file ทั้งนี้การ tar
    จะเก็บทั้งโครงสร้าง directory และ file permission ด้วย
    (เหมาะสำหรับการเคลื่อนย้าย หรือแจกจ่ายโปรแกรมบนระบบ UNIX) มาจากคำว่า tape
    archiveโครงสร้างคำสั่ง
    tar [option]... [file]...

     



    โดย option ที่มักใช้กันใน echo คือ
    -c ทำการสร้างใหม่ (backup)-t แสดงรายชื่อแฟ้มข้อมูลในแฟ้มที่ backup ไว้-v ตรวจสอบความถูกต้องของการประมวลผล-f ผลลัพธ์ของมาที่ file-x ทำการ restore

    ตัวอย่าง
    tar -cvf mybackup.tar /home/*tar -tf mybackup.tartar -xvf mybackup.tar

    แหล่งข้อมูลเพิ่มเติม : tar --help และ man tar

    alias
    เพื่อกำหนด macro ให้ใช้คำสั่งได้สะดวกมากขึ้น
    (แบบเดียวกันกับการกำหนด macro ด้วย doskey)โครงสร้างคำสั่ง
    alias macroname='command'
     


    ตัวอย่าง
    alias ll='ls -F -l'

    แหล่งข้อมูลเพิ่มเติม : man ของ Shell ที่ใช้อยู่

    echo
    แสดงข้อความออกทาง standard outputโครงสร้างคำสั่ง
    echo [option]... msg
     


    โดย option ที่มักใช้กันใน echo คือ
    -n ไม่ต้องขึ้นบรรทัดใหม่

    ตัวอย่าง
    echo -n "Hello"echo "Hi.."free -k

    แหล่งข้อมูลเพิ่มเติม : man echo

    free
    แสดงหน่วยความจำที่เหลืออยู่บนระบบโครงสร้างคำสั่ง
    free [-b|-k|-m]
     


    โดย option ที่มักใช้กันใน free คือ
    -b แสดงผลลัพธ์เป็นหน่วย byte-k แสดงผลลัพธ์เป็นหน่วย kilobyte-m แสดงผลลัพธ์เป็นหน่วย megabyte


    ตัวอย่าง
    freefree -bfree -k

    แหล่งข้อมูลเพิ่มเติม : free--help และ man free

    sort
    ใช้เพื่อทำการจัดเรียงข้อมูลในแฟ้มตามลำดับ
    (ทั้งนี้จะถือว่าข้อมูลแต่ละบรรทัดเป็น 1 record และจะใช้ field แรกเป็น key)โครงสร้างคำสั่ง
    sort [option] file

    ตัวอย่าง
    sort data.txt

    แหล่งข้อมูลเพิ่มเติม : sort --help และ man sort

    การ Redirection และ Pipe


    ทั้ง DOS/Windows และ UNIX ต่างก็มีความสามารถในการ
    Redirection และ Pipe ด้วยกันทั้งสิ้น ซึ่งประโยชน์ของการ Redirection และ การ Pipe
    คือการที่สามารถนำโปรแกรมเล็กๆ
    หลายโปรแกรมมาช่วยกันทำงานที่ซับซ้อนมายิ่งขึ้นได้การ Pipe คือการนำผลลัพธ์ที่ได้จากโปรแกรมหนึ่ง
    ไปเป็นอินพุทของอีกโปรแกรมหนึ่งเช่น


    ls | sortเป็นการนำผลลัพธ์ที่ได้จาก ls ส่งเป็นอินพุตให้โปรแกรม sort
    ทำงานต่อเป็นต้น

    การ Redirection คือการเปลี่ยนที่มาของอินพุต และ
    เอาพุตที่แสดงผลลัพธ์ จาก Keyboard หรือ จอ Monitor เป็นแฟ้มข้อมูล หรือ Device
    ต่างๆเช่น

    ls >list.txtเป็นการนำผลลัพธ์ที่ได้จาก ls เก็บลงในแฟ้มข้อมูลชื่อ list.txt
    เป็นต้น

    ทั้งนี้ การ Redirection จะเป็นการสร้างแฟ้มข้อมูลใหม่เสมอ
    ในกรณีที่ต้องการเขียนข้อมูลต่อท้ายอาจทำได้โดยการใช้>>แทน>เช่น

    ls >list.txtpwd >> list.txtผลลัพธ์จากคำสั่ง pwd จะแสดงต่อท้ายผลลัพธ์จากคำสั่ง ls ใน
    list.txt

    ในทำนองเดียวกัน เราสามารถใช้ Redirection เพื่อรับข้อมูลจาก File
    ได้ซึ่งจะช่วยให้เราสามารถ Run Program ที่ต้องการ Input แบบ Batch ได้
    (ซึ่งจะกล่าวถึงในการเขียน Shell Script ต่อไป)ใช้คำสั่ง Unix บน DOS/Windowsปัจจุบันได้มีผู้ Port โครงสร้างและ Utility ของ Unix
    ไปยัง Window หรือ WindowNT ภายใต้ชื่อ Project “Cygwin” ซึ่งผู้ใช้สามารถทำงานบน
    Window ได้เหมือนกับการทำงานบน Unix ทุกประการ ทั้งนี้รวมถึงการพัฒนาโปรแกรมด้วย
    ซึ่งในปัจจุบัน Project ดังกล่าวดูแลโดย RedHat ดังนั้นหากผู้อ่านท่านใดมีความสนใจ
    สามารถหาข้อมูลเพิ่มเติมและ Download ได้จาก href="http://www.cygwin.com/">http://www.cygwin.com/ หรือ href="http://www.cygnus.com/">http://www.cygnus.com/นอกจากนี้ยังมีการ Port โปรแกรมต่างๆ ในโครงการของ GNU
    ไปยังระบบ DOS ภายใต้ชื่อ DJGPP ซึ่งประกอบไปด้วย Compiler และโปรแกรมต่างๆ บน Unix
    โดยสามารถหาข้อมูลเพิ่มเติมได้จาก href="http://www.gnu.org/">http://www.gnu.org/ตารางเปรียบเทียบ


    การใช้คำสั่งระหว่าง DOS และ UNIX














































































































    DOS UNIX หมายเหตุ
    ATTRIB +-attrib file chmod mode file ระบบ Permission แตกต่างกัน
    BACKUP tar cvf file file การทำงานแตกต่างกัน
    CD dir cd dir/ คล้ายคลึงกัน
    COPY file1 file2 cp file1 file2 เหมือนกัน
    DEL file rm file เหมือนกัน
    DELTREE rm -R file เหมือนกัน
    DIR ls หรือ ls -al และ du , df dir จะแสดงเนื้อที่ที่ใช้ และ เนื้อที่ที่เหลือด้วย ซึ่ง UNIX ต้องดูด้วย
    du และ df แทน
    DIR file /S find . -name file บน Unix จะทำงานได้ดีกว่า
    DOSKEY name command alias name='command' เป็นการสร้าง macro ในทำนองเดียวกัน
    ECHO msg echo "msg" เหมือนกัน
    FC file1 file2 diff file1 file2 เหมือนกัน
    HELP command man command ทำนองเดียวกัน
    MEM free ทำนองเดียวกัน
    MD dir หรือ MKDIR dir mkdir dir เหมือนกัน
    MORE < file more file หรือ less file less จะทำงานได้ดีกว่า
    MOVE file1 file2 mv file1 file2 เหมือนกัน
    RD dir หรือ RMDIR dir rmdir dir หรือ rm -d dir เหมือนกัน
    RESTORE tar xvf file การทำงานแตกต่างกัน
    SORT file sort file เหมือนกัน
    TYPE file more file หรือ less file less จะทำงานได้ดีกว่า

    เอกสารอ้างอิง

    • R. Thomas, J. Yates, "A USER GUIDE TO THE UNIX SYSTEM",
      OSBORNE/McGRAW-HILL,2nd Edition,1987.

    • G. Gonzato,"From DOS/Windows to Linux HOWTO"

    • Unix man pages Document.


    แหล่งที่มา : http://www.vwin.co.th/document.php?node=4