nativeInstallernginx

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 "installdir/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:

  • If you installed the stack using an administrator account, run the following commands to create the directories and assign the necessary directory permissions:

    $ sudo mkdir installdir/myapp
    $ sudo chown -R $USER:daemon installdir/myapp
    $ sudo chmod -R g+w installdir/myapp
    

    If you installed the stack using an unprivileged user account, run the following commands instead:

    $ mkdir installdir/myapp
    $ chown -R $USER:$USER installdir/myapp
    $ chmod -R g+w installdir/myapp
    
  • Create and edit the installdir/nginx/conf/server_blocks/myapp-server-block.conf file and add the configuration block shown below:

    server {
        # Port to listen on, can also be set in IP:PORT format
        listen 80 default_server;
        root installdir/myapp;
        # Catch-all server block
        # See: https://nginx.org/en/docs/http/server_names.html#miscellaneous_names
        server_name _;
        include  "installdir/nginx/conf/bitnami/*.conf";
    }
    
  • Create and edit the installdir/nginx/conf/server_blocks/myapp-https-server-block.conf file and add the configuration block shown below:

    server {
        # Port to listen on, can also be set in IP:PORT format
        listen 443 ssl default_server;
        root installdir/myapp;
        # Catch-all server block
        # See: https://nginx.org/en/docs/http/server_names.html#miscellaneous_names
        server_name _;
        ssl_certificate      bitnami/certs/server.crt;
        ssl_certificate_key  bitnami/certs/server.key;
        include  "installdir/nginx/conf/bitnami/*.conf";
    }
    
  • Restart the NGINX server:

    $ sudo installdir/ctlscript.sh restart nginx
    

    You should now be able to access the application at http://localhost/.

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 installdir/nginx/html/ directory. 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 installdir/docs/demo folder into the installdir/apps directory.

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

      include "installdir/apps/demo/conf/nginx-prefix.conf";
      
    • Restart the NGINX server using the command-line tool.

      $ sudo installdir/ctlscript.sh restart nginx
      

    You should now be able to access the demo application at http://localhost/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 depending on the approach you prefer.

  • If you intend to have multiple applications under the Web server root, follow the steps below. These steps assume that your application will live in the installdir/apps/myapp/ directory:

    • If you installed the stack using an administrator account, run the following commands to create the directories and assign the necessary directory permissions:

      $ sudo mkdir installdir/apps/myapp
      $ sudo mkdir installdir/apps/myapp/htdocs/
      $ sudo mkdir installdir/apps/myapp/conf
      $ sudo chown -R $USER:daemon installdir/apps/myapp/htdocs/
      $ sudo chmod -R g+w installdir/apps/myapp/htdocs/
      

      If you installed the stack using an unprivileged user account, run the following commands instead:

      $ mkdir installdir/apps/myapp
      $ mkdir installdir/apps/myapp/htdocs/
      $ mkdir installdir/apps/myapp/conf
      $ chown -R $USER:$USER installdir/apps/myapp/htdocs/
      $ chmod -R g+w installdir/apps/myapp/htdocs/
      
    • Create and edit the installdir/apps/myapp/conf/nginx-prefix.conf file and add the configuration block shown below:

      location /myapp {
        alias "installdir/apps/myapp/htdocs/";
        include "installdir/apps/myapp/conf/nginx-app.conf";
      }
      
    • Create and edit the installdir/apps/myapp/conf/nginx-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.

      index index.php index.html index.htm;
      location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_read_timeout 300;
        fastcgi_pass unix:/opt/bitnami/php/var/run/www.sock;
        fastcgi_index index.php;
        fastcgi_param  SCRIPT_FILENAME $request_filename;
        include fastcgi_params;
      }
      
    • Add the following line to the end of the installdir/nginx/conf/bitnami/bitnami-apps-prefix.conf file:

      include "installdir/apps/myapp/conf/nginx-prefix.conf";
      
    • Restart the NGINX server:

      $ sudo installdir/ctlscript.sh restart nginx
      

    You should now be able to access the application at http://localhost/myapp.

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

    • Edit the installdir/apps/myapp/conf/nginx-prefix.conf file and add the configuration block shown below:

      location / {
        alias "installdir/apps/myapp/htdocs/";
        include "installdir/apps/myapp/conf/nginx-app.conf";
      }
      
    • Restart the NGINX server:

      $ sudo installdir/ctlscript.sh restart nginx
      

    You should now be able to access the application at http://localhost/.

  • If you intend to use a separate virtual host for each application on the Web server, follow the steps below. These steps assume that your application will live in the installdir/apps/myapp/ directory

    • Edit the installdir/apps/myapp/conf/nginx-vhosts.conf and add the content below to it:

      server {
        listen    80;
        root "installdir/apps/myapp/htdocs/";
        server_name  myapp.example.com www.myapp.example.com;
        include "installdir/apps/myapp/conf/nginx-app.conf";
      }
      
      server {
        listen    443 ssl;
        root   "installdir/apps/myapp/htdocs/";
        server_name  myapp.example.com www.myapp.example.com;
        ssl_certificate  "installdir/apps/myapp/conf/certs/server.crt";
        ssl_certificate_key  "installdir/apps/myapp/conf/certs/server.key";
        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
        include "installdir/apps/myapp/conf/nginx-app.conf";
      }
      

    Remember to either copy the SSL certificates to the installdir/apps/myapp/conf/certs folder or update the path to the certificates in the above configuration file.

    • Add the following line to the end of the installdir/nginx/conf/bitnami/bitnami-apps-vhosts.conf file:

      include "installdir/apps/myapp/conf/nginx-vhosts.conf";
      
    • Restart the NGINX server:

      $ sudo installdir/ctlscript.sh restart nginx
      
    • Access the application at http://myapp.example.com.

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.

Last modification October 6, 2020