LNMP 環境安裝

善良的死神發表於2020-01-15

簡介

LNMP的安裝,使用 root 使用者執行,伺服器 CentOS 7.2 64 位

Nginx

新增 Nginx 的源

sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

安裝Nginx

 通過 yum search nginx 看看是否已經新增源成功。如果成功則執行下列命令安裝 Nginx。
sudo yum install -y nginx

啟動Nginx並設定開機自動執行

#啟動nginx
sudo systemctl start nginx.service
#設定開機自動執行
sudo systemctl enable nginx.service
#重啟
sudo systemctl restart nginx
#停止
sudo systemctl stop nginx

瀏覽器檢視效果

7c0vOYHMe5.png!large

end

參考 CentOS7中使用yum安裝Nginx的方法

PHP7.2

使用 yum 安裝

更換rpm源

直接使用 `yum -y install php` 安裝的是 5.4 版本的,所以更新一下源
```
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm   
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
```

安裝需要的擴充套件

7.2版本名為72w,安裝 php72w 和各種擴充,選自己需要的即可

yum -y install php72w php72w-cli php72w-common php72w-devel php72w-embedded php72w-fpm php72w-gd php72w-mbstring php72w-mysqlnd php72w-opcache php72w-pdo php72w-xml

php -v 檢視資訊

u23OyABfPP.png!large

php-fpm 開機自啟動

#開機自啟動
systemctl enable php-fpm
#啟動php-fpm
systemctl start php-fpm
#重啟
systemctl restart php-fpm
#停止
systemctl stop php-fpm

PHP 配置資訊

    php.ini =>  /etc/php.ini
    php-fpm.conf  =>  /etc/php-fpm.conf
    www.conf  =>  /etc/php-fpm.d/www.conf

end

參考 centos安裝php7.2

Nginx 關聯 PHP

檢視 Nginx 的配置檔案

kogqmcIaQq.png!large

更改 nginx.conf 使其支援 PHP 檔案請求

我的 nginx.conf 實際載入的是在 include /etc/nginx/conf.d/*.conf

server {
    listen       80;
    server_name  localhost;
    index index.html index.php;
    #web訪問的路徑
    root /usr/share/nginx/html;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;
    location / {
        index  index.html index.htm index.php;
        try_files $uri $uri/ /index.php?$query_string;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #配置FastCGI資訊
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
        deny  all;
    }
}

可根據具體的情況進行修改,最主要的就是配置 location ~ \.php$ 的引數資訊

配置 Nginx 和 PHP 執行使用者

這裡使用 www-data 這個使用者執行

  1. 建立使用者組和使用者

    #建立使用者組
    groupadd www-data
    #建立使用者
    useradd -g www-data -s /sbin/nologin www-data
  2. 配置 Nginx

    修改 nginx.conf 的執行使用者,之後重啟 Nginx systemctl restart nginx

    zDluYHtA4F.png!large
  3. 配置 PHP

    php-fpm 的執行使用者是在 www.conf 檔案裡面修改,我使用 yum 安裝的 PHP ,所以 www.conf 是在 /etc/php-fpm.d 這個目錄下,如果使用原始碼編譯安裝,可能在 /usr/local/php 下的某個檔案的 php-fpm.d 這個目錄下。

    更改 www.conf 檔案的 usergroup 這兩個屬性

    GFRsSiKOPA.png!large
  4. 重啟 php-fpm systemctl restart php-fpm

編寫 PHP 檔案,檢視結果

  1. 建立 PHP 檔案

    在 nginx.conf 檔案中,web 訪問的目錄是 /usr/share/nginx/html,在此目錄下建立 index.php

    oJLHobfK28.png!large
  2. 訪問 index.php 檔案

    skeAlgfUUT.png!large
  3. 如果出現 nginx File not found 可參考 nginx File not found 錯誤

end

MySql5.7

建立執行 MySql 的使用者和所屬組

groupadd mysql
useradd -r -g mysql mysql

安裝 MySql5.7 可檢視 此教程

建立只讀使用者並開啟遠端連線

  1. 建立測試使用者,並允許遠端連線

    create user test@'%' identified by '123456'

    此命令建立一個使用者名稱為 test,密碼為 123456 的使用者,% 表示此使用者可以遠端連線。

  2. 授權此使用者對所有庫和表僅有讀許可權
    可根據實際情況替換 SELECT ,『select , insert, update, create, drop 』or 『all privileges』

    GRANT SELECT ON *.* TO 'test'@'%';
  3. 檢視此使用者許可權

    SHOW GRANTS for test@'%';   
  4. 撤銷此使用者的讀許可權

    revoke SELECT ON *.* TO 'test'@'%';
  5. 刪除使用者

    drop user test@'%';
  6. 使用 MySql 工具連線

JsrNTsw0cZ.png!large

Redis

安裝 Redis 服務

  1. yum install redis
  2. systemctl start redis
  3. systemctl status redis 檢視狀態
  4. systemctl stop redis 停止服務
  5. systemctl restart redis 重啟服務
  6. systemctl enable redis 開機啟動

安裝 PHP 的 redis 擴充套件

  1. wget https://github.com/phpredis/phpredis/archive/5.1.1.tar.gz
  2. tar -zxvf redis.tar.gz
  3. cd phpredis
  4. phpize
  5. which php-config
  6. ./configure --with-php-config=/usr/bin/php-config
  7. yum install gcc
  8. php --ini 檢視引入方式
  9. redis.ini or php.ini => redis.so

Composer 安裝

安裝 Composer 並配置國內映象

  1. 安裝 composer

    php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"
    php composer-setup.php
  2. 移動 composer.phar,這樣 composer 就可以進行全域性呼叫。

    mv composer.phar /usr/local/bin/composer
  3. 配置國內映象,使用的是 阿里雲Composer全量映象

    composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章