如何在Alpine Linux上安装PHP 7 fpm

时间:2020-01-09 10:39:25  来源:igfitidea点击:

如何在Alpine Linux上安装PHP 7.x和Nginx Web服务器?

Nginx是一个免费的开源Web服务器。
您需要nginx才能显示静态或动态网页。

Nginx还可以充当反向代理和负载平衡器。

PHP是一种免费的开源服务器端编程语言。
本教程介绍了如何在Alpine Linux上安装PHP 7.x和nginx。

步骤1:安装Nginx Web服务器

执行以下apk命令以在Alpine Linux上安装nginx服务器:

# apk update && apk upgrade
# apk add nginx

步骤2:在Alpine Linux上安装PHP 7 fpm

执行以下apk命令以安装PHP 7.x语言运行时引擎:

# apk add php7 php7-fpm php7-opcache

步骤3:安装PHP 7.x模块

添加流行的PHP模块,例如MySQL,GD和其他:

# apk add php7-gd php7-mysqli php7-zlib php7-curl

要搜索其他模块,请运行以下apk命令。
运行:

# apk search php7
# apk search php7 | more

或将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

停止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.theitroad.local.conf

在服务器上下文中追加/编辑:

location ~ \.php$ {
              fastcgi_pass      127.0.0.1:9000;
              fastcgi_index     index.php;
              include           fastcgi.conf;
    }

保存并关闭文件。
这是完整的配置文件:

## START: SSL/HTTPS newsletter.theitroad.local ###
server {                                        
    listen 443 http2;
    server_name newsletter.theitroad.local; 
    ssl on;
    ssl_certificate /etc/nginx/ssl/letsencrypt/newsletter.theitroad.local/newsletter.theitroad.local.cer;
    ssl_certificate_key /etc/nginx/ssl/letsencrypt/newsletter.theitroad.local/newsletter.theitroad.local.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.theitroad.local/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.theitroad.local_ssl_access.log;
    error_log /var/log/nginx/newsletter.theitroad.local_ssl_error.log;
    #-------- END SSL config -------##
 
   root /var/www/localhost/htdocs;
   index         index.html index.htm index.php;
   server_name   newsletter.theitroad.local;
   # 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.theitroad.local ######

确保重新启动Nginx和php7:

# rc-service nginx restart
# rc-service php-fpm7 restart

步骤6:测试PHP 7

在您的根目录中创建一个php脚本文件,例如/var/www/localhost/htdocs

# vi test.php

添加以下内容:

<?php
  phpinfo();
?>

保存并关闭文件。
测试一下:

http://your-domain/test.php

或者

https://your-domain/test.php