Centos7学习笔记(十九)- lnmp组合的安装与集成

一、安装

学习环境下,nginx采用官方安装

sudo yum install yum-utils

vi /etc/yum.repos.d/nginx.repo

编辑以下内容进去:

[nginx-stable]

name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

其中,$releasever填写RHEL/Centos的系统版本

sudo yum install nginx

<br/>

安装PHP7.1版本

yum install epel-release -y
rpm -ivh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum install php71w php71w-fpm php71w-cli php71w-common php71w-devel php71w-gd php71w-pdo php71w-mysqlnd php71w-mbstring php71w-bcmath php71w-pecl-memcached php71w-pecl-mongodb php71w-pecl-redis php71w-opcache php71w-mcrypt php71w-xml php71w-embbedded

  

二、nginx与php的集成

主要涉及3个指令:fastcgi_pass、fastcgi_index、fastcgi_param。以下是示例来说明用法:

location ~ ..php(/.)*$ {                      《==========location匹配.php结尾的文件名并匹配其后续参数
             fastcgi_pass unix:/run/php-fpm/www.sock;        《==========fastcg_pass指令指示nginx把php交由谁来处理,这里是交给本地unix类型文件处理。也可以写作127.0.0.1:9000(IP地址加php-fpm运行的端口)            
             fastcgi_index   index.php;                    《==========php程序文件的索引文件是什么
             fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;      《===========这里写法固定。SCRIPT_FILENAME是固定的变量,$document_root$fastcgi_script_name表示把前面定义的root下的php文件名传递出去
             include fastcgi_params;                        《=================这里其实是用include包括了一个文件进来,是/etc/nginx下的factcgi_params文件。
     }

打开/etc/nginx/fastcgi_params文件,可以看到

fastcgi_param  QUERY_STRING       $query_string;</p><p style="white-space: normal;">fastcgi_param&nbsp; REQUEST_METHOD&nbsp; &nbsp; &nbsp;$request_method;

fastcgi_param  CONTENT_TYPE       $content_type;</p><p style="white-space: normal;">fastcgi_param&nbsp; CONTENT_LENGTH&nbsp; &nbsp; &nbsp;$content_length;

<br/>

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;</p><p style="white-space: normal;">fastcgi_param&nbsp; REQUEST_URI&nbsp; &nbsp; &nbsp; &nbsp; $request_uri;

fastcgi_param  DOCUMENT_URI       $document_uri;</p><p style="white-space: normal;">fastcgi_param&nbsp; DOCUMENT_ROOT&nbsp; &nbsp; &nbsp; $document_root;

fastcgi_param  SERVER_PROTOCOL    $server_protocol;</p><p style="white-space: normal;">fastcgi_param&nbsp; REQUEST_SCHEME&nbsp; &nbsp; &nbsp;$scheme;

fastcgi_param  HTTPS              $https if_not_empty;</p><p style="white-space: normal;"><br/></p><p style="white-space: normal;">fastcgi_param&nbsp; GATEWAY_INTERFACE&nbsp; CGI/1.1;</p><p style="white-space: normal;">fastcgi_param&nbsp; SERVER_SOFTWARE&nbsp; &nbsp; nginx/$nginx_version;

<br/>

fastcgi_param  REMOTE_ADDR        $remote_addr;</p><p style="white-space: normal;">fastcgi_param&nbsp; REMOTE_PORT&nbsp; &nbsp; &nbsp; &nbsp; $remote_port;

fastcgi_param  SERVER_ADDR        $server_addr;</p><p style="white-space: normal;">fastcgi_param&nbsp; SERVER_PORT&nbsp; &nbsp; &nbsp; &nbsp; $server_port;

fastcgi_param  SERVER_NAME        $server_name;

<br/>

# PHP only, required if PHP was built with --enable-force-cgi-redirect

fastcgi_param  REDIRECT_STATUS    200;

其实这里用fast_param指令定义了一堆的变量。

<br/>

三、附件上传大小限制

<br/>

nginx有一个<br/>

client_max_body_size=200m

<br/>

PHP有2个

php.ini文件中,

post_max_size =200M<br/>

upload_max_filesiz =200M<br/>

四、集成时的用户与权限

<br/>

1、ningx进程的用户

默认在/etc/nginx/nginx.conf文件中,

user nginx;

2、php-fpm进程的用户

默认在/etc/php-fpm.d/www.conf文件中

user = apache

group = apache

3、统一新建一个www的用户

sudo groupadd -r -g 49 www

sudo useradd -r -u 49 -g 49 -c "www user" -d /usr/share/www -s /sbin/nologin www

4、给/www/html目录授权

sudo chown -R www:www /www/html/

5、更改上述1、2中的用户后,重启nginx和php-fpm

sudo systemctl restart nginx

sudo systemctl restart php-fpm

<br/>

以上做的好处:

可以将nfs上的共享目录,rsync的nfs备份目录,都统一授权给www(提前在对应服务器上建立好www账户),解决权限、用户容易混乱的问题。

五、nginx、php和mysql分离

<br/>

1、需要在新mysql服务器上,授权一个用户远程登录。

grant all privileges on . to www@"%" identified by 'Langrenqingge123';

2021 10月 12