oraclemautic

Create a custom PHP application

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.

Many users run a Bitnami stack as a development environment for their own PHP projects (as opposed to running third-party applications such as Joomla! or WordPress). To deploy your PHP application in this environment, follow these steps.

Approach A: Bitnami installations using system packages

Create the same structure used by Bitnami when installing Bitnami PHP applications. To do this, follow these steps:

  • Run the following commands to create the directories and assign the necessary permissions. Replace the USERNAME and GROUP placeholders with the same username and group you used when installing the stack.

    $ sudo mkdir /opt/bitnami/myapp
    $ sudo chown -R USERNAME:GROUP /opt/bitnami/myapp
    $ sudo chmod -R g+w /opt/bitnami/myapp
    
  • Create and edit the /opt/bitnami/apache2/conf/vhosts/myapp-vhost.conf file and add the configuration block shown below:

    <VirtualHost 127.0.0.1:80 _default_:80>
      ServerAlias *
      DocumentRoot /opt/bitnami/myapp
      <Directory "/opt/bitnami/myapp">
        Options -Indexes +FollowSymLinks -MultiViews
        AllowOverride All
        Require all granted
      </Directory>
    </VirtualHost>
    

    NOTE: If your application uses .htaccess files, you should change the AllowOverride None option to AllowOverride All. Find out how to move the .htaccess file content to the main server configuration file.

  • Create and edit the /opt/bitnami/apache2/conf/vhosts/myapp-https-vhost.conf file and add the configuration block shown below:

    <VirtualHost 127.0.0.1:443 _default_:443>
      ServerAlias *
      DocumentRoot /opt/bitnami/myapp
      SSLEngine on
      SSLCertificateFile "/opt/bitnami/apache2/conf/bitnami/certs/server.crt"
      SSLCertificateKeyFile "/opt/bitnami/apache2/conf/bitnami/certs/server.key"
      <Directory "/opt/bitnami/myapp">
        Options -Indexes +FollowSymLinks -MultiViews
        AllowOverride All
        Require all granted
      </Directory>
    </VirtualHost>
    

    NOTE: If your application uses .htaccess files, you should change the AllowOverride None option to AllowOverride All. Find out how to move the .htaccess file content to the main server configuration file.

  • Restart the Apache server:

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

    You should now be able to access the application at http://SERVER-IP/.

Approach B: Self-contained Bitnami installations

  • To make a single PHP application accessible at the root URL of the Web server (for example, http://localhost), simply copy the PHP files into the /opt/bitnami/apache2/htdocs folder. For an example, take a look at the phpinfo example.

  • To have several applications running, create the same structure used by Bitnami when installing Bitnami PHP applications. Recent versions of Bitnami stacks ship a demo application with this structure to help you get started. To use this, follow the steps below:

    • Copy the /opt/bitnami/docs/demo folder into the /opt/bitnami/apps directory.

      $ sudo cp -r /opt/bitnami/docs/demo /opt/bitnami/apps
      
    • Add the following line to the end of the /opt/bitnami/apache2/conf/bitnami/bitnami-apps-prefix.conf file:

      Include "/opt/bitnami/apps/demo/conf/httpd-prefix.conf"
      
    • Restart the Apache server using the command-line tool.

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

    You should now be able to access the demo application at http://SERVER-IP/demo. You should see a “Hello world” message in your browser.

  • If your stack does not include a demo application, or if you prefer to create a custom PHP application from scratch, follow the steps below. These steps assume that your application will live in the /opt/bitnami/apps/myapp/ directory:

    • Run the following commands to create the directories and assign the necessary permissions:

      $ sudo mkdir /opt/bitnami/apps/myapp
      $ sudo mkdir /opt/bitnami/apps/myapp/htdocs/
      $ sudo mkdir /opt/bitnami/apps/myapp/conf
      $ sudo chown -R bitnami:daemon /opt/bitnami/apps/myapp/htdocs/
      $ sudo chmod -R g+w /opt/bitnami/apps/myapp/htdocs/
      
    • Create and edit the /opt/bitnami/apps/myapp/conf/httpd-prefix.conf file and add the line below to it:

      Alias /myapp/ "/opt/bitnami/apps/myapp/htdocs/"
      Alias /myapp "/opt/bitnami/apps/myapp/htdocs/"
      Include "/opt/bitnami/apps/myapp/conf/httpd-app.conf"
      
    • Create and edit the /opt/bitnami/apps/myapp/conf/httpd-app.conf file and add the content below to it. This is the main configuration file for your application, so modify it further depending on your application’s requirements.

      <Directory /opt/bitnami/apps/myapp/htdocs/>
          Options +FollowSymLinks
          AllowOverride None
          <IfVersion < 2.3 >
          Order allow,deny
          Allow from all
          </IfVersion>
          <IfVersion >= 2.3>
          Require all granted
          </IfVersion>
      </Directory>
      

      NOTE: If your application uses .htaccess files, you should change the AllowOverride None option to AllowOverride All. Find out how to move the .htaccess file content to the main server configuration file.

    • Once you have created the files and directories above, add the following line to the end of the main Apache configuration file at /opt/bitnami/apache2/conf/bitnami/bitnami-apps-prefix.conf, as shown below:

      Include "/opt/bitnami/apps/myapp/conf/httpd-prefix.conf"
      
    • Restart the Apache server:

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

    You should now be able to access the application at http://SERVER-IP/myapp.

    If you prefer to have the application available at the server root, perform these additional steps:

    • Edit the /opt/bitnami/apps/myapp/conf/httpd-prefix.conf file and modify it so that it looks like this:

      DocumentRoot "/opt/bitnami/apps/myapp/htdocs/"
      Include "/opt/bitnami/apps/myapp/conf/httpd-app.conf"
      
    • Restart the Apache server:

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

    You should now be able to access the application at http://SERVER-IP/.

Once you have created and deployed your custom PHP application, you can also proceed to create and integrate a MySQL/MariaDB database with your application.

Check out the following tutorial if you want to learn more about configuring a custom PHP application.

Last modification June 10, 2020