步骤:
1.源码安装Nginx
[root@server0 nginx-1.12.2]# useradd -s /sbin/nologin nginx
[root@server0 lnmp_soft]# cd nginx-1.12.2/
[root@server0 nginx-1.12.2]# yum -y install gcc openssl-devel pcre-devel
[root@server0 nginx-1.12.2]# ./configure
–user=nginx
–group=nginx
–with-http_ssl_module
make && make install
2.使用YUM装包
[root@server0 nginx-1.12.2]# yum -y install mariadb mariadb-server mariadb-devell php php-fpm php-mysql
3.开启服务
[root@server0 nginx-1.12.2]# systemctl restart mariadb
[root@server0 nginx-1.12.2]# systemctl restart php-fpm
[root@server0 nginx-1.12.2]# /usr/local/nginx/sbin/nginx
4.修改php-fpm配置文件
[root@server0 nginx-1.12.2]# vim /etc/php-fpm.d/www.conf
[www]
listen = 127.0.0.1:9000 //PHP端口号
pm.max_children = 32 //最大进程数量
pm.start_servers = 15 //最小进程数量
pm.min_spare_servers = 5 //最少需要几个空闲着的进程
pm.max_spare_servers = 32 //最多允许几个进程处于空闲状态
5.修改Nginx配置文件并启动服务
[root@server0 nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf
… …
location / {
root html;
index index.php index.html index.htm;
#设置默认首页为index.php,当用户在浏览器地址栏中只写域名或IP,不说访问什么页面时,服务器会把默认首页index.php返回给用户
}
location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000; #将请求转发给本机9000端口,PHP解释器
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME
d
o
c
u
m
e
n
t
r
o
o
t
document_root
documentrootfastcgi_script_name;
include fastcgi.conf; #加载其他配置文件
}
[root@server0 nginx-1.12.2]# /usr/local/nginx/sbin/nginx -s reload
6.创建php页面,测试LNMP架构能否解析php压面
1).[root@server0 nginx-1.12.2]# vim /usr/local/nginx/html/test.php
<?php $i="This is a test Page"; echo $i; ?>2).[root@server0 nginx-1.12.2]# vim /usr/local/nginx/html/mysql.php
<?php $mysqli = new mysqli('localhost','root','密码','mysql'); //注意:root为mysql数据库的账户名称,密码需要修改为实际mysql密码,无密码则留空即可 //localhost是数据库的域名或IP,mysql是数据库的名称 if (mysqli_connect_errno()){ die('Unable to connect!'). mysqli_connect_error(); } $sql = "select * from user"; $result = $mysqli->query($sql); while($row = $result->fetch_array()){ printf("Host:%s",$row[0]); printf(""); printf("Name:%s",$row[1]); printf(""); } ?>3).使用客户端检验是否成功
[root@client ~]# firefox http://192.168.4.5/test.php
[root@client ~]# firefox http://192.168.4.5/mysql.php
7.LNMP常见问题
Nginx的默认访问日志文件为/usr/local/nginx/logs/access.log
Nginx的默认错误日志文件为/usr/local/nginx/logs/error.log
PHP默认错误日志文件为/var/log/php-fpm/www-error.log
如果动态网站访问失败,可用参考错误日志,查找错误信息。