oraclewordpress-pro

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/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 /opt/bitnami/myapp;
        # Catch-all server block
        # See: https://nginx.org/en/docs/http/server_names.html#miscellaneous_names
        server_name _;
        include  "/opt/bitnami/nginx/conf/bitnami/*.conf";
    }
    
  • Create and edit the /opt/bitnami/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 /opt/bitnami/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  "/opt/bitnami/nginx/conf/bitnami/*.conf";
    }
    
  • Restart the NGINX server:

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

    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/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 /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/nginx/conf/bitnami/bitnami-apps-prefix.conf file:

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

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

    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 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 /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/nginx-prefix.conf file and add the configuration block shown below:

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

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

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

    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/nginx-prefix.conf file and add the configuration block shown below:

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

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

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

  • 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 /opt/bitnami/apps/myapp/ directory

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

      server { listen 80; root “/opt/bitnami/apps/myapp/htdocs/”; server_name myapp.example.com www.myapp.example.com; include “/opt/bitnami/apps/myapp/conf/nginx-app.conf”; }

      server { listen 443 ssl; root “/opt/bitnami/apps/myapp/htdocs/”; server_name myapp.example.com www.myapp.example.com; ssl_certificate “/opt/bitnami/apps/myapp/conf/certs/server.crt”; ssl_certificate_key “/opt/bitnami/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 “/opt/bitnami/apps/myapp/conf/nginx-app.conf”; }

    Remember to either copy the SSL certificates to the /opt/bitnami/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 /opt/bitnami/nginx/conf/bitnami/bitnami-apps-vhosts.conf file:

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

      $ sudo /opt/bitnami/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 July 29, 2020