ocinginx

Create a custom PHP application

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, you have two options:

  • 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/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. These steps assume that your application will live in the /opt/bitnami/apps/myapp/ directory:

    • Run the following commands to create the directories:

      $ sudo mkdir /opt/bitnami/apps/myapp
      $ sudo mkdir /opt/bitnami/apps/myapp/htdocs/
      $ sudo mkdir /opt/bitnami/apps/myapp/conf
      
    • Create and edit the /opt/bitnami/apps/myapp/conf/nginx-prefix.conf file and add the line below to it:

      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
      
    • Access the application at http://SERVER-IP/myapp.

  • If you want to use a virtual host for an application located at /opt/bitnami/apps/myapp, 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";
    }
    
    • 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.