Vagrant 搭建个人开发环境(3)在 ubuntu 中安装php7.4

apt-get 安装

为了避免折腾。还是采用apt-get 方式.

#先安装一下这个命令 add-apt-repository
sudo apt-get install software-properties-common

#添加第三方源:
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update

#安装php:
sudo apt-get install php7.4 php7.4-fpm php7.4-mysql php7.4-gd php7.4-mbstring
# service php7.4-fpm start
#验证
php -v
# PHP 7.4.4 (cli) (built: Mar 20 2020 13:47:45) ( NTS )
# Copyright (c) The PHP Group
# Zend Engine v3.4.0, Copyright (c) Zend Technologies
#    with Zend OPcache v7.4.4, Copyright (c), by Zend Technologies
phpize -v
#Configuring for:
#PHP Api Version:         20190902
#Zend Module Api No:      20190902
#Zend Extension Api No:   320190902

配置

获得php.ini路径.

php -i|grep "Loaded Configuration File"
#Loaded Configuration File => /etc/php/7.4/cli/php.ini
#有两个可调用的配置文件, fpm 配置文件./etc/php/7.4/cli/php.ini

修改php.ini文件

在/etc/php/7.4/fpm/pool.d/www.conf

修改listen = 9000

php-fpm常用管理命令:

开启php-fpm: sudo systemctl start php7.4-fpm
关闭php-fpm: sudo systemctl stop php7.4-fpm
重启php-fpm: sudo systemctl restart php7.4-fpm
编辑php-fpm配置文件: vi /etc/php/7.4/fpm/php-fpm.ini

编辑nginx添加主机并设置fastcgi 端口

worker_processes 1; #nginx worker 数量
error_log logs/error.log; #指定错误日志文件路径
events {
    worker_connections 1024;
}

http {
    lua_code_cache off;
    index index.php index.html;

    server {
        #监听端口,若你的6699端口已经被占用,则需要修改
        listen 6699;
        location / {
            default_type text/html;
            #include /home/vagrant/openresty-test/conf/hello.lua;
            #lua_shared_dict limit 10m;
            #init_by_lua_file  /root/waf/init.lua;

            # content_by_lua_block {
            #     ngx.say("123123")
            # }

            content_by_lua_file /home/vagrant/openresty-test/conf/hello.lua;

        }
    }

    server {
        listen 80;
        root /home/vagrant/php_works/test/;

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

如果提示

nginx: [emerg] open() "./openresty-test/conf/fastcgi-php.conf" failed (2: No such file or directory) in ./openresty-test/conf/nginx.conf:37

请在nginx/conf 目录下找到 fastcgi.conf 并改名,或者复制到你的conf目录下面

设置vagrant 同步目录

config.vm.synced_folder "D:/wwwroot", "/home/vagrant/php_works"

在本地d:/wwwroot/ 创建test目录

vagrant reload

执行开启openresty

[email protected]:~$ sudo openresty -p ./openresty-test/
nginx: [alert] lua_code_cache is off; this will hurt performance in ./openresty-test/conf/nginx.conf:8
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

这里已经开启了。由systemctl 执行的。我们停止掉

sudo systemctl stop oepnresty

再次执行 sudo openresty -p ./openresty-test/

你们目录可能和我的不同。或者你们才有默认的配置文件都可以忽略这步.

启动成功.

再启动php-fpm

sudo systemctl start php7.4-fpm

编写测试index.php

<?php
    $name = "john";
    echo "Hello {$name}";

访问 192.168.100.100

可以看到我们的网站已经可以渲染php了

参考链接

树莓派4B安装PHP7.3 + Nginx + MySQL 教程

添加新评论