Posts tagged ‘php’

Mobile Browser Detection in PHP with RegEx

In October of last year, two thousand eight A.D., I stumbled upon a nice mobile browser detection script. Inspired by the comments, I modified the script to detect iPhones. I also made simple example scripts for two different ways of serving different content for different browsers, separate stylesheets or subdomains.

Today I received an e-mail requesting this modified script which had been removed from the file-sharing site, so I dug it up and updated it per December 2008 update and and included detection of iPods, and Android devices separately from the iPhone, mobile, and desktop groups.

An without further ado, the code. (.zip 2.76kB / .tar.gz 1.76kB)

February 25, 2009 at 12:38 am Leave a comment

Install Apache2 Web Server on Ubuntu

Ubuntu is an intuitive Linux-based operating system that is easy to switch to from Windows. The operating system and almost any software you will ever need is available free of charge. You can get Ubuntu by visiting, where you can download the software or request a CD be mailed to you.

For fun, and for profit, I design and code web sites, so I like having Apache web server software running on my machine. I can easily test changes to web sites by modifying the local code and browsing to “localhost” in my browser instead of uploading a new copy of each file for every change that is made. If you work with HTML, xHTML, CSS, PHP, or other web-based technologies, running a local server will greatly increase your productivity.

Ubuntu makes installing software a breeze. Being based on Debian, it includes the Synaptic Package Manager, which can be used to search for and install software packages. But if one knows the name(s) of the specific package one wants to install, the command-line apt interface is quicker.

In this article, I will walk you through the process of installing Apache2 web server with PHP support. Some of this software is available in the extended “universe” software repository. This may not already be enabled on your computer, so go through the following steps to ensure the software will be available for you to install.

  • On the menu bar, click System, Administration, Synaptic Package Manager (this will require you to enter the administrator password, which is simply the password of the user that was set up during installation)Synaptic Package Manager
  • On the menu bar in Synaptic, click Settings, Repositories and make sure the box next to the option for “universe” is checked. Other software you are bound to want in the future will require the “multiverse” repository, so you may as well enable it while you’re here.Enabling repositories in Synaptic

Alternatively, you can do this in terminal (command-line) by using this method.

Now you are ready to install Apache.

  • Open a terminal window, either by choosing Applications, Accessories, Terminal, or by pressing ALT+F2 (run) and entering “gnome-terminal”.Run dialog
  • Execute the following command (you must right-click and choose paste or press Shift+CTRL+V to paste into the terminal window, instead of CTRL+V):

    sudo apt-get install apache2 apache2-common apache2-mpm-prefork apache2-utils ssl-cert

    The sudo portion runs the apt-get command as a temporary administrator action, and as such requires the administrator password, as above. This will install all libraries necessary to run Apache. You will likely have to press the Y key to accept the install.

  • Now you must tell Apache where to find your web site files. Apache stores configuration information in the /etc/apache2 folder. The two important subfolders are sites-available and sites-enabled. The sites-available folder contains configuration files for all sites that Apache is able to access. The sites-enabled folder contains links (shortcuts, in Windows terms) to those config files (in the sites-available folder) for sites which are “turned on”. In other words, disabling a site is as easy as deleting the link in the sites-enabled folder, which allows one to preserve the configuration file so the site can be easily re-enabled in the future. In the terminal window, execute the following command to open the sites-available configuration:

    sudo gedit /etc/apache2/sites-available/default

  • The configuration file will open in gedit text editor. The line to look for should be the fourth, and begins with “DocumentRoot”. This should be modified to point to the folder where your website files reside. An example might be:

    DocumentRoot /data/www/

    Apache site configAfter DocumentRoot, there is a section <Directory />. The next Directory section should also point to your files, and should look like the following for most users’ needs:

    <Directory /data/www/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all

  • Once these changes are made, save the file by pressing CTRL+S or clicking File, Save; close the text editor.
  • If you’re feeling frisky, you can check out the Apache main config file at /etc/apache2/apache2.conf and if you need to change the port Apache listens to, edit /etc/apache2/ports.conf, changing the line from “Listen 80” to “Listen [PORT-NUMBER]”
  • When you installed Apache, it created a script in /etc/init.d which will start the Apache server when your computer boots up. It also started the Apache server, but with the old configuration files, so it is necessary to use the script’s “restart” option to end and restart the server. Execute the following command in terminal:

    sudo /etc/init.d/apache2 restart

  • Now you should be able to see your web site files from your browser by entering http://localhost into your browser’s address bar. If you changed the port number, you’ll need to tell the browser that. For instance, if you’re using port 78, enter instead http://localhost:78
  • To enable PHP and CGI support for your web server, some additional packages must be installed. Execute the following command in terminal:

    sudo apt-get install libapache2-mod-php4 php4-cli php4-common php4-cgi

  • Finally, if you wish to install extra Apache modules, execute one or more of the following commands in terminal:

    sudo a2enmod include
    sudo a2enmod rewrite
    sudo a2enmod ssl
    sudo a2enmod suexec

    Google for more information on these modules. I recommend installing at least rewrite.

  • All that is left is to restart Apache and test the PHP support. Execute:

    sudo /etc/init.d/apache2 restart

  • Now create an phptest.php (or any name you choose) file in your web site folder (/data/www in this example) and paste the following as the only content:

    <?php phpinfo(); ?>

    Or you can download the phptest.php file here: .php [20b] / .tar.gz [144b] / .zip [174b]

  • Then navigate to http://localhost/phptest.php in your web browser, and you should see a readout of information on your PHP version.phpinfo() result

Your local web server is now up-and-running. Use it to create an intranet for your work or home network, to quickly test web site changes without having to upload to your web host after each modification, or install a dynamic DNS package (like noip2 with a account) and serve live websites from your local computer!

February 7, 2009 at 7:18 am 2 comments


June 2017
« Feb    

Recent Posts