如何在 Alpine Linux 上安装 PHP 7 fpm
Nginx 是一个免费的开源 Web 服务器。您需要 nginx 来显示静态或动态网页。Nginx 还可以充当反向代理和负载平衡器。PHP 是一种免费的开源服务器端编程语言。本教程介绍如何在 Alpine Linux 上安装 PHP 7.x 和 nginx。
教程详细信息 | |
---|---|
难度等级 | 简单的 |
Root 权限 | 是的 |
要求 | Linux 或 Unix 终端 |
类别 | 包管理器 |
先决条件 | Alpine Linux |
操作系统兼容性 | Alpine • Linux |
预计阅读时间 | 3 分钟 |
步骤 1:安装 Nginx Web 服务器
输入以下apk 命令在 Alpine Linux 上安装 nginx服务器:
# apk update && apk upgrade
# apk add nginx
# cat /etc/apk/repositories
http://dl-cdn.alpinelinux.org/alpine/v3.16/main
http://dl-cdn.alpinelinux.org/alpine/v3.16/community
# The following repos enabled for PHP 7 on the latest version #
http://dl-cdn.alpinelinux.org/alpine/edge/main
http://dl-cdn.alpinelinux.org/alpine/edge/community
http://dl-cdn.alpinelinux.org/alpine/edge/testing
第 2 步:在 Alpine Linux 上安装 PHP 7 fpm
输入以下apk 命令来安装 PHP 7.x 语言运行引擎:
# apk add php7 php7-fpm php7-opcache
图 01:安装 PHP 7
# php -v
# php --version
PHP 7.4.30 (cli) (built: Jul 5 2022 22:41:04) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.30, Copyright (c), by Zend Technologies
步骤 3:安装 PHP 7.x 模块
添加流行的 PHP 模块,例如 MySQL、GD 等:
# apk add php7-gd php7-mysqli php7-zlib php7-curl
图 02:安装 PHP 7 模块
# apk search php7
# apk search php7 | more
php7-intl-7.4.30-r0
php7-openssl-7.4.30-r0
php7-dba-7.4.30-r0
php7-sqlite3-7.4.30-r0
php7-pear-7.4.30-r0
php7-tokenizer-7.4.30-r0
php7-phpdbg-7.4.30-r0
php7-pecl-imagick-dev-3.6.0-r0
php7-pecl-protobuf-3.19.1-r0
xapian-bindings-php7-1.4.18-r2
php7-litespeed-7.4.30-r0
php7-gmp-7.4.30-r0
php7-phalcon-4.1.2-r0
php7-pecl-maxminddb-1.11.0-r0
php7-pdo_mysql-7.4.30-r0
php7-sodium-7.4.30-r0
php7-pcntl-7.4.30-r0
php7-common-7.4.30-r0
php7-pecl-oauth-2.0.7-r1
php7-xsl-7.4.30-r0
php7-7.4.30-r0
php7-fpm-7.4.30-r0
php7-pecl-mailparse-3.1.3-r0
php7-pecl-gmagick-2.0.6_rc1-r1
php7-pecl-imagick-3.6.0-r0
php7-mysqlnd-7.4.30-r0
php7-enchant-7.4.30-r0
php7-pecl-uuid-1.2.0-r0
php7-pspell-7.4.30-r0
php7-pecl-ast-1.0.16-r0
php7-pecl-redis-5.3.6-r0
php7-snmp-7.4.30-r0
php7-doc-7.4.30-r0
php7-tideways_xhprof-5.0.4-r0
php7-pecl-uploadprogress-doc-2.0.2-r0
php7-fileinfo-7.4.30-r0
php7-mbstring-7.4.30-r0
php7-pecl-zstd-0.11.0-r0
php7-pecl-lzf-1.6.8-r1
php7-pecl-amqp-1.11.0-r0
php7-pecl-yaml-2.2.2-r0
php7-pecl-memcache-4.0.5.2-r1
php7-pecl-timezonedb-2021.5-r0
php7-dev-7.4.30-r0
php7-pecl-psr-1.1.0-r0
php7-xmlrpc-7.4.30-r0
php7-pecl-rdkafka-5.0.2-r0
php7-embed-7.4.30-r0
php7-xmlreader-7.4.30-r0
php7-pdo_sqlite-7.4.30-r0
php7-exif-7.4.30-r0
php7-pecl-msgpack-2.2.0_rc1-r0
php7-opcache-7.4.30-r0
php7-ldap-7.4.30-r0
php7-posix-7.4.30-r0
php7-session-7.4.30-r0
php7-gd-7.4.30-r0
php7-pecl-xdebug-3.1.4-r0
php7-pecl-mongodb-1.11.1-r0
php7-gettext-7.4.30-r0
php7-pecl-couchbase-3.2.2-r0
php7-json-7.4.30-r0
php7-xml-7.4.30-r0
php7-iconv-7.4.30-r0
php7-sysvshm-7.4.30-r0
php7-curl-7.4.30-r0
php7-shmop-7.4.30-r0
php7-odbc-7.4.30-r0
php7-pecl-uploadprogress-2.0.2-r0
php7-phar-7.4.30-r0
php7-pdo_pgsql-7.4.30-r0
php7-imap-7.4.30-r0
php7-pecl-apcu-5.1.21-r0
mongo-php7-library-doc-1.8.0-r1
php7-pdo_dblib-7.4.30-r0
php7-pgsql-7.4.30-r0
php7-pdo_odbc-7.4.30-r0
php7-pecl-igbinary-3.2.6-r0
php7-pecl-xhprof-2.3.5-r0
mongo-php7-library-1.8.0-r1
php7-zip-7.4.30-r0
php7-apache2-7.4.30-r0
php7-cgi-7.4.30-r0
php7-ctype-7.4.30-r0
php7-pecl-mcrypt-1.0.4-r0
php7-bcmath-7.4.30-r0
php7-calendar-7.4.30-r0
php7-tidy-7.4.30-r0
php7-dom-7.4.30-r0
php7-sockets-7.4.30-r0
php7-pecl-zmq-1.1.3-r8
php7-pecl-event-3.0.6-r0
php7-pecl-vips-1.0.12-r0
php7-pecl-memcached-3.1.5-r2
php7-brotli-0.11.1-r0
unit-php7-1.26.1-r0
php7-dbg-7.4.30-r0
php7-soap-7.4.30-r0
php7-sysvmsg-7.4.30-r0
php7-pecl-ssh2-1.3.1-r0
php7-ffi-7.4.30-r0
php7-ftp-7.4.30-r0
php7-sysvsem-7.4.30-r0
php7-pdo-7.4.30-r0
php7-bz2-7.4.30-r0
php7-mysqli-7.4.30-r0
php7-pecl-xhprof-assets-2.3.5-r0
php7-simplexml-7.4.30-r0
php7-xmlwriter-7.4.30-r0
或者将grep 命令与apk 命令一起使用:
# apk search php7 | grep -i gd
步骤4:启动php-fpm7服务器
确保 nginx 和 php-fpm7 在系统重启时启动:
# rc-update add nginx default
* service nginx added to runlevel default
# rc-update add php-fpm7 default
* service php-fpm7 added to runlevel default
重启 nginx 和 PHP7-fpm 服务器的命令
# rc-service nginx restart
# rc-service php-fpm7 restart
示例输出:
图 03:启动/停止 PHP 和 Nginx 服务
停止 nginx 和 PHP7-fpm 服务器的命令
# rc-service nginx stop
# rc-service php-fpm7 stop
启动 nginx 和 PHP7-fpm 服务器的命令
# rc-service nginx start
# rc-service php-fpm7 start
步骤5:配置PHP 7
按如下方式更新您的虚拟主机配置文件
# vi /etc/nginx/conf.d/ssl.newsletter.example.com.conf
在服务器上下文中附加/编辑:
location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; }
保存并关闭文件。以下是完整的配置文件:
## START: SSL/HTTPS newsletter.example.com ### server { listen 443 http2; server_name newsletter.example.com; ssl on; ssl_certificate /etc/nginx/ssl/letsencrypt/newsletter.example.com/newsletter.example.com.cer; ssl_certificate_key /etc/nginx/ssl/letsencrypt/newsletter.example.com/newsletter.example.com.key; ssl_session_timeout 1d; ssl_session_cache shared:SSL:50m; ssl_session_tickets off; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS; ssl_dhparam /etc/nginx/ssl/letsencrypt/newsletter.example.com/dhparams.pem; ssl_prefer_server_ciphers on; ## Improves TTFB by using a smaller SSL buffer than the nginx default ssl_buffer_size 8k; ## Enables OCSP stapling ssl_stapling on; resolver 8.8.8.8; ssl_stapling_verify on; ## Send header to tell the browser to prefer https to http traffic #add_header Strict-Transport-Security max-age=31536000; ## SSL logs ## access_log /var/log/nginx/newsletter.example.com_ssl_access.log; error_log /var/log/nginx/newsletter.example.com_ssl_error.log; #-------- END SSL config -------## root /var/www/localhost/htdocs; index index.html index.htm index.php; server_name newsletter.example.com; # configure php location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; } # rest of your config ## } ## END SSL newsletter.example.com ######
确保重新启动 nginx 和 php7:
# rc-service nginx restart
# rc-service php-fpm7 restart
步骤6:测试PHP 7
在根目录(例如 /var/www/localhost/htdocs)中创建一个 php 脚本文件,
# vi test.php
添加以下内容:
<?php phpinfo(); ?>
保存并关闭文件。测试一下:
http://your-domain/test.php
或者
https://your-domain/test.php
- 在 Alpine Linux 上安装 Nginx
- 在 Alpine Linux 上安装 PHP7-fpm
- 如何安装和配置 logrotate
- 如何在 Alpine Linux 上为 Nginx 证书安装 Letsencrypt 免费 SSL/TLS