CentOS 7部署ThinkPHP 5:完整指南

引言

ThinkPHP 5是一款广受欢迎的PHP开发框架,而CentOS 7则是稳定可靠的服务器操作系统。本文将指导您在CentOS 7上成功部署ThinkPHP 5,打造高效的Web开发环境。

环境准备

在开始部署之前,我们需要确保CentOS 7系统已经更新到最新版本。执行以下命令:

sudo yum update -y

安装LNMP环境

ThinkPHP 5需要LNMP(Linux, Nginx, MySQL, PHP)环境。按照以下步骤安装:

1. 安装Nginx

sudo yum install epel-release
sudo yum install nginx
sudo systemctl start nginx
sudo systemctl enable nginx

2. 安装MySQL

sudo yum install mariadb-server mariadb
sudo systemctl start mariadb
sudo systemctl enable mariadb
sudo mysql_secure_installation

3. 安装PHP 7.2及必要扩展

sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
sudo yum-config-manager --enable remi-php72
sudo yum install php php-fpm php-mysql php-gd php-xml php-mbstring php-opcache
sudo systemctl start php-fpm
sudo systemctl enable php-fpm

安装Composer

Composer是PHP的依赖管理工具,用于安装ThinkPHP 5。执行以下命令:

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
sudo chmod +x /usr/local/bin/composer

部署ThinkPHP 5

现在,我们可以使用Composer来安装ThinkPHP 5:

cd /var/www/html
composer create-project topthink/think tp5

配置Nginx

创建一个新的Nginx配置文件:

sudo nano /etc/nginx/conf.d/tp5.conf

添加以下内容:

server {
    listen 80;
    server_name your_domain.com;
    root /var/www/html/tp5/public;

    location / {
        index index.php index.html index.htm;
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ .php$ {
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

保存文件并重启Nginx:

sudo systemctl restart nginx

设置权限

确保Web服务器有正确的权限访问ThinkPHP 5文件:

sudo chown -R nginx:nginx /var/www/html/tp5
sudo chmod -R 755 /var/www/html/tp5

优化性能

为提高ThinkPHP 5的运行效率,可以启用PHP OpCache和Nginx的Gzip压缩:

启用OpCache

编辑PHP配置文件:

sudo nano /etc/php.ini

找到并修改以下设置:

opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=1

启用Nginx Gzip

编辑Nginx主配置文件:

sudo nano /etc/nginx/nginx.conf

在http块中添加或修改以下内容:

gzip on;
gzip_comp_level 5;
gzip_min_length 256;
gzip_proxied any;
gzip_types
  application/atom+xml
  application/javascript
  application/json
  application/ld+json
  application/manifest+json
  application/rss+xml
  application/vnd.geo+json
  application/vnd.ms-fontobject
  application/x-font-ttf
  application/x-web-app-manifest+json
  application/xhtml+xml
  application/xml
  font/opentype
  image/bmp
  image/svg+xml
  image/x-icon
  text/cache-manifest
  text/css
  text/plain
  text/vcard
  text/vnd.rim.location.xloc
  text/vtt
  text/x-component
  text/x-cross-domain-policy;

结语

通过以上步骤,您已经成功在CentOS 7上部署了ThinkPHP 5框架。这个强大的组合为您的PHP开发项目提供了稳定高效的运行环境。记得定期更新系统和软件包,以确保安全性和性能。祝您开发愉快!

寰宇互联服务器4核4G云服务器1元/月,网络稳定、抗DDos、国际BGP、性能强劲,十年服务经验QQ:97295700 微信:huanidc

阅读剩余
THE END