2023 PHP project deployment notes. Wait, people still use PHP?
PHP? Still using it in 2023? Docker deployment is a headache; it's easier to just install php-fpm and nginx directly. Permission issues? Just chown it to fix. Composer installs dependencies and everything runs perfectly.
目录28 个章节
Preface
This is my first time using PHP's package manager, Composer.
I initially tried deploying with Docker, but kept running into issues. In the end, I decided to go with directly installing php-fpm on the server paired with Nginx.
PS: The Docker approach was truly exhausting; just thinking about it still gives me chills 😢
Docker Solution
Let me start by sharing my Docker solution, which took quite some troubleshooting but is now in a basically usable state.
Directory Structure
The directory structure is as follows:
php-code
├─ log // 日志
│ ├─ nginx
│ └─ php
├─ conf // 配置
│ ├─ nginx
│ │ ├─ nginx.conf
│ │ └─ test.conf
│ └─ php
│ ├─ conf.d
│ │ └─ docker-php-ext-xdebug.ini
│ ├─ php-fpm.conf
│ └─ php.ini
├─ code // PHP代码
├─ docker-compose.yml
├─ Dockerfile
└─ .env
nginx Configuration
server {
listen 8080;
server_name localhost;
root /usr/share/nginx/html/public;
index index.php index.html index.htm;
charset utf-8;
client_max_body_size 100M;
location ~ \.php$ {
fastcgi_pass php-fpm:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/public$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
include fastcgi_params;
}
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?s=$1 last; break;
}
}
}
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log warn;
server_tokens off;
PHP Configuration
The main purpose is to enable debug logging to view any errors that occur during program execution.
docker-php-ext-xdebug.ini
xdebug.remote_enable = On
xdebug.remote_handler = dbgp
xdebug.remote_host = host.docker.internal
xdebug.remote_port = 9001
xdebug.remote_log = /var/log/php/xdebug.log
xdebug.idekey = PHPSTOR
php-fpm.conf
[global]
error_log = /proc/self/fd/2
; https://github.com/docker-library/php/pull/725#issuecomment-443540114
log_limit = 8192
[www]
; if we send this to /proc/self/fd/1, it never appears
access.log = /proc/self/fd/2
clear_env = no
; Ensure worker stdout and stderr are sent to the main error log.
catch_workers_output = yes
decorate_workers_output = no
php.ini
log_errors=On
dockerfile
To build a Docker image, the required PHP version for this project is 7.4.
What strikes me as odd is that the official PHP image does not come with Composer, so you have to troubleshoot it yourself.
This Dockerfile was based on various articles and blogs, but it turns out that troubleshooting it yourself is even better. The known issue is that the
gdextension installation does not take effect. It is suspected that you still need to useaptto installphp-gd, and thendocker-php-ext-installcan work.
FROM composer:2.2 as composer
FROM php:7.4.33-fpm as php
Image Information
LABEL Author="DealiAxy" LABEL Version="2023.11" LABEL Description="PHP 7-fpm development environment image."
Switch to Domestic Mirror Sources
RUN apt install apt-transport-https ca-certificates \
&& mv /etc/apt/sources.list /etc/apt/sources.list.bak
&& echo "Acquire::http::Pipeline-Depth "0";" > /etc/apt/apt.conf.d/99nopipelining
&& echo 'deb http://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free' > /etc/apt/sources.list
&& echo 'deb http://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free' >> /etc/apt/sources.list
&& echo 'deb http://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free' >> /etc/apt/sources.list
&& apt-get update
bcmath, calendar, exif, gettext, sockets, dba
mysqli, pcntl, pdo_mysql, shmop, sysvmsg, sysvsem, sysvshm extensions
RUN docker-php-ext-install -j$(nproc) bcmath calendar exif gettext sockets dba mysqli pcntl pdo_mysql shmop sysvmsg sysvsem sysvshm iconv
RUN apt update && apt-get install -y git zip libpng-dev libzip-dev &&
rm -rf /var/lib/apt/lists/* &&
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&
docker-php-ext-install pdo pdo_mysql mysqli gd zip bcmath opcache &&
docker-php-ext-enable mysqli gd
Copy Code
COPY --chown=www-data:www-data ./code /var/www/html
copy vendor
COPY --from=composer /usr/bin/composer /usr/bin/composer
WORKDIR /var/www/html
RUN composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
&& composer config -g --unset repos.packagist
&& composer install
&& ls -la
EXPOSE 9000 CMD ["php-fpm"]
### Environment Variables .env
```ini
APP_NAME=php-oa
docker-compose
version: "3.6"
services:
nginx:
image: nginx:stable-alpine
container_name: ${APP_NAME}_nginx
restart: unless-stopped
volumes:
- ./conf/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
- ./code:/usr/share/nginx/html
- ./log/nginx:/var/log/nginx
ports:
- "8080:8080"
depends_on:
- php-fpm
networks:
- default
- swag
php-fpm:
build: .
user: 'www-data:www-data'
container_name: ${APP_NAME}-php
restart: unless-stopped
volumes:
# - ./conf/php:/usr/local/etc/php
- ./code/public:/var/www/html/public
- ./conf/php/php-fpm.conf:/usr/local/etc/php-fpm.d/docker.conf
- ./conf/php/php.ini:/usr/local/etc/php/php.ini
- ./log/php:/var/log/php
networks:
- default
networks:
swag:
name: swag
external: true
default:
name: ${APP_NAME}
Direct Deployment Solution
This is the easiest solution.
My server is Ubuntu 22.04. The PHP version in the official repository is 8.1, so a PPA (Personal Package Archive) needs to be added to use version 7.4.
Adding PPA Repository
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
Install PHP 7.4
Also, install common extensions.
sudo apt install php7.4 php7.4-fpm
sudo apt install php7.4-{cli,common,curl,zip,gd,mysql,xml,mbstring,json,intl}
After installation, check the status.
sudo systemctl status php7.4-fpm.service
Install and Configure nginx
I won't say much about the installation.
Configure directly.
Add a configuration file in /etc/nginx/sites-available.
The filename here is php.conf.
We place the PHP code in /var/www/html/php-code.
Set the website root directory to the public directory under the PHP code (this is how this project is designed).
server {
server_name domain.com;
listen 8081;
root /var/www/html/php-code/public;
index index.php index.html index.htm;
charset utf-8;
client_max_body_size 100M;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php-fpm.sock;
}
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?s=$1 last; break;
}
}
}
Installing Composer and Dependencies
Use Composer to install dependencies
Without this step, it would be no different from using PHP 5 or 6 years ago.
Official Source
You can install directly from the official source and use PHP to execute the installation script.
sudo apt-get install curl
sudo apt-get install php php-curl
curl -sS https://getcomposer.org/installer -o composer-setup.php
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
sudo composer self-update
Domestic Mirrors (国内源)
Download the binary file from a domestic mirror.
curl https://mirrors.cloud.tencent.com/composer/composer.phar -o /usr/local/bin/composer
curl https://mirrors.aliyun.com/composer/composer.phar -o /usr/local/bin/composer
chmod +x /usr/local/bin/composer
Check Composer
composer -v
Configure Mirror
First, configure a domestic mirror. You can refer to this project: https://github.com/china-speed/china-speed.github.io
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
composer config -g --unset repos.packagist
Install Dependencies
cd /var/www/html/php-code
composer install
Done
Handling Permission Issues
I'm not sure if all PHP projects prefer to create and modify files within the project directory, but this certainly poses a significant challenge for Docker deployment methods.
For direct deployment on the system, it's better to first transfer ownership of the code directory to the www-data user:
chown -R www-data:www-data /var/www/html
To allow the current user to also write to it, add them to the user group:
usermod -a -G www-data {username}
Oh, right, PHP files also require executable permissions...
chmod -R u+rwx /var/www/html/php-code
Add read and write permissions for users within the group:
chmod -R g+rw /var/www/html/php-code
That should basically do it.
For backends using the ThinkPHP framework, there's no concept of starting a backend service. Simply placing the code in the directory is sufficient, and it's accessible directly.
References
- https://linux.how2shout.com/how-to-install-php-7-4-on-ubuntu-22-04-lts-jammy-linux/
- https://www.myfreax.com/secure-nginx-with-let-s-encrypt-on-ubuntu-22-04/
- https://www.cherryservers.com/blog/how-to-install-composer-ubuntu
- https://www.myfreax.com/how-to-install-php-on-ubuntu-20-04/
- https://learnku.com/articles/31344
- https://github.com/mouyong/docker-php/tree/master/fpm
_EOF_
2023 PHP project deployment notes. Wait, people still use PHP?
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
PHP-Ajax-跨域问题最佳解决方案
前言 Ajax跨域问题在开发中非常常见。 例如网页的域名是client.abc.com,而请求的域名是server.abc.com。 如果直接使用ajax访问,会有以下错误: XMLHttpRequest cannot load http://server.abc.com/server.php. No 'Access-Control-Allow-Origin' header is present
2021-12-18 · PHP · 约 1 分钟
PHP7-0-新特性
变量类型 PHP7版本函数的参数和返回值增加了类型限定。 为什么PHP要加入类型呢?实际上此项特性是为了PHP7.1版本的JIT特性做准备,增加类型后PHP Jit可以准确判断变量类型,生成最佳的机器指令。 (针对密集运算的优化) 使用示例 function test(int $a, string $b, array $c) : int { //code } 错误异常 在过去,PHP程序出
2021-12-18 · PHP · 约 1 分钟
PHP7-0-+-Swoole-环境配置
前言 Swoole是一个面向生产环境的 PHP 异步网络通信引擎。 使 PHP 开发人员可以编写高性能的异步并发 TCP、UDP、Unix Socket、HTTP,WebSocket 服务。Swoole 可以广泛应用于互联网、移动通信、企业软件、云计算、网络游戏、物联网(IOT)、车联网、智能家居等领域。 使用 PHP + Swoole 作为网络通信框架,可以使企业 IT 研发团队的效率大大提升,
2021-12-18 · PHP · 约 1 分钟
个人数据保全计划:搭建私有音乐库
下载音乐防灰,工具链折腾值不值?搭建私有流媒体,客户端体验一般般。数据自主权在手,平台下架算什么?
2023-09-29 · 个人数据保全计划 · 约 1 分钟
讨论
共 0 条加载评论中…