Apache Http Server Project



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.

The Apache web server was used to host the system because it is the most used web server. Apache web server is an open source project, easy to customize environments, fast, reliable, and highly. Our all-volunteer board oversees more than 350 leading Open Source projects, including Apache HTTP Server - the world's most popular Web server software. The ASF provides an established framework for intellectual property and financial contributions that simultaneously limits potential legal exposure for our project committers.

HttpCore is a set of low level HTTP transport components that can be used to build custom client and server side HTTP services with a minimal footprint. HttpCore supports two I/O models: blocking I/O model based on the classic Java I/O and non-blocking, event driven I/O model based on Java NIO. Home page of The Apache Software Foundation. This page provides detailed information on the export control status of the Apache Software Foundation's products, as well as pointers to the open source code from which those products are built.

  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.

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:

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:

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:

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:

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:

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!

Apache server versions

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):

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!).

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

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.

Http

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:

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

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:

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:

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.)

Apache 2.4.41

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

Apache Web Server Documentation

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:

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.

Apache Http Server Project

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

[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. 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. In the hosts file, add this new line at the end (or anywhere):

  9. Enable new virtual host and (optionally) disable the default virtual host.
  10. Open web browser to http://laravel.dev:8080/.

  11. [Optional] Install Node.JS JavaScript engine, which is required for Laravel Elixir.
  12. [Optional] Create a MySQL (or SQLite) database to use with your Laravel project.
  13. 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:

    This should return something like:

    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:

    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:

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