oraclelapp

Get started with Laravel

NOTE: We are in the process of modifying the file structure and configuration for many Bitnami stacks. On account of these changes, the file paths stated in this guide may change depending on whether your Bitnami stack uses native Linux system packages (Approach A), or if it is a self-contained installation (Approach B). To identify your Bitnami installation type and what approach to follow, run the command below:

 $ test ! -f "/opt/bitnami/common/bin/openssl" && echo "Approach A: Using system packages." || echo "Approach B: Self-contained installation."

The output of the command indicates which approach (A or B) is used by the installation, and will allow you to identify the paths, configuration and commands to use in this guide. Refer to the FAQ for more information on these changes.

Approach A: Bitnami installations using system packages

The Laravel framework is not installed by default in Bitnami installations using system packages. However, it can be easily enabled via the Laravel Installer tool. In this guide, you will learn how to install and configure a Laravel project.

Installation and Testing

NOTE: This section assumes that the application project directory will be located at /opt/bitnami/projects/APPNAME. Remember to replace the APPNAME placeholder with the application name, such as myapp.

  • First, install the Laravel Installer tool and make it available in your PATH. Run the following commands:

    $ composer global require laravel/installer
    $ echo 'PATH=$PATH:$HOME/.config/composer/vendor/bin' | sudo tee -a /etc/profile
    $ export PATH=PATH=$PATH:$HOME/.config/composer/vendor/bin
    
  • Create a project directory where to store your application. Run the following commands:

    $ sudo mkdir -p /opt/bitnami/projects
    $ sudo chown $USER /opt/bitnami/projects
    
  • Create the Laravel project:

    $ cd /opt/bitnami/projects
    $ laravel new APPNAME
    
  • Grant write permissions for the web server to the storage directory. Run the following command:

    $ sudo chown daemon:daemon /opt/bitnami/projects/APPNAME/storage
    
  • Create the Apache configuration for your application. Follow these steps:

    • Create the /opt/bitnami/apache2/conf/vhosts/APPNAME-vhost.conf file containing the application’s HTTP virtual host configuration with the configuration block shown below:

      <VirtualHost 127.0.0.1:80 _default_:80>
        ServerAlias *
        DocumentRoot /opt/bitnami/projects/APPNAME/public
        <Directory "/opt/bitnami/projects/APPNAME/public">
          Options -Indexes +FollowSymLinks -MultiViews
          AllowOverride All
          Require all granted
        </Directory>
      </VirtualHost>
      
    • Create the /opt/bitnami/apache2/conf/vhosts/APPNAME-https-vhost.conf file containing the application HTTPS virtual host configuration with the configuration block shown below:

      <VirtualHost 127.0.0.1:443 _default_:443>
        ServerAlias *
        DocumentRoot /opt/bitnami/projects/APPNAME/public
        SSLEngine on
        SSLCertificateFile "/opt/bitnami/apache2/conf/bitnami/certs/server.crt"
        SSLCertificateKeyFile "/opt/bitnami/apache2/conf/bitnami/certs/server.key"
        <Directory "/opt/bitnami/projects/APPNAME/public">
          Options -Indexes +FollowSymLinks -MultiViews
          AllowOverride All
          Require all granted
        </Directory>
      </VirtualHost>
      
  • Finally, restart the Apache server:

    $ sudo /opt/bitnami/ctlscript.sh restart apache
    

You can now verify that the example application is working by visiting its URL using your browser at http://SERVER-IP/. Here is an example of what you might see (Laravel 7):

Laravel welcome page

Configuration

Before using the example application, here are a few important points to consider:

  • To start a new project, edit the file at routes/web.php:

    Route::get('my-first-route', function()
    {
        return 'Hello World!';
    });
    

    This will create the application route /my-first-route. To see this route in action, append this route to the application URL and visit it in your browser, such as http://SERVER-IP/my-first-route. If all is working correctly, you will see the output “Hello World!”.

  • If your application will use a database, edit the .env file.

    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=database_name
    DB_USERNAME=user
    DB_PASSWORD=pass
    

    MySQL support is already available by default. If you plan to use PostgreSQL, enable the pdo_pgsql extension in the /opt/bitnami/php/etc/php.ini file.

    extension=pdo_pgsql
    

Key Regeneration

A random key of 32 characters is generated during the installation. You can change it later to another random key.

$ cd /opt/bitnami/projects/APPNAME
$ php artisan key:generate

These commands will generate a new key and automatically write it to the .env file:

APP_KEY=base64:ZPk7xxAwtRBn2tN8lrOFAzcXotIsouLULGv/WXHClUg=

Approach B: Self-contained Bitnami installations

The Laravel framework is installed in the frameworks/laravel directory in the installation directory. This directory includes an example application. Application configuration files are in the conf/ directory and public files, such as HTML pages, CSS and JavaScript files, images and other media assets are stored in the public/ directory. In this guide, you will learn how to enable and configure a Laravel project.

Activation and Testing

To enable the example application, edit the Apache configuration file at /opt/bitnami/apache2/conf/bitnami/bitnami-apps-prefix.conf and uncomment the following line

Include "/opt/bitnami/frameworks/laravel/conf/httpd-prefix.conf"

Then, restart the Apache server.

$ sudo /opt/bitnami/ctlscript.sh restart apache

You can now verify that the example application is working by visiting its URL using your browser at http://SERVER-IP/laravel.

Here is an example of what you might see (Laravel 7):

Laravel 7 welcome page

Configuration

Before using the example application, here are a few important points to consider:

  • To start a new project, edit the file at routes/web.php:

    Route::get('my-first-route', function()
    {
        return 'Hello World!';
    });
    

    This will create the application route /my-first-route. To see this route in action, append this route to the application URL and visit it in your browser, such as http://SERVER-IP/my-first-route. If all is working correctly, you will see the output “Hello World!”.

  • If your application will use a database, edit the .env file.

    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=database_name
    DB_USERNAME=user
    DB_PASSWORD=pass
    

    MySQL support is already available by default. If you plan to use PostgreSQL, enable the php_pdo_pgsql extension in the /opt/bitnami/php/etc/php.ini file.

    extension=php_pdo_pgsql
    
  • To move the Laravel application such that it is available at the root URL of the server (without the /laravel URL suffix), follow these steps:

    • Edit the /opt/bitnami/frameworks/laravel/conf/httpd-prefix.conf file so that it looks like this:

      DocumentRoot "/opt/bitnami/frameworks/laravel/public"
      #Alias /laravel/ "/opt/bitnami/frameworks/laravel/public/"
      #Alias /laravel "/opt/bitnami/frameworks/laravel/public"
      Include "/opt/bitnami/frameworks/laravel/conf/httpd-app.conf"
      
    • Edit the /opt/bitnami/frameworks/laravel/conf/httpd-app.conf file and replace the AllowOverride None directive with the AllowOverride All directive:

      AllowOverride All
      
    • Restart the Apache server:

      $ sudo /opt/bitnami/ctlscript.sh restart apache
      

    You should now be able to access the example application at the root URL of your server.

Key Regeneration

A random key of 32 characters is generated during the installation. You can change it later to another random key.

$ cd /opt/bitnami/frameworks/laravel
$ sudo php artisan key:generate

These commands will generate a new key and automatically write it to the .env file:

APP_KEY=base64:ZPk7xxAwtRBn2tN8lrOFAzcXotIsouLULGv/WXHClUg=

More Information

Learn more about developing applications with Laravel at https://laravel.com/docs/.

Last modification July 29, 2020