NMP
是一套技术的组合,L=Linux、N=Nginx、M=MySQL、P=PHP
PHP-FPM(FastCGI Process Manager:FastCGI进程管理器)是一个PHPFastCGI管理器
1使用官方仓库安装Nginx
2修改nginx用户
root@nginx ~]# groupadd www -g 666
[root@nginx ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M
#修改nginx配置文件
[root@nginx ~]# sed -i '/^user/c user www;' /etc/nginx/nginx.conf
使用第三方扩展源安装php7.1
安装PHP服务
(1) 配置PHP仓库
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
清除之前的残留:
yum remove php-mysql-5.4 php php-fpm php-common
#配置第三方源 (不配置 直接 yum 也可以)
[root@nginx ~]# vim /etc/yum.repos.d/php.repo
[php-webtatic]
name = PHP Repository
baseurl = http://us-east.repo.webtatic.com/yum/el7/x86_64/
gpgcheck = 0
(2): 安装PHP服务
yum -y install php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd php71w-opcache php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb --nogpgcheck
注意: 如果网络慢可以通过本地rpm方式安装(需要上传rpm包)
查看 安装的 php 包
rpm -qa |grep php
配置php-fpm用户与Nginx的运行用户保持一致
(3): 修改php的启动用户为www
sed -i '/^user/c user = www' /etc/php-fpm.d/www.conf
sed -i '/^group/c group = www' /etc/php-fpm.d/www.conf
(4)启动php-fpm加入开机自启
systemctl start php-fpm
systemctl enable php-fpm
查看PHP默认端口号9000
netstat -tunlp
3安装Mariadb数据库 (默认端口 3306)
(1)安装数据库mariadb-server
yum install mariadb-server -y
(2) 开启启动
systemctl start mariadb
systemctl enable mariadb
netstat -tunlp
(3)配置登录密码 默认用户root
mysqladmin password '123456'
测试登录: mysql -uroot -p123456
show databases; # 查看当前有哪些库
create database hehe; # 创建hehe库
drop database hehe; # 删除hehe库
quit # 退出
4Nginx连接PHP-MySQL
(1) 配置 php.conf
在 /etc/nginx/conf.d/ 建立 一个 php.conf 的文件
|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| bash server { listen 80; server_name php.baimei .com; location / { root /code; index index.php index.html; } location ~ \.php$ { root /code; fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } }
|
(2)在/code目录下创建info.php文件,测试能否通过浏览器访问,访问成功如下图
systemctl restart nginx
|---------------------------------|
| bash <?php phpinfo(); ?>
|
[root@nginx ~]# cat /code/info.php
刷新浏览器是空白。
我们需要加入一个Include fastcgi_params
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | server { listen 80; server_name php.baimei.com; location / { root /code; index index.php index.html; } location ~ \.php$ { root /code; fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } |
fastcgi_params 是一个文件。
案例2:
在/code
目录下创建mysqli.php
文件,填入对应的数据库IP、用户名、密码
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | <?php $servername = "localhost"; $username = "root"; $password = "123456"; // 创建连接 $conn = mysqli_connect($servername, $username, $password); // 检测连接 if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "小哥哥,php可以连接MySQL..."; ?> <img style='width:100%;height:100%;' src=mysql.png> |