How to Use PHP on a Gandi Web Hosting

This web hosting family enables you to host dynamic or static websites using PHP and one of the available databases.

Prerequisites

You must have a file-transfer client that can upload files via SFTP (like FileZilla), or git and an SSH client to push and deploy code from the command line.

Version support

Web Hosting currently supports PHP 7.2, PHP 7.3, PHP 7.4, PHP 8.0, PHP 8.1 and PHP 8.2 only for Wordpress auto installation.

To learn how to update to the latest version of PHP see our instructions on updating a language.

General Functioning

Your web hosting can host a number of PHP-powered or static HTML websites. For each of your websites, you can register each as a new Site on your web hosting.

Each site gets its own folder inside your web hosting. Files uploaded to these folders via sFTP will be served at the corresponding URLs. Each Site also get its own git repository, so you can push and deploy code to each one independently.

The number of requests your web hosting can serve is only limited by its size, which you can upgrade at any time to accommodate more visitors. You can also make use of the platform’s free built-in HTTP caching service to get maximum performance.

You can also create an unlimited number of databases and database users on each web hosting (for example, one database and one database user per Site). You are only limited by the storage space available on your web hosting, which you can also increase at any time. All databases can be managed from the web-based control panel or the command-line clients through the SSH emergency console.

Each web hosting also gets its own username (generated by the platform) and password (chosen by you). These credentials are used to access all of the web hosting’s dedicated management interfaces:

  • Web-based Control Panel and SSH console for process, database, logs and file management

  • sFTP and git+SSH for files and dependencies management

You can also login to sFTP, git and SSH using SSH keys instead of the password.

More details can be found in the sections below.

Dependency Management

You can use Composer to manage dependencies on Web Hosting by adding the composer.json and composer.lock files to your project’s ../htdocs folder.

To get Web Hosting to install your dependencies, you’ll need to use the Git+SSH workflow to deploy your code, instead of uploading your files with sFTP.

Instructions:

  1. Place your composer.phar file into the root of your project folder

  2. Run Composer to install the dependencies locally

  3. Add both Composer files, composer.json and composer.lock to your git tree

  4. Push your code to your Site’s git repository

  5. Deploy the code with SSH

Check out our Composer tutorial for a full example.

Sites

Each Site that you create via the management interface at Gandi will generate a new directory in your web hosting.

You will upload your files via sFTP to your web hosting in your Site’s directory.

Here’s an example of an absolute path to a Site’s htdocs directory:

/lamp0/web/vhosts/www.yourdomainname.com/htdocs/

where “www.yourdomainname.com” is the name of the Site you specified in your web hosting admin page.

Logs

You can view your PHP and Apache logs from your web hosting’s Control Panel, sFTP or SSH console.

Apache Logs:

error.log: Contains all errors related to the Apache Web server. It is recommended to verify these logs regularly to detect possible configuration problems (bad directives on .htaccess, missing index files, and so on).

PHP Logs:

admin-error.log: PHP logs executed on the admin part of your web hosting. fpm.log: Logs related to the PHP interpreter. It’s here that you can see if the max number of PHP processor is reached. www-error.log:PHP logs executed on the public part of your web hosting.

Understanding the logs

Note

Logs are in the UTC time zone by default.

Apache logs

[Thu Jun 14 12:08:45.199853 2012] [mpm_event:notice] [pid 186:tid 3557842618176] AH00489: Apache/2.4.1 (Unix) configured -- resuming normal operations
[Thu Jun 14 12:08:45.199869 2012] [core:notice] [pid 186:tid 3557842618176] AH00094: Command line: '/usr/sbin/apache2 -d /srv/data/.config/apache -f /srv/data/.config/apache/apache2.conf -D lamp0-1'
[Thu Jun 14 15:08:45.213486 2012] [mpm_event:notice] [pid 186:tid

These lines indicates that the web server has been started.

PHP Logs

[08-Jun-2012 12:56:29] WARNING: [pool www] server reached max_children setting (x), consider raising it

This alert occurs when PHP-FPM has reached the maximum number of simultaneous processes for PHP. The maximum number of simultaneous process is defined in terms of the size of your web hosting (2 on a pack S, 4 on a M, 8 on a L …). If this error occurs too frequently, you should consider increasing the size of your web hosting Web Hosting. When you see [pool www], it is the number of processes running on the public part of your web hosting, [pool admin] denotes the admin part of the proceedings.

[05-Jun-2012 09:08:36] WARNING: [pool admin] 'user' directive is ignored when FPM is not running as root

This alert is not an error and can be ignored.

Databases

You can choose between two types of databases: MySQL or PostgreSQL.

You can manage both via their web management interfaces (PHPMyAdmin or phpPgAdmin, respectively) or via their command-line interfaces, accessible through the SSH console.

MySQL

MySQL 5.7 is available to use with PHP web hostings.

You can connect to the MySQL database service on localhost. The default user is root and no password is required by default. However, you should create safe credentials for real-world usage. You can also use the default_db database for fast setups and testing purposes.

You can create as many users and databases as you need; you’re only limited by your disk space, which you can increase at any moment up to 1 TB.

You can manage your MySQL database with the standard shell tools (mysql, mysqldump, etc.) via the SSH console, or from the web admin console with PHPMyAdmin. Check out the reference article about MySQL management on Web Hosting to learn more.

PHP web hostings come with pre-installed libraries to access your MySQL database from your code : mysql, mysqli and PDO.

Below is a minimalistic PHP code example to test the database connection on Web Hosting using the default connection settings:

index.php

<?php

  function test_mysql_conn() {
    $mysql_server = 'localhost';
    $mysql_user = 'root';
    $mysql_pass = '';
    $mysql_db = 'default_db';

    $conn = new mysqli($mysql_server, $mysql_user, $mysql_pass, $mysql_db);
    if ($conn->connect_error) {
      error_log("Connection to MySQL failed: " . $conn->connect_error);
      return "NOT WORKING";
    }
    return "OK";
  };

?>
<html>
  <head>
    <title>PHP MySQL on Web Hosting</title>
  </head>
  <body>
    <p>MySQL connection: <?php echo test_mysql_conn(); ?>.</p>
  </body>
</html>

PostgreSQL

PostgreSQL version 9.4 is available for Web Hosting with PHP.

Your web hosting’s PostgreSQL service will be reachable on localhost, at port 5432.

By default, you can use the hosting-db user without a password to connect to the default database, called postgres. We recommend that you create secure credentials for real-world usage.

You can create as many users and databases as you need; you’re only limited by your disk space, which you can increase at any moment up to 1 TB.

You can manage your PostgreSQL database with the standard shell commands via the SSH console (psql, pg_dump, etc.), or from the Control Panel using phpPgAdmin. Check out the reference article about PostgreSQL management on Web Hosting to learn more.

PHP web hostings come with pre-installed libraries to access your PostgreSQL database from your code : php-postgresql and PDO.

Below is a minimalistic PHP code example to test the database connection on Web Hosting using the default settings:

index.php

<?php

  function test_pg_conn() {
    $pg_conn_string = "host='localhost' user='hosting-db' dbname='postgres'";
    $conn = pg_connect($pg_conn_string);
    $conn_status = pg_connection_status($conn);
    if ($conn_status === PGSQL_CONNECTION_OK) {
      return "OK";
    } else {
      error_log("Connection to PostgreSQL failed: " . $conn_status);
      return "NOT WORKING";
    };
  };

?>
<html>
  <head>
    <title>PHP PostgreSQL on Web Hosting</title>
  </head>
  <body>
    <p>PostgreSQL connection: <?php echo test_pg_conn(); ?>.</p>
  </body>
</html>