在CentOS 7上使命Nginx和(Laravel)PHP-FPM运行的笔记

任务记录

使用Vagrant创建一个CentOS7虚拟机,并在上面运行Nginx和Laravel(PHP-FPM)。

只要 Laravel 的欢迎页面能够显示出来,就可以达到目标了。

启动 CentOS 7

在安装了Vagrant的情况下,CentOS7的镜像是已经准备好的。

 

请使用这个。

$ vagrant box add bento/centos-7
$ mkdir test
$ cd test
$ vagrant init
//これでVagrantfileが作られる
$ ls
Vagrantfile
config.vm.box = "bento/centos-7" //box名を設定
config.vm.network "private_network", ip: "192.168.56.54" //ipを指定
//Ranges: 192.168.56.0/21この範囲内で指定しないといけないっぽい
config.vm.synced_folder "./", "/vagrant_data" //マウント?localのファイルを共有する
$ ping 192.168.56.54

如果能通过,就站起来。

通过SSH进行登录。

$ vagrant ssh
[vagrant@localhost ~]$ ls /
bin   dev  home  lib64  mnt  proc  run   srv  tmp  vagrant       var
boot  etc  lib   media  opt  root  sbin  sys  usr  vagrant_data

这里创建了一个名为vagrant_data的文件夹
它作为与此本地文件夹的共享目录。

安装php-fpm

CentOS 7的官方yum软件仓库只提供PHP 5.4版本,而我想要安装的是PHP 8版本,所以需要使用remi软件仓库。

似乎为了安装Remi提供的软件,必须先添加EPEL仓库,然后再添加Remi仓库。

■安装EPEL存储库
[vagrant@localhost ~]$ sudo yum -y install epel-release
■ 安装 remi 代码库
[vagrant@localhost ~]$ sudo yum -y install http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
■安装php
//remiリポジトリからinstallできるphpを確認
[vagrant@localhost ~]$ ls /etc/yum.repos.d | grep php
remi-php54.repo
remi-php70.repo
remi-php71.repo
remi-php72.repo
remi-php73.repo
remi-php74.repo
remi-php80.repo
remi-php81.repo
remi-php82.repo

//phpを削除するには
[vagrant@localhost ~]$ sudo yum remove php-*

//phpとphp-fpmをinstall
[vagrant@localhost ~]$ sudo yum -y install --enablerepo=remi,remi-php81 php php-fpm php-xml

[vagrant@localhost ~]$ php -v
PHP 8.1.12 (cli) (built: Oct 25 2022 17:30:00) (NTS gcc x86_64)
Copyright (c) The PHP Group
Zend Engine v4.1.12, Copyright (c) Zend Technologies

//php-fpm自動起動
[vagrant@localhost ~]$ sudo systemctl enable php-fpm

//php-fpm起動・停止・再起動
[vagrant@localhost ~]$ sudo systemctl start php-fpm
[vagrant@localhost ~]$ sudo systemctl stop php-fpm
[vagrant@localhost ~]$ sudo systemctl restart php-fpm

■安装 composer

 

根据这个公式执行命令

[vagrant@localhost ~]$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" 
[vagrant@localhost ~]$ php -r "if (hash_file('sha384', 'composer-setup.php') === '55ce33d7678c5a611085589f1f3ddf8b3c52d662cd01d4ba75c0ee0459970c2200a51f492d557530c71c15d8dba01eae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup. php'); } echo PHP_EOL;"
[vagrant@localhost ~]$ php composer-setup.php
[vagrant@localhost ~]$ php -r "unlink('composer-setup.php');"
[vagrant@localhost ~]$ sudo mv composer.phar /usr/local/bin/composer
■启动Laravel

 

需要安装git才能进行安装。

[vagrant@localhost ~]$ sudo yum install git -y
//共有フォルダに移動する
[vagrant@localhost ~]$ cd /vagrant_data
[vagrant@localhost vagrant_data]$ composer create-project laravel/laravel:^8.0 example-app
[vagrant@localhost vagrant_data]$ ls
Vagrantfile  example-app

安装Nginx

[vagrant@localhost ~]$ sudo yum install nginx -y
[vagrant@localhost ~]$ nginx -v
nginx version: nginx/1.20.1
//自動起動
[vagrant@localhost ~]$ sudo systemctl enable nginx

//起動・停止・再起動
[vagrant@localhost ~]$ sudo systemctl start nginx
[vagrant@localhost ~]$ sudo systemctl stop nginx
[vagrant@localhost ~]$ sudo systemctl restart nginx

因为到这里,已经安装了所需的内容,所以会继续进行设置。
将进行nginx和php-fpm的配置设置。

//vimを使うのでinstall
[vagrant@localhost ~]$ sudo yum -y install vim-enhanced

Nginx配置文件

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    #serverコンテキストをconf.dないに置く必要性がわからないのでnginx.confに直書きする
    #include /etc/nginx/conf.d/*.conf;

    server {
            listen 80;
            root /vagrant_data/example-app/public;
            index index.php;
            client_max_body_size 128M;

            fastcgi_buffers 8 16k;
            fastcgi_buffer_size 32k;
            proxy_buffer_size 128k;
            proxy_buffers 4 256k;
            proxy_busy_buffers_size 256k;

            location / {
                    try_files $uri $uri/ /index.php$is_args$args;
            }

            location ~ \.php$ {
                    fastcgi_split_path_info ^(.+\.php)(/.+)$;
                    fastcgi_pass   127.0.0.1:9000;
                    fastcgi_index  index.php;

                    include        fastcgi_params;
                    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
                    fastcgi_param  PATH_INFO $fastcgi_path_info;
            }
    }
}

php-fpm设置

listen = 127.00.1:9000
listen.owner = nginx
listen.group = nginx

user = nginx
group = nginx
广告
将在 10 秒后关闭
bannerAds