0
0

More than 1 year has passed since last update.

centos7でnginxと(laravel)php-fpmを動かすメモ

Posted at

作業記録

vagrantでcentOS7を立てて
nginxとlaravel(php-fpm)を動かす。

ゴールはlaravelのwelcome画面が出ればok

centOS7を立ち上げ

vagrantが既に入っている状態で
centOS7のイメージは

こちらを使用する。

$ vagrant box add bento/centos-7
$ mkdir test
$ cd test
$ vagrant init
//これでVagrantfileが作られる
$ ls
Vagrantfile
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が作られていますね
ここのlocalとの共有ディレクトリになっている

php-fpmをinstall

CentOS 7 の標準の yum リポジトリでは PHP 5.4 が提供されていますが、今回入れたいのはphp8系なので
remiリポジトリを利用する必要がある。

Remi で提供されているソフトウェアをインストールするためには EPEL のリポジトリも必須要件らしいので、
先に EPEL のリポジトリの追加を行った後に Remi のリポジトリを追加します。

■EPELリポジドリinstall
[vagrant@localhost ~]$ sudo yum -y install epel-release
■remi リポジトリinstall
[vagrant@localhost ~]$ sudo yum -y install http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
■php install
//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 install

この公式を参考にコマンドを叩く

[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をinstall必要があるのでinstall

[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をinstall

[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

ここまでで必要なものはinstallできたので
設定を行なっていきます。
nginxの設定とphp-fpmの設定を行います。

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

nginx設定

/etc/nginx/nginx.conf
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設定

/etc/php-fpm.d/www.conf
listen = 127.00.1:9000
listen.owner = nginx
listen.group = nginx

user = nginx
group = nginx
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0