LNMP環境構築(PHP 7.4.0)

18358 ワード

目次
  • 準備
  • wget
  • をインストール
  • net-tools
  • をインストール
  • vim
  • を取り付ける
  • 表示行番号
  • の構成
  • ファイアウォール
  • を閉じる
  • インストールNginx
  • インストール依存
  • コンパイルインストールNginx
  • 環境変数
  • の構成
  • Systemd管理
  • MySQLのインストール
  • インストール依存
  • boost
  • をダウンロード
  • コンパイルインストールMySQL
  • 環境変数
  • の構成
  • プロファイル
  • の変更
  • Systemd管理
  • MySQL
  • にログイン
  • PHP取付
  • インストール依存
  • PHP
  • のコンパイルインストール
  • Systemdサービス
  • の構成
  • 関連NginxとPHP
  • Redisのインストール
  • コンパイルインストール
  • 環境変数
  • の構成
  • 構成バックグラウンド運転
  • Systemdサービス
  • の構成
  • 参考資料
  • PHP開発者として、PHP開発環境を構築する方法を理解しなければならない.現在主流のPHP開発環境の組み合わせはLAMPとLNMPである.本文ではCentOS 7.*でどのように構築するかを紹介する.LNMP開発環境を構築する.
    各バージョンの説明:
    CentOS7: 7.7
    Nginx: 1.16.1
    MySQL:5.7.28
    PHP:7.4.0
    準備作業
    wgetのインストールwgetは、HTTP、HTTPS、FTPの3つの最も一般的なTCP/IPプロトコルを介してファイルを自動的にダウンロードするフリーツールであり、HTTPエージェントを使用することができます.
    sudo yum install wget

    net-toolsのインストール
    CentOS 7のインストールを最小化ifconfigコマンドが使用できない場合は、net-toolsをインストールする必要があります.インストールされているCentOS 6バージョンであればインストールする必要はありません.
    sudo yum -y install net-tools

    vimのインストール
    sudo yum -y install vim

    表示行番号の設定
    vim ~/.vimrc #   .vimrc    
    set nu #   set nu      

    ファイアウォールを閉じる
    systemctl stop firewalld.service  #      
    systemctl disable firewalld.service  #          
           IP      

    インストールNginx
    インストール依存
    (1)インストールnginxは、まず公式サイトからダウンロードしたソースコードをコンパイルし、コンパイルはgcc環境に依存し、gcc環境がなければgcc-c++をインストールする必要がある.
    yum -y install gcc gcc-c++

    (2)PCREはPerlライブラリであり、中国語の「Perl互換の正規表現ライブラリ」である.Nginxをインストールするのは、NginxがURI書き換え機能を備えたrewriteモジュールをサポートするためであり、pcreライブラリをインストールしないと、Nginxはrewriteモジュール機能を使用できず、NginxのRewriteモジュール機能はほとんど企業応用に必須である.
    yum -y install pcre pcre-devel

    (3)zlibライブラリは圧縮と解凍の方法を多く提供し,nginxはzlibを用いてhttpパッケージの内容をgzipするため,Centosにzlibライブラリをインストールする必要がある.
    yum -y install zlib zlib-devel

    (4)OpenSSLは強力なセキュリティソケット層暗号ライブラリであり、主な暗号アルゴリズム、一般的な鍵と証明書パッケージ管理機能、SSLプロトコルを網羅し、テストまたはその他の目的で豊富なアプリケーションを提供しています.nginxはhttpプロトコルだけでなく、http(すなわち、sslプロトコル上でhttpを転送する)もサポートするので、OpenSSLライブラリをインストールする必要があります.
    yum -y install openssl openssl-devel

    説明:yumインストール方式でインストールされているpcreバージョンは低いが、ほとんど使用に影響しないが、インストール公式サイトの最新安定版opensslを手動でコンパイルしたほうがいい.
    ベース依存パッケージの確認
    上記の依存インストールが完了したら、次のコマンドで各依存インストールが成功したかどうかを確認できます.
    rpm -qa pcre pcre-devel
    rpm -qa zlib zlib-devel
    rpm -qa pcre pcre-devel

    コンパイルインストールNginx
    #             /usr/local/src   ,      
    cd /usr/local/src  #        
    wget http://nginx.org/download/nginx-1.16.1.tar.gz   #  nginx   
    useradd nginx -s /sbin/nologin -M   #  nginx      nginx  
    tar -zxvf nginx-1.16.1.tar.gz  #  nginx   
    
    cd nginx-1.16.1
    
    #  ,        openssl  ,make    ,      nginx      conf  
    #/usr/local/src/nginx-1.16.1/auto/lib/openssl/conf,         
    
    #   
    ./configure \
    --user=nginx \
    --group=nginx \
    --prefix=/usr/local/nginx-1.16.1 \
    --with-http_v2_module \
    --with-http_ssl_module \
    --with-http_stub_status_module
    
    make && make install #       
    
    cd /usr/local
    ln -s nginx-1.16.1 nginx  #  nginx    

    インストールの説明
    --prefix=PATH    #      
    --user=USER      #      
    --group=GROUP    #       
    --with-http_v2_module  # HTTP2
    --with-http_stub_status_module   #      
    --with-http_ssl_module  #  ssl  

    環境変数の設定
    vim /etc/profile
    export PATH=/usr/local/nginx/sbin:$PATH
    source /etc/profile

    Systemd管理/usr/lib/systemd/system/nginx.serviceファイルの新規作成と編集
    vim /usr/lib/systemd/system/nginx.service

    以下の内容を追加します(ここでの構成は、自分がNginxをインストールするパスに基づいて構成され、Nginxは/usr/localディレクトリの下にインストールされています).
    [Unit]
    Description=The nginx HTTP and reverse proxy server
    After=network.target remote-fs.target nss-lookup.target
    
    [Service]
    Type=forking
    ExecStartPre=/usr/local/nginx/sbin/nginx -t
    ExecStart=/usr/local/nginx/sbin/nginx
    ExecReload=/usr/local/nginx/sbin/nginx -s reload
    ExecStop=/usr/local/nginx/sbin/nginx -s stop
    PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target

    yumでインストールするnginxで、デフォルトのnginx.サービスは以下のように構成されており、参考にすることができます.
    # /usr/lib/systemd/system/nginx.service
    [Unit]
    Description=The nginx HTTP and reverse proxy server
    After=network.target remote-fs.target nss-lookup.target
    
    [Service]
    Type=forking
    PIDFile=/run/nginx.pid
    # Nginx will fail to start if /run/nginx.pid already exists but has the wrong
    # SELinux context. This might happen when running `nginx -t` from the cmdline.
    # https://bugzilla.redhat.com/show_bug.cgi?id=1268621
    ExecStartPre=/usr/bin/rm -f /run/nginx.pid
    ExecStartPre=/usr/sbin/nginx -t
    ExecStart=/usr/sbin/nginx
    ExecReload=/bin/kill -s HUP $MAINPID
    KillSignal=SIGQUIT
    TimeoutStopSec=5
    KillMode=process
    PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target

    重荷daemon
    次のコマンドを実行してsystemdを再ロードし、新しいユニットまたは変動したユニットをスキャンすればよい.
    systemctl daemon-reload

    POSTの設定
    systemctl enable nginx.service #       
    systemctl disable nginx.service #         

    Nginxサービス管理共通コマンド
    systemctl status nginx.service #   Nginx  
    systemctl start nginx.service #   Nginx
    systemctl stop nginx.service #   Nginx
    systemctl reload nginx.service #     
    systemctl restart nginx.service  #   Nginx(   stop&start)

    サービス起動チェック
    このコマンドを使用して、80ポートが誰に使用されているかを問い合わせることができます.
    lsof -i :80

    このコマンドが認識できない場合は、lsofをインストールする必要があります.
    sudo yum -y install lsof

    MySQLのインストール
    インストール依存
    (1)cmakeは新版MySQLのコンパイルツールで、必ずインストールしなければならない
    sudo yum -y install ncurses-devel perl perl-devel autoconf

    yum方式でcmakeをインストールしないで、デフォルトのcmakeバージョンが低いため、これは後でPHPをインストールする時バージョンが低すぎてインストールできない問題が発生することができて、MySQLをインストールするのはcmakeを必要とするため、ここで直接最新の安定版cmakeをインストールします
    cd /usr/local/src
    yum remove cmake
    wget https://github.com/Kitware/CMake/releases/download/v3.16.1/cmake-3.16.1.tar.gz
    tar -zxvf cmake-3.16.1.tar.gz
    
    cd cmake-3.16.1
    ./configure \
    --prefix=/usr/local/cmake
    make && make install
    
    cd /usr/local
    ln -s /usr/local/cmake/bin/cmake /usr/bin/cmake
    # ln -s /usr/local/cmake/share/cmake-3.16.1 /usr/share/cmake

    ダウンロード
    インストールされているMySQL 5の場合7以上のバージョンでは、インストールをコンパイルする前にboostをインストールする必要があります.高バージョンmysqlはbootsライブラリのインストールが必要であるため、正常に動作します.そうでないとCMake Error at cmake/boost.cmake:81エラーが発生します/usr/local/srcディレクトリに切り替え、このディレクトリの下でboost MySQL 5をダウンロードします.7.28 boostのバージョンは1.59であることを要求し、より高いバージョンはMySQL 5を適用しない.7.28
    wget http://www.sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz

    コンパイルインストールMySQL
    #   MySQL  
    useradd -s /sbin/nologin -M mysql
    
    #    /usr/src  
    cd /usr/local/src
    
    #   MySQL
    wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.28.tar.gz
    
    #   MySQL
    tar -zxvf mysql-5.7.28.tar.gz
    
    #  boost,   mysql/boost
    tar -zxvf boost_1_59_0.tar.gz
    mv boost_1_59_0 mysql-5.7.28/boost
    
    #   MySQL  
    cd mysql-5.7.28
    
    #    
    cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.7.28 \
    -DWITH_BOOST=boost \
    -DWITH_SYSTEMD=1 \
    -DWITH_SSL=system \
    -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \
    -DMYSQL_DATADIR=/var/lib/mysql/data \
    -DDEFAULT_CHARSET=utf8mb4 \
    -DDEFAULT_COLLATION=utf8mb4_general_ci \
    -DWITH_EXTRA_CHARSETS=all \
    -DWITH_MYISAM_STORAGE_ENGINE=1 \
    -DWITH_INNOBASE_STORAGE_ENGINE=1 \
    -DWITH_MEMORY_STORAGE_ENGINE=1 \
    -DWITH_READLINE=1 \
    -DWITH_INNODB_MEMCACHED=1 \
    -DWITH_DEBUG=OFF \
    -DWITH_ZLIB=bundled \
    -DENABLED_LOCAL_INFILE=1 \
    -DENABLED_PROFILING=ON \
    -DMYSQL_MAINTAINER_MODE=OFF \
    -DMYSQL_TCP_PORT=3306
    
    #   &  
    make && make install
    
    #      
    cd /usr/local
    ln -s mysql-5.7.28 mysql

    環境変数の設定
    #        
    vim /etc/profile
    export PATH=/usr/local/mysql/bin:$PATH
    source /etc/profile

    プロファイルの変更
  • /var/libディレクトリの下にmysqlフォルダ
    mkdir -p /var/lib/{mysql,mysql/data}
    touch /var/lib/mysql/mysqld.pid
    chown mysql.mysql -R /var/lib/mysql/
  • を作成する.
  • 修正/etc/my.cnfファイル
    #   /etc/my.cnf  ,        
    [mysqld]
    character-set-server=utf8mb4
    collation-server=utf8mb4_general_ci
    datadir=/var/lib/mysql/data
    socket=/var/lib/mysql/mysql.sock
    
    [mysqld_safe]
    log-error=/var/log/mysql/mysqld.log
    pid-file=/var/lib/mysql/mysqld.pid
    
    [client]
    default-character-set=utf8mb4
  • mysqld.logmysqld.pidのファイルを作成し、ファイル権限
    #   mysqld.log   mysqld.pid  
    mkdir /var/log/mysql
    touch /var/log/mysql/mysqld.log
    chown mysql.mysql -R /var/log/mysql/
  • を変更します.
  • データベース
    #       , –initialize              ,–initialize-insecure        
    mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/var/lib/mysql/data
  • の初期化
    Systemd管理/usr/lib/systemd/system/mysqld.serviceファイルを作成し、次のように編集します.
    vim /usr/lib/systemd/system/mysqld.service
    [Unit]
    Description=MySQL Server
    Documentation=man:mysqld(8)
    Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
    After=network.target
    After=syslog.target
    
    [Install]
    WantedBy=multi-user.target
    
    [Service]
    User=mysql
    Group=mysql
    
    Type=forking
    
    PIDFile=/var/lib/mysql/mysqld.pid
    
    # Disable service start and stop timeout logic of systemd for mysqld service.
    TimeoutSec=0
    
    # Execute pre and post scripts as root
    PermissionsStartOnly=true
    
    # Needed to create system tables
    ExecStartPre=/usr/local/mysql/bin/mysqld_pre_systemd
    
    # Start main service
    ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/var/lib/mysql/mysqld.pid $MYSQLD_OPTS
    
    # Use this to switch malloc implementation
    EnvironmentFile=/etc/my.cnf
    
    # Sets open_files_limit
    LimitNOFILE = 5000
    
    Restart=on-failure
    
    RestartPreventExitStatus=1
    
    PrivateTmp=false

    重荷daemon
    次のコマンドを実行してsystemdを再ロードし、新しいユニットまたは変動したユニットをスキャンすればよい.
    systemctl daemon-reload

    MySQLの起動
    systemctl start mysqld.service #   MySQL
    systemctl stop mysqld.service #   MySQL
    systemctl status mysqld.service #   MySQL  

    ブートストラップ
    systemctl enable mysqld.service #       
    systemctl disable mysqld.service #       

    MySQLへのログイン
    mysql -u root -p #          ,    
    set password for root@localhost = password('root');  #    

    PHPを取り付ける
    インストール依存
    sudo yum -y install gcc gcc-c++ zip unzip libxml2 libxml2-devel curl-devel autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel gd-devel bzip2 bzip2-devel libsqlite3x libsqlite3x-devel oniguruma oniguruma-devel

    libzipのアップグレード
    yum remove libzip
    cd /usr/local/src
    wget https://libzip.org/download/libzip-1.5.2.tar.gz
    tar -zxvf libzip-1.5.2.tar.gz
    cd libzip-1.5.2
    mkdir build
    cmake .
    make && make install
    
    echo '/usr/local/lib64
    /usr/local/lib
    /usr/lib
    /usr/lib64'>>/etc/ld.so.conf
    ldconfig -v

    コンパイルインストールPHP
    cd /usr/local/src
    wget http://hk1.php.net/get/php-7.4.0.tar.gz/from/this/mirror -O php-7.4.0.tar.gz
    
    tar -zxvf php-7.4.0.tar.gz
    cd  php-7.4.0
    ./configure \
    --prefix=/usr/local/php-7.4.0 \
    --enable-fpm \
    --with-fpm-user=nginx \
    --with-fpm-group=nginx \
    --with-zlib \
    --enable-mysqlnd \
    --enable-bcmath \
    --enable-gd \
    --with-pdo-mysql=mysqlnd \
    --with-mysqli=mysqlnd \
    --with-mysql-sock=/var/lib/mysql/mysql.sock \
    --with-jpeg \
    --with-freetype \
    --with-iconv \
    --with-openssl \
    --with-curl \
    --enable-mbstring \
    --enable-static \
    --enable-sockets \
    --enable-xml
    
    make && make install

    コンパイルパラメータの詳細
    ./configure \
    --prefix=/usr/local/php-7.4.0 \ #       
    --enable-fpm \             #     PHP-FPM    , FactCGI    PHP  。
    --with-fpm-user=nginx \    #   PHP-FPM        www,     Nginx      。
    --with-fpm-group=nginx \   #   PHP-FPM        www,     Nginx       。
    --with-zlib \ #   zlib    ,  http    
    --enable-mysqlnd \
    --with-pdo-mysql=mysqlnd \
    --with-mysqli=mysqlnd \
    --with-mysql-sock=/var/lib/mysql/mysql.sock \
    --with-gd \        #   gd    
    --with-png-dir \
    --with-jpeg-dir \
    --with-freetype-dir \
    --with-openssl \   #   openssl,       
    --with-curl \      #   curl        
    --enable-mbstring \ #    ,      
    --enable-static \ #        
    --enable-zip \ #    zip   
    --enable-sockets \ #    sockets   
    --enable-xml

    コンフィギュレーション
    cd /usr/local
    
    ln -s php-7.4.0 php
    cp  /usr/local/src/php-7.4.0/php.ini-development /usr/local/php-7.4.0/lib/php.ini
     
     vim /usr/local/php/lib/php.ini
     date.timezone = PRC  (   954 )
     expose_php = Off  #  PHP     http  (  369 )
     
     display_errors = Off(       off,        On,    )
       :   dispaly_errors off ,   php-fpm.conf           error_log = log/php-fpm.log
     
    cd /usr/local/php 
    cp etc/php-fpm.conf.default etc/php-fpm.conf
    
    cd /usr/local/php/etc/php-fpm.d/
    cp www.conf.default www.conf
    cd /usr/local/php
    sbin/php-fpm
    ps -e | grep php-fpm
    
         PHP    --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd   ,           socket    ,      php.ini     : pdo_mysql.default_socket=/usr/local/mysql/tmp/mysql.sock
    
          PHP   ,  mysql.socket   :
    --with-mysql-sock=/usr/local/mysql/tmp/mysql.sock

    PHP-FPMの管理
    vim /usr/local/php/etc/php-fpm.conf
    pid = run/php-fpm.pid
    error_log = log/php-fpm.log #24    php.ini  display_errors = Off   
              
           ,         
      : kill -INT `cat /usr/local/php/var/run/php-fpm.pid`
        : kill -QUIT `cat /usr/local/php/var/run/php-fpm.pid`
      :kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`
          :kill -USR1 `cat /usr/local/php/var/run/php-fpm.pid`

    環境変数の設定
    vim /etc/profile
    export PATH=/usr/local/php/bin:$PATH
    source /etc/profile

    Systemdサービスの構成
    実はphp-fpm.サービスファイルphpは、指定した場所にコピーして有効にするだけで構成できます.
    cp /usr/local/src/php-7.4.0/sapi/fpm/php-fpm.service /usr/lib/systemd/system/

    ファイルを編集
    vim /usr/lib/systemd/system/php-fpm.service

    php-fpm.サービスファイルの内容は次のとおりです.
    # It's not recommended to modify this file in-place, because it
    # will be overwritten during upgrades.  If you want to customize,
    # the best way is to use the "systemctl edit" command.
    
    [Unit]
    Description=The PHP FastCGI Process Manager
    After=network.target
    
    [Service]
    Type=simple
    PIDFile=/usr/local/php/var/run/php-fpm.pid
    ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf
    ExecReload=/bin/kill -USR2 $MAINPID
    PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target

    重荷daemon
    次のコマンドを実行してsystemdを再ロードし、新しいユニットまたは変動したユニットをスキャンすればよい.
    systemctl daemon-reload

    ブートストラップ
    systemctl enable php-fpm.service
    systemctl disable php-fpm.service

    php-fpmの起動
    systemctl start php-fpm.service

    関連NginxとPHP
    nginx.conf構成
    #user  nobody;
    #          ,      ,     ,     CPU
    #     CPU  *   
    worker_processes  1; 
    
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #pid        logs/nginx.pid;
    
    
    events {
    #     Nginx        
    #       
        multi_accept on; #                  。
        use epoll;  #  epoll    ,  epoll               
        worker_connections  10240; #              10240   
    }
    
    
    http { #     http       
        include       mime.types;
        default_type  application/octet-stream;
        
        #  Nginx     
        server_tokens off;
        
        #  tcp_nodelay  ,  I/O  
        tcp_nodelay on;
    
        #                  。      15,     ,      
        client_header_timeout 15;
    
        #                
        client_body_timeout 15;
    
        #             
        send_timeout 25;
    
        #         
        client_max_body_size 8m;
        
        #    
        gzip on;
        gzip_min_length 1k;
        gzip_buffers 4 16k;
        gzip_http_version 1.0;
        gzip_comp_level 2;
        gzip_types text/css text/xml application/javascript;
        gzip_vary on;
        #include extra/gzip.config;
    
        #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  logs/access.log  main;
    
        sendfile        on;
        #tcp_nopush     on;
    
        #keepalive_timeout  0;
        keepalive_timeout  65;
    
        include extra/*.conf;
    }

    Redisのインストール
    コンパイルインストール
    #       
    tar -zxf redis-5.0.6.tar.gz
    
    #        
    cd redis-5.0.6
    
    #     
    mkdir /usr/local/redis-5.0.6
    make PREFIX=/usr/local/redis-5.0.6 install
    
    mkdir /usr/local/redis-5.0.6/etc
    cp redis.conf /usr/local/redis-5.0.6/etc/
    
    #      
    cd /usr/local
    ln -sf redis-5.0.6 redis

    環境変数の設定
    vim /etc/profile
    export PATH=/usr/local/redis/bin:$PATH
    source /etc/profile #        

    バックグラウンド実行の構成redis以降のプロセスの形式で実行させる
    vim /usr/local/redis/etc/redis.conf
    
    # daeonize no(  )
    #    ->
    daemonize yes

    Systemdサービスの構成/etc/systemd/system/redis.serviceファイルを追加し、次の内容を追加します.
    [Unit]
    Description=Redis
    After=network.target
     
    [Service]
    Type=forking
    PIDFile=/var/run/redis_6379.pid
    ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
    ExecStop=/usr/local/redis/bin/redis-cli shutdown
    PrivateTmp=true
     
    [Install]
    WantedBy=multi-user.target

    重荷daemon
    次のコマンドを実行してsystemdを再ロードし、新しいユニットまたは変動したユニットをスキャンすればよい.
    systemctl daemon-reload

    ブートストラップ
    systemctl enable redis.service

    redisサービスの開始
    systemctl start redis.service

    参考資料
    centos 7ソースコードコンパイルインストールmysql 5.7
    mysql linux 7でのsystemdの関連構成
    Managing MySQL Server with systemd
    centos 7 7.3 phpコンパイルインストール
    centos 7でphp 7をコンパイルインストール.3
    cmakeインストール構成および入門ガイド
    コンパイルCMake 3.15とgcc 5.3.0
    CentOS 7はOpenSSLを1.1.1にアップグレード