본문 바로가기
Database/Oracle Lecture

How to: Installing Oracle XE on Ubuntu with PHP

by 현이빈이 2008. 7. 11.
반응형

This tutorial assumes you know at least a little bit about Linux, Oracle, and PHP. Or at least you know what they are. Or something. Oracle Express is Oracle’s free version of their database. It imposes some resources and functionality limitations, but for the purposing of learning and deploying smaller applications, it works fabulously. It’s also the easiest to install. So that’s what I’ll be demonstrating. I’m not going to talk to you about the advantages of Oracle, because I have a feeling that would be a waste of my time and yours. On to the tutorial:

  1. The first thing your going to want to do is prepare Ubuntu. I’m going to assume you have Ubuntu installed. I used Ubuntu Dapper Drake 6.06 LTS Server Edition, with the LAMP install option. No matter what installation you choose, you’ll need to have a relatively recent version of Apache and PHP (I used Apache 2.0.55 and PHP 5.1.2). Run
    sudo apt-get update
    sudo apt-get upgrade
    to make sure you’ve got the latest versions. You should also make sure you’ve got a static local IP. That’s out of the scope of this tutorial, but you can find information on that here.
  2. Now it’s time to actually install Oracle XE. You’ll need to edit the /etc/apt/sources.list file. The following should do the trick, from now on it’s assumed you know how to edit a text file.
    sudo vi /etc/apt/sources.list
    Now add the following line:
    deb http://oss.oracle.com/debian unstable main non-free
    somewhere and save the file. Then run
    sudo apt-get update
    again. Finally, run
    apt-get install oracle-xe
    and Oracle XE should begin to install. There is a hefty amount of files that need to be downloaded, so it give it sometime. Once the install is complete, you’ll get a message asking to run
    /etc/init.d/oracle-xe configure
    so go ahead and run that, appending “sudo” to the beginning of the command to run it as the root user. You basically just want to accept the defaults here, running Oracle Application Express on port 8080, the Oracle Listener on 1521, and having it automatically start on bootup. You’ll also need to enter and confirm a password for the SYS and SYSTEM accounts. It should now begin to start the Listener and configure the database. This might take a few minutes. Oracle is now installed. Give yourself a pat on the back. Now that wasn’t so hard was it?
  3. We’re now going to create a user for us to connect with, because it’s bad practice to connect with a DBA account. We’ll use Oracle Application Express to do this. Point your browser to http://127.0.0.1:8080/apex or to the appropriate local ip address if your connecting from a different machine. You’ll want to login with the account username system and the password you set in the last step. Select Administration>Database Users>Create User. Set a username and password, and verify that the CONNECT and RESOURCES roles are selected. Press create and your user has been created. Remember this username and password for later.
  4. We now have Oracle, Apache, and PHP installed and running. This step will put the glue between Oracle and PHP. For the purposes of this tutorial, we’re using OCI and Oracle Instant Client. Download the Basic and SDK packages from this site. You should then create a directory that will hold the Instant Client files. /opt/oracle/instantclient works fine, but you should be able to use basically any directory as long as the right permissions are set. Unzip both packages into this new directory. Now to create the necessary symlinks, by running these two commands from within your newly created directory.
    ln -s libclntsh.so.10.1 libclntsh.so
    ln -s libocci.so.10.1 libocci.so
    Run the
    sudo apt-get install php-pear
    command to get the PEAR libraries. You’ll also want phpize, which is included in the PHP-dev package, so run
    sudo apt-get install php5-dev
    as well. We’re finally ready to actually install OCI8. Run the command
    sudo pecl install oci8
    and enter ‘instantclient,/x/y/z’ replacing /x/y/z with your appropriate path to your instant client when you’re prompted. You should now enable OCI8 within the PHP configuration file(s). Open up /etc/php5/apache2/php.ini and /etc/php5/cli/php.ini and add the line
    extension=oci8.so
    The latter file is only necessary if you plan on connecting to Oracle from the command line version of PHP. You can really put extension=oci8.so line anywhere in either file, but I’d recommend somewhere after one of the ;extension examples for consistency sake. Your last step is restarting Apache, with the command
    sudo /etc/init.d/apache2 restart
    To make sure everything went smoothly, you’ll want to create a new PHP file in the www directory (by default, /var/www) that consists of a single line,
    < ?php phpinfo(); ?>
    Visit this page in your web browser, and find the oci8 section. If there isn’t one, something isn’t working right. If it appears, verify everything looks right, and move on to the next and final step.
  5. Our last step, actually connecting to Oracle XE with PHP. Create a new PHP file containing the following lines (replacing username and password with the username and password you created earlier of course):

    < ?php

    $oracle = oci_connect(’username’, ‘password’, ‘//localhost/XE’);

    if (!$oracle)
    {
    $e = oci_error();
    print htmlentities($e[’message’]);
    exit;
    }
    ?>

And we’re done! Leave feedback, ask questions etc. by posting a comment or by emailing me at my first name (hint s-a-m) @samgerstenzang.com

Further reading

반응형