All Products
Search
Document Center

Elastic Compute Service:Build the ThinkPHP framework

Last Updated:Oct 31, 2024

ThinkPHP is an open source, fast, simple, lightweight, and object-oriented PHP development framework distributed under the Apache2 open source license. ThinkPHP is designed to develop agile web applications and to simplify enterprise application development. This topic describes how to build the ThinkPHP 8.0 framework on Elastic Compute Service (ECS) instances that run Alibaba Cloud Linux 3, Alibaba Cloud Linux 2, and CentOS 7.x.

Prerequisites

This topic is intended for users who are learning PHP or developing applications based on the ThinkPHP framework. The ECS instance must meet the following requirements when you build the ThinkPHP framework:

  • A public IP address is assigned to the instance or an elastic IP address (EIP) is associated with the instance.

  • The operating system Alibaba Cloud Linux 3, Alibaba Cloud Linux 3, or CentOS 7.x.

  • The inbound rules of the security group of the instance allow traffic on ports 22, 8000, and 443. 8000 is the default port of ThinkPHP. For more information, see Add a security group rule.

Procedures

  1. Install the PHP 8.0.

    Note

    ThinkPHP 8.0 requires PHP 8.0 or later.

    1. If your instance runs Alibaba Cloud Linux 3, install the OpenSSL10 dependencies.

      sudo yum install -y compat-openssl10
    2. Install PHP.

      Alibaba Cloud Linux 3

      1. Run the following command to update the YUM repository:

        sudo rpm -Uvh https://mirrors.aliyun.com/remi/enterprise/remi-release-8.rpm  --nodeps
      2. Run the following command to enable the php:remi-8.0 module stream:

        sudo yum module enable -y php:remi-8.0
      3. Run the following command to install PHP:

        sudo yum install -y php php-cli php-fpm php-common php-mysqlnd php-gd php-mbstring php-xml

      Alibaba Cloud Linux 2

      1. Run the following command to update the YUM repository:

        sudo rpm -Uvh https://mirrors.aliyun.com/remi/enterprise/remi-release-7.rpm
      2. Run the following command to modify the configuration of the yum-plugin-releasever-adapter plugin to support remi repository adaptation for Alibaba Cloud Linux 2:

        sudo echo ", remi-php54.repo, remi-php71.repo, remi-php73.repo, remi-php80.repo, remi-php82.repo, remi.repo, epel.repo, remi-modular.repo, remi-php70.repo, remi-php72.repo, remi-php74.repo, remi-php81.repo, remi-php83.repo, remi-safe.repo" >> /etc/yum/pluginconf.d/releasever-adapter.conf
      3. Run the following command to enable the PHP 8.0 repository:

        sudo sed -i '1,10s/enabled=0/enabled=1/' /etc/yum.repos.d/remi-php80.repo
      4. Run the following command to install PHP:

        sudo yum install -y php php-cli php-fpm php-common php-mysqlnd php-gd php-mbstring

      CentOS 7.x

      1. Update the YUM repositories.

        1. Run the following commands to install the Extra Packages for Enterprise Linux (EPEL) repository and Remi repository:

          sudo yum install -y epel-release
          sudo yum install -y https://rpms.remirepo.net/enterprise/remi-release-7.rpm
        2. Run the following command to enable the PHP 8.0 repository:

          sudo yum install -y yum-utils
          sudo yum-config-manager --enable remi-php80
      2. Run the following command to install PHP:

        sudo yum install -y php php-cli php-fpm php-mysqlnd php-zip php-devel php-gd php-mcrypt php-mbstring php-curl php-xml php-pear php-bcmath php-json
    3. Run the following command to view the version of PHP:

      php -v

      The following command output indicates that PHP is installed:

      PHP 8.0.30 (cli) (built: Aug  3 2023 17:13:08) ( NTS gcc x86_64 )
      Copyright (c) The PHP Group
      Zend Engine v4.0.30, Copyright (c) Zend Technologies           
  2. Install Composer.

    Composer is a dependency management tool of PHP. It allows you to define and manage the external dependencies required by the project. It can also automatically install, update, and load such dependencies. For more information, visit the official website of Composer.

    1. Install dependencies required by Composer.

      sudo yum install -y unzip git
    2. Install Composer.

      curl -sS https://getcomposer.org/installer | php
      sudo mv composer.phar /usr/local/bin/composer
    3. View the Composer version.

      composer --version

      The following information indicates that Composer is installed.

      image

  3. Install ThinkPHP.

    1. Create a new ThinkPHP application by using Composer.

      Run the following command to create the my-thinkphp-app directory in the current directory and download the core files and dependencies of ThinkPHP.

      composer create-project topthink/think my-thinkphp-app
    2. Change directory to the new ThinkPHP application directory and start the ThinkPHP built-in server.

      cd my-thinkphp-app
      php think run

      The following information indicates that ThinkPHP is started.

      image

    3. Open your browser and enter http://<Public IP address of the ECS instance>:8000 in the address bar.

      When the interface can be accessed normally indicates that ThinkPHP is deployed.

  4. Configure the Web server (production environment).

    You need to create a web server, such as Apache HTTP Server or NGINX Web Server in a production environment to deploy the ThinkPHP application. You need to properly configure URL rewrite rules of the web server to ensure that framework routing works.

    Configuration example of Apache. The mod_rewrite module must be enabled:

    <IfModule mod_rewrite.c>
        Options +FollowSymlinks -Indexes
        RewriteEngine On
    
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
    </IfModule>
    

    Configuration example of NGINX:

    location / {
        if (!-e $request_filename) {
           rewrite  ^(.*)$  /index.php?s=/$1  last;
           break;
        }
    }