Steps for configuring Laravel on Apache HTTP Server

While most people develop Laravel applications on the excellent Homestead platform, which uses Nginx for the HTTP server, I still prefer to use Apache 2 HTTP server, because it is most widely supported, especially on shared hosting. Accordingly, I frequently set up Linux hosts, such as Vagrant boxes, to run Laravel with Apache. Here are the key (and sometimes easy to overlook!) steps.

  1. Install PHP, Apache HTTP server, and MySQL.
  2. Enable mod_rewrite for Apache.
  3. Install Composer globally.
  4. Create your Laravel project in user directory.
  5. Set permissions for Laravel folders.
  6. Set up an Apache virtual host for your Laravel project.
  7. Add new virtual host to hosts file.
  8. Enable new virtual host and (optionally) disable the default virtual host.
  9. [Optional] Install Node.JS JavaScript engine, which is required for Laravel Elixir.
  10. [Optional] Create a MySQL (or SQLite) database to use with your Laravel project.

Let’s look at each step in detail. (Or skip to the Summary below for minimal details.)

Install PHP, Apache HTTP server, and MySQL.

To install PHP, including everything needed to support Laravel framework, on Ubuntu-based Linux system, such as Vagrant, run the following commands in the terminal/shell.

sudo apt-get update && sudo apt-get -y install software-properties-common python-software-properties
sudo apt-add-repository -y ppa:ondrej/php && sudo apt-get update
sudo apt-add-repository -y ppa:git-core/ppa && sudo apt-get update
export DEBIAN_FRONTEND=noninteractive
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password password mysql_password'
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password mysql_password'
sudo apt-get -y install mysql-server
sudo apt-get -y install php5.6-bz2 php5.6-cli php5.6-curl php5.6-json php5.6-mbstring php5.6-mcrypt php5.6-mysql php5.6-readline php5.6-sqlite3 php5.6-xml php5.6-xsl php5.6-zip php-xdebug apache2 libapache2-mod-php5.6 git curl

By using Ondrej Sury‘s PPA for PHP, you can install PHP 5.5, 5.6, or 7.0 (and soon 7.1!) or any combination. See this article for more details. Specifically, to install PHP 7.0 instead of 5.6, just replace each instance of php5.6 with php7.0 above.

Also, note that using debconf-set-selections utility allows us to install MySQL without being prompted for the root password. In this case, we set the password to mysql_password, but you probably want to use something else.

Enable mod_rewrite for Apache.

One of the most frequently encountered difficulties when starting with Laravel on Apache HTTP server is that the mod_rewrite extension is not enabled. Laravel requires mode_rewrite to properly transform URLs to be interpreted by its routing function via .htaccess. On some Ubuntu platforms, mod_rewrite will be enabled by default, but for enhanced security, the default is now for it to be disabled. To enable
mod_rewrite extension, run:

sudo a2enmod rewrite
sudo service apache2 restart
sudo apachectl -t -D DUMP_MODULES | grep rewrite

The last command (apachectl) should return rewrite_module (shared). If this is not returned, then some failure occurred with the installation or configuration of mod_rewrite extension.

Install Composer globally.

The Laravel framework (and most recent PHP packages) use the Composer package management utility. Composer allows you to quickly and easily install the PHP modules that you need without having to worry about managing all of their dependencies. Likewise, Composer will automatically keep all of your packages updated, when maintainers publish new versions.

To simplify use of Composer, we are going to install it globally, so that you can run it from most any directory in the terminal/shell. (The Laravel installation instructions have you install Composer specifically for your Laravel project, but that’s usually not necessary.) We won’t go over all of the details for install Composer, because the Composer web site has simple, yet thorough instructions. Just go to the Composer Download page and follow the instructions. The only change to make to the given instructions is that on the third step, run the installer like this:

sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer

Since we are installing to the /usr/local/bin directory you must run this command as root user by using sudo.

The Composer installer will report the results and you should be able to verify successful installation by running composer in terminal/shell which should give you a list of all of the Composer commands.

Create your Laravel project in user directory.

Now we are ready to create our Laravel project, which installs all of the necessary files into a new directory. For this example, we will create our project in a user directory, which simplifies managing the permissions of the files and directories. Specifically, we’ll install to projects/laravel_project under our user’s home directory ($HOME). Here’s how to do it:

cd $HOME
mkdir projects
cd projects
composer create-project laravel/laravel laravel_project --prefer-dist 5.2.*

In this example, we are explicitly installing version 5.2 of Laravel framework, but you can choose any version; check this article for details. Also, we use the Composer --prefer-dist parameter to reduce the amount of data that must be downloaded.

After a few minutes, all of the necessary files will be downloaded and installed, including all of the dependencies. Composer should finish with the messages:

Writing lock file
Generating autoload files
> Illuminate\Foundation\ComposerScripts::postUpdate
> php artisan optimize
Generating optimized class loader
> php artisan key:generate
Application key [base64:Q1wREnWZ5E/AmhQ8JLZr85NjiFot9IIDJ8+vTeWnNts=] set successfully.

Of course, your application key will be different. And, as shown, you can always re-generate the key by running php artisan key:generate in your project directory.

To confirm that the installation succeeded, run:

cd laravel_project
php artisan

This should give you a list of the Artisan commands. Artisan is Laravel’s powerful built-in command-line utility, which simplifies many common development tasks. Check it out!

Set permissions for Laravel folders.

Another common pitfall in configuring Laravel on Linux is setting the appropriate permissions for a couple of the directories. Laravel needs to be able to write transient (temporary) data to some directories while it’s performing it’s magic, specifically the storage and bootstrap/cache directories. Run the following commands from the Laravel base project folder (e.g., $HOME/projects/laravel_project):

chmod -R 777 storage bootstrap/cache

Essentially, this sets “full access” permissions on these directories, so that no matter what process executes the Laravel application, it will be able to read from and write to them. Read more about Linux file permissions and how to manage them here.

Set up an Apache virtual host for your Laravel project.

Probably the one thing (from the developer’s perspective anyway!) that Nginx makes so simple compared to Apache is virtual host configuration. Virtual hosts is the web server terminology for allowing a single machine (host) to serve multiple web sites. For us, the main value is in allowing us to create an abbreviated name for our Laravel project. In addition, for Laravel, we actually must use a virtual host, so that we can properly set permissions for the project directories to avoid ‘403 Forbidden’ HTTP error, due to changes to default security settings in versions 2.4.3 and above of Apache server. See this article for more details.

In this step, we’ll create a new site configuration named laravel_project.conf which contains our named virtual host configuration. All of the files that we’ll be working with are system files owned by the root user, so we must use sudo command with all of them. Also, don’t feel obligated to use the nano editor; I just use it here, since it’s installed by default on Ubuntu Linux (and it’s a pretty good editor, too!).

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/laravel.conf
sudo nano /etc/apache2/sites-available/laravel_project.conf

Modify the laravel_project.conf contents so that they look like this:

NameVirtualHost *:8080
Listen 8080

<VirtualHost *:8080>
    ServerAdmin admin@example.com
    ServerName laravel.dev
    ServerAlias www.laravel.dev
    DocumentRoot /home/user/projects/laravel_project/public
    
    <Directory /home/user/projects/laravel_project/public/>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
            Require all granted
    </Directory>
    
    LogLevel debug
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

In this file, replace /home/user with the name of the $HOME directory for your account on the machine. (This is the directory where you created the projects directory earlier.) Also, note that the root directory (DocumentRoot) is the public directory in your Laravel project folder. This is mainly for security as it means that the actually application files are stored in directories that the web server does not have access to. Finally, observe that we have specified that the TCP port that Apache will allow connections on (“listen” on) for the virtual host is 8080. This is a frequently used alternate port to the standard HTTP port of 80. We use 8080 to avoid conflicting with any other web applications already installed on your system.

Add new virtual host to hosts file.

You probably noticed in the previous step when setting up the virtual host configuration for our project, we specified laravel.dev as the ServerName. Basically, this is the “short” name for our virtual host. So, everything is set from the virtual host perspective to use this name. However, we need to configure our machine to recognize this name. To do this, we add an entry to machine’s hosts file. Open the hosts file:

sudo nano /etc/hosts

In the hosts file, add this new line at the end (or anywhere):

127.0.0.1   laravel.dev

Save the file.

Enable new virtual host and (optionally) disable the default virtual host.

After all of this, we are almost done! The last thing that we need to do is enable the virtual host configuration laravel_project.conf that we created above. To do this:

sudo a2ensite laravel_project.conf

You’ll be prompted to reload the Apache server process. However, before we do that, we (optionally) may want to disable the default Apache configuration. Again, if you have other web applications, such as phpMyAdmin, running on your machine, you do not want to disable this configuration. Otherwise, to avoid confusion, it probably makes sense to disable it:

sudo a2dissite 000-default.conf

Note that enabling (a2ensite) and disabling (a2dissite) don’t delete or otherwise change your configurations; they simply turn them “on” or “off”. (Basically, these utilities create or remove a symbolic link to the configurations in the /etc/apache2/sites-available directory to the same name in the /etc/apache2/sites-enabled directory.)

Now, we are ready to restart Apache server and see if everything works! To restart the server:

sudo services apache2 restart

Now, open a web browser and enter http://laravel.dev:8080/ in the address field. You should see the standard Laravel landing page screen.

If you don’t see the landing page, then check that the above steps were performed correctly. Specifically, ensure that you have set permissions on storage and bootstrap/cache directories and that you have specified Require all granted in the section of laravel_project.conf.

[Optional] Install Node.JS JavaScript engine, which is required for Laravel Elixir.

These final two steps are entirely optional, but they can be helpful in setting up a fully-functional development environment.

In this step, we install the popular Node.JS JavaScript engine. Node.JS provides many great utilities, including supporting the Gulp task-runner/build utility used to process CSS and JavaScript files, which underpins the Laravel Elixir tool.

Previously, I have written about installing Node.JS from source code. However, using the official Node.JS PPA makes things a snap. To install Node.JS, do the following:

curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
sudo apt-get -y install nodejs

The script that is downloaded via curl sets up the Node.JS repositories and updates the repository cache, so you can proceed directly to installation.

To confirm that the install succeeded, check the versions of Node.JS and its companion NPM utility:

node -v
npm -v

[Optional] Create a MySQL (or SQLite) database to use with your Laravel project.

The final (optional) step in the process is to create a MySQL database for your project. Instead of showing all the details here, please refer to this article. It includes information about setting the database configuration details in the Laravel .env file so that Laravel can load them directly.

Summary

To wrap up this article, here are the steps with only the actual commands that you run to use as a quick reference.

  1. Install PHP, Apache HTTP server, and MySQL.
  2. sudo apt-get update && sudo apt-get -y install software-properties-common python-software-properties
    sudo apt-add-repository -y ppa:ondrej/php && sudo apt-get update
    sudo apt-add-repository -y ppa:git-core/ppa && sudo apt-get update
    export DEBIAN_FRONTEND=noninteractive
    sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password password mysql_password'
    sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password mysql_password'
    sudo apt-get -y install mysql-server
    sudo apt-get -y install php5.6-bz2 php5.6-cli php5.6-curl php5.6-json php5.6-mbstring php5.6-mcrypt php5.6-mysql php5.6-readline php5.6-sqlite3 php5.6-xml php5.6-xsl php5.6-zip php-xdebug apache2 libapache2-mod-php5.6 git curl
    
  3. Enable mod_rewrite for Apache.
  4. sudo a2enmod rewrite
    sudo service apache2 restart
    sudo apachectl -t -D DUMP_MODULES | grep rewrite
    
  5. Install Composer globally.
  6. php -r &quot;copy('https://getcomposer.org/installer', 'composer-setup.php');&quot;
    sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
    php -r &quot;unlink('composer-setup.php')&quot;
    
  7. Create your Laravel project in user directory.
  8. cd $HOME
    mkdir projects
    cd projects
    composer create-project laravel/laravel laravel_project --prefer-dist 5.2.*
    ## Check installation
    cd laravel_project
    php artisan
    
  9. Set permissions for Laravel folders.
  10. chmod -R 777 storage bootstrap/cache
    
  11. Set up an Apache virtual host for your Laravel project.
  12. sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/laravel.conf
    sudo nano /etc/apache2/sites-available/laravel_project.conf
    
    ## laravel_project.conf
    NameVirtualHost *:8080
    Listen 8080
    
    <VirtualHost *:8080>
        ServerAdmin admin@example.com
        ServerName laravel.dev
        ServerAlias www.laravel.dev
        DocumentRoot /home/user/projects/laravel_project/public
        
        <Directory /home/user/projects/laravel_project/public/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
                Require all granted
        </Directory>
        
        LogLevel debug
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
    
  13. Add new virtual host to hosts file.
  14. sudo nano /etc/hosts
    

    In the hosts file, add this new line at the end (or anywhere):

    127.0.0.1   laravel.dev
    
  15. Enable new virtual host and (optionally) disable the default virtual host.
  16. sudo a2ensite laravel_project.conf
    sudo a2dissite 000-default.conf  ### OPTIONAL
    sudo services apache2 restart
    

    Open web browser to http://laravel.dev:8080/.

  17. [Optional] Install Node.JS JavaScript engine, which is required for Laravel Elixir.
  18. curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
    sudo apt-get -y install nodejs
    node -v
    npm -v
    
  19. [Optional] Create a MySQL (or SQLite) database to use with your Laravel project.
  20. mysql -uusername -ppassword
    mysql> CREATE DATABASE `laravel` CHARACTER SET utf8 COLLATE utf8_general_ci;
    mysql> CREATE USER 'laravel_user'@'%' IDENTIFIED BY 's3cr3t';
    mysql> USE `laravel`;
    mysql> GRANT ALL PRIVILEGES ON `laravel`.* TO 'laravel_user'@'%';
    mysql> FLUSH PRIVILEGES;
    mysql> exit;
    

    Note that on the GRANT statement, the backticks (`) are only around the database name. The .*, which means “all tables”, are outside of the backticks.

    To see the permissions (privileges) granted to laravel_user, in the MySQL shell, run:

    mysql> SHOW GRANTS FOR 'laravel_user'@'%';
    

    This should return something like:

    +-----------------------------------------------------------+
    | Grants for laravel_user@%                                 |
    +-----------------------------------------------------------+
    | GRANT USAGE ON *.* TO 'laravel_user'@'%'                  |
    | GRANT ALL PRIVILEGES ON `laravel`.* TO 'laravel_user'@'%' |
    +-----------------------------------------------------------+
    

    An alternate method of creating the database non-interactively (i.e., without logging into MySQL directly, but by running commands at the shell prompt) is to use the MySQL -e command-line parameter:

    mysql -uusername -ppassword -e "CREATE DATABASE \`laravel\` CHARACTER SET utf8 COLLATE utf8_general_ci;"
    mysql -uusername -ppassword -e "CREATE USER 'laravel_user'@'%' IDENTIFIED BY 's3cr3t';"
    mysql -uusername -ppassword -e "USE \`laravel\`; GRANT ALL PRIVILEGES ON `laravel`.* TO 'laravel_user'@'%'; FLUSH PRIVILEGES;"
    

    Note that you must “escape” the backtick characters (`) by putting a backslash (\) in front of them.

    Update the .env file in the root directory of your project with the appropriate parameter values to match your new database:

    ## .env file
    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=laravel
    DB_USERNAME=laravel_user
    DB_PASSWORD=s3cr3t
    

Hope that you found this useful. Let me know your thoughts and questions in the comments.

Creating and configuring a new MySQL database via the command line

When developing almost any application, you will need to have database of some type to store the data created and used by the application and its users. The most popular open-source database is MySQL. Of course, there are other well-known open-source database platforms, such as PostgreSQL and SQLite, as well as so-called NoSQL databases like MongoDB, since MySQL is ubiquitous, we’ll work with it.

Just as there are many different database platforms, there are many ways to interact with MySQL. There are web-based tools, like phpMyAdmin and Adminer, and regular client applications, such as DBeaver and SQuirrel SQL, all of which are free and open-source. I use and can recommend all of the tools mentioned. However, since you may not always have access to these tools, it’s always good to know how to use the default command-line/shell tools for MySQL.

To log into MySQL at the command line, enter this command:

mysql -uusername -ppassword

Note that there is no space between the -u and -p and the username and the password, respectively. (Technically, you can put a space between -u and username, but I find it difficult to remember which one you can and which one you can’t, so I just do them both the same way!) Assuming that you enter a valid username and password, you will be logged in and at the MySQL command prompt: mysql>.

All MySQL commands end with ; (semi-colon). To try things out, let’s look at the databases that you already have:

mysql> SHOW DATABASES;

Running this command should return something like this:

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.40 sec)

Every MySQL database server will have an information_schema and a mysql database. These databases hold metadata about the configuration of the MySQL instance itself. While you won’t often use these databases, you might want to know a little about them.

Let’s create a new database and grant privileges (permissions) to that database to a specific (new) user. I prefer to create a user with the same name as the database itself, but you can use any username that you like. For this example, we’ll the username will be different from the database name to avoid confusion. Of course, to maintain security, you should usually not use the MySQL root user for access to application databases.

mysql> CREATE DATABASE `laravel` CHARACTER SET utf8 COLLATE utf8_general_ci;
mysql> CREATE USER 'laravel_user'@'%' IDENTIFIED BY 's3cr3t';
mysql> GRANT ALL ON `laravel.*` TO 'laravel_user'@'%';

After each command above, you should get a response message of Query OK followed by the number of rows affected (usually 0 or 1 for DDL commands like these). Also, if you get the message ERROR 1046 (3D000): No database selected when running the GRANT command, this means that MySQL doesn’t know what database you are referring to. You can execute USE `laravel`; to select the laravel database as the default.

Let’s look at the details of each of these commands in turn. The CREATE DATABASE command does just that: it creates a new database with the given name. Note that we included the name (laravel) in a pair of back ticks (`). While this isn’t absolutely necessary in most cases, it’s good practice since it allows you to use non-ASCII characters in the name. Likewise, we specified the optional CHARACTER SET and COLLATE parameters. By using UTF8 character set, this ensures that our database can store non-ASCII characters properly. Essentially, we are making our database more flexible.

Next, we turn to the CREATE USER command. Obviously, this creates a new user in our database platform. However, initially, this user (laravel_user) can’t actually do anything (other than log into MySQL). The username should be contained in single quotes (') [not back ticks!). Also, you are probably wondering about the @'%' following the username. This defines the scope of accessiblity for this user and % essentially means that this user can connect to the database from any host (machine). If you want to restrict connections to only the host where MySQL is running, such as in a development environment, you can specify @'localhost' (or @'127.0.0.1') instead of @'%'. Finally, the IDENTIFIED BY clause specifies the password (called authentication_string in MySQL terminology) for the user. In this case, we simply specify the actual password (s3cr3t) enclosed in single quotes. MySQL hashes passwords so that they are not stored in plain text in the database. You can see the hashed value for this user (laravel) by running:

mysql> select authentication_string from mysql.user where user = 'laravel_user';

You should get a result similar to this:

+-------------------------------------------+
| authentication_string                     |
+-------------------------------------------+
| *58C036CDA51D8E8BBBBF2F9EA5ABF111ADA444F0 |
+-------------------------------------------+
1 row in set (0.08 sec)

Obviously, the actual value will (or should!) be different on your system.

Finally, we come to the GRANT command, which explicitly gives priveliges (permissions) to a user for certain databases or objects in a database. In this case the ALL specification means that all privileges (permissions) available are being given (“granted”) to the specified user. If you want to grant only certain permissions, such as to allow “read-only” access, you can specify any list of the other privileges, such as SELECT, CREATE, UPDATE, and DELETE. (See MySQL documentation on GRANT for full list.) The specification after the ON indicates the scope of the privileges that are granted. In this case, by specifying `laravel.*`, we are granting these permissions to all objects (not only tables) in the laravel database. If you want to limit permissions to only a specific table, you could specify that, such as `laravel.user`. And, last but not least, the TO clause indicates which user (or users) these permissions should be given to. Again, the format of the user follows that of the format for CREATE USER. You can specify multiple usernames by separating each with a comma (,).

To log out of the MySQL command prompt, run:

mysql> exit;

Now, we have our database created and ready to use. In Laravel, you need to update the .env file in the root directory of your project with the appropriate parameter values to match your new database. Using the database above (and assuming that MySQL server is running on the same machine as our Laravel project), our settings would be:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laravel_user
DB_PASSWORD=s3cr3t

Hope you enjoyed this article. Please provide feedback/questions in the comments.

Use PHP array_map() function to remove white space from array elements

One common scenario in PHP is take an input list, such as some search criteria entered by a user, and tokenize the input into individual items using a separator, such as comma. The PHP explode() function works great for splitting up the individual values into an array(). However, if have white space before or after the separator, each of the elements of the new array will contain that white space. You could use a foreach loop, with the $key => $value syntax to apply the trim() function to each item. But PHP has the perfect function already built in: array_map().

array_map() takes a callback function as its first argument and an array as the second argument. It applies the specified callback function to each element of the array that is passed in. You simply provide the name of the callback function as a string (either in single or double quotes) and the function can be a function within your code or any of the built-in PHP functions. For example, to put all of the pieces together, you could do something like this:

$input = "apricot, banana, cherry, dewberry, eggplant, fig";
$output_trimmed = array_map("trim", explode(',', $input));

TKE – Excellent new Tcl/Tk Text Editor

If you are looking for an excellent new text editor, please check out TKE. It’s a new Tcl/Tk-based editor with many features similar to Sublime Text.

TKE has excellent documentation. However, it is a little bit tricky to install it on Ubuntu, because of some dependencies. To install the dependencies on Ubuntu, run:

sudo apt-get install tcl8.5 tk8.5 tclx8.4 tcllib tklib

Now, just follow the installation instructions in the TKE documentation. And remember to run the installation command with sudo. I installed to /usr/local without any problems.

Installing and configuring WampServer on Windows XP, Windows Vista, or Windows 7

WampServer is full-fledged web environment that runs on Windows and works very much like your web hosting provider’s system, except that it is located entirely on your computer.  With WampServer, you don’t even have to be connected to the Internet to do web design and development!

WampServer consists of the following components:

  • Windows –  You’ve already got this part!  These instructions apply to Windows XP, Windows Vista, and Windows 7 (and may work on other versions, as well).
  • Apache – The Apache web server is a free, open-source HTTP server that is used on over half of the web servers on the Internet.  It is very well supported and has an active and helpful community, so it is quite easy to get help and find information about it.
  • MySQL – MySQL is a popular free, open-source RDBMS server platform.  As with Apache, it is widely used and has many users who share their experiences and tips.  In addition, one of the great advantages of MySQL is that his native integration with Apache and PHP.
  • PHP – PHP is also free and open-source and is a popular scripting language for developing dynamic interactive web applications.  PHP is both easy to learn, even for beginners, and powerful.

You can think of Apache, MySQL, and PHP as three legs of a sturdy stool that stand on the foundation of Windows.  You’ll undoubtedly hear about LAMP, which is similar to WAMP, except that the ‘L’ stands for Linux, which is a popular open-source server operating system that runs many web hosting systems.

To install WampServer, download the installation package from http://www.wampserver.com/en/download.php.  Run the installation package and choose the defaults for each prompt.  This will install WampServer to the c:\wamp folder (directory).

If you did not choose to launch WampServer after the installation completed, go ahead and run WampServer from the Start Menu:  Start → Programs → WampServer → start WampServer.  If you received any security warnings (very likely in Windows Vista and Windows 7) permit WampServer to run.  These are warnings that WampServer wants to make updates to your system and to use network resources, which is normal since this is a web server environment! You’ll see an icon in the Windows system tray that looks like gauge or speedometer with a needle.  It will show red, then yellow, and finally (hopefully!), the needle will settle all the way to the right and the icon will be white.  Hover the mouse of the icon and it should display ‘WAMPSERVER – server Online’.  If so, everything is good.

To confirm that the server is running and “serving” web pages, open your web browser and enter http://localhost/ in the address field and press <Enter>.  If all is good, you’ll see a web page with the WampServer logo and Server Configuration information.  This page will show you the versions of Apache, MySQL, and PHP that are installed, as well as the PHP extensions (more about them later!) loaded.  Congratulations!  You have a working web server platform running on your machine.  Wasn’t very hard, was it?!

If for some reason WampServer is unable to start or, more likely, start completely, see this page for information about troubleshooting problems with WampServer.  Keep this page in mind as you make changes to the WampServer configuration.  It is almost inevitable that something will go wrong, but that shouldn’t deter you from experimenting and learning from your mistakes.  Again, the Apache, MySQL, and PHP communities are very helpful and the Internet is filled with resources on solving these problems.

The web root folder will be, by default, set to c:\wamp\www.  The web root folder is like the “home” (base) folder for the local Apache web server.  Also, Apache is configured, by default, to “listen” on port 80, which is the standard port for HTTP, the protocol used for web sites. Apache is very flexible and we’ll talk later about how to change some of these common parameters.  If you look in the c:\wamp\www folder, you’ll see a file called index.php.  When you opened http://localhost/ to test your WampServer installation, the page that was actually displayed (really interpreted for display by the Apache HTTP server) was this index.php file.

But how did Apache know to open index.php specifically?  This is just one of Apache’s huge set of configuration parameters (settings).  The main Apache configuration file is called httpd.conf. (“httpd” stands for HTTP daemon.  The term “daemon” simply means background service and is a term borrowed from Unix.  In other words a daemon that runs in the background and performs tasks without user interaction.)  You’ll find httpd.conf on WampServer in c:\wamp\bin\apache\Apacheversion\conf, where version is the version number of Apache in your WampServer installation (2.2.11 at the time of this writing).  Open httpd.conf with any text editor, such as Notepad.  Take a look through httpd.conf to get familiar with its contents.  In particular, we are looking for a line that contains DirectoryIndex.  When you find it, it will probably contain something like this:

   DirectoryIndex index.php index.php3 index.html index.htm

Did you see that this line contains index.php?  Remember that we saw that c:\wamp\www folder contained a file named index.php?  The DirectoryIndex directive (Apache’s name for commands!) tells Apache what file names to look for if you don’t specify a particular file name when entering a URL (web address) and the order to look for them.  Therefore, in this case, Apache looks for a file named index.php first and, if it doesn’t find it, then for index.php3 and so on.  Thus, this is how Apache “translated” http://localhost/ to http://localhost/index.php when you tested the WampServer installation.

The other important item from this discussion is that c:\wamp\www in your Windows file system (directory or folder structure) is essentially the same as the “root” folder of the web server provided by WampServer.  Thus, if you create another sub-folder in c:\wamp\www, such as c:\wamp\www\mysite, you can access it in your web browser by entering http://localhost/mysite.  (Remember that you’ll need to have a file in c:\wamp\www\mysite that Apache can interpret, such as index.html, or you’ll need to specify the specific file to display, such as http://localhost/mysite/myfile.php.)

However, we haven’t explained the localhost part, yet.  Basically, localhost is just another name (called an “alias” in the web vernacular) for the loopback IP address of your computer, which is usually 127.0.0.1.  This alias is defined, on Windows systems, in the c:\windows\system32\drivers\etc\hosts file.  We’ll come back to hosts when we discuss setting up “virtual hosts” (basically multiple web “sites”) in Apache.  For now, just remember that c:\wamp\www corresponds to the “root” (top-level) directory of http://localhost/.

That’s it for now.  Leave any questions in the comments and look for the next installment in the series soon.

Web development on Windows

Today, we are starting a new series on setting up a web development environment and developing web applications on Windows.  The series will focus on using WampServer, Eclipse, and CodeIgniter PHP framework.

Some of the topics that we plan to cover include:

  • Installing WampServer on Windows XP, Windows Vista, or Windows 7.
  • Adding additional server versions to WampServer.
  • Creating a new MySQL database via phpMyAdmin.
  • Configuring multiple virtual hosts in Apache.
  • Installing Subversion and set up WebSVN with WampServer.
  • Installing and configure PEAR on WampServer.
  • How to set up Eclipse to work with WampServer environment.
  • How to use Eclipse with local Subversion (or Git?) environment.
  • Free remote Subversion and Git hosting services.
  • Installing Xdebug for PHP on WampServer and Eclipse.
  • Installing and configuring CodeIgniter.
  • Adding Zen Coding plugin to Eclipse.
  • Configuring mod_rewrite on WampServer for URL rewriting.
  • Moving your finished web design to web hosting platform.

I hope your looking forward to the adventure.  If you have any suggestions for other topics, please leave your suggestions in the comments.

Welcome!

Thanks for visiting!

First, let me say a little bit about the blog name. Obviously (probably!), it’s a combination of PHP and praxis. I chose the name because PHP is my favorite programming language and because praxis describes what I want this blog to emphasize: the practical application of theory.

I hope to post some tutorials and my experiences (including mistakes!) that I make on my journey through PHP and web design.

Come along!