装包
1.安装依赖包
yum - y install gcc openssl-devel pcre-devel zlib-devel
2.解源码包
tar -xf nginx-1.12.3.tar.gz
3.切换到解压缩后的目录,配置参数
useradd -s /sbin/nologin nginx // 创建安全帐号
[root@proxy nginx-1.10.3]# ./configure > --prefix=/usr/local/nginx //指定安装路径 > --user=nginx //指定用户 > --group=nginx //指定组 > --with-http_ssl_module //开启SSL加密功能
4.编译安装
make && make install
5.创建软链接
ln -s /usr/local/nginx/sbin/nginx /sbin/
6.yum 安装mysql、php
yum - y install mariadb mariadb-server mariadb-devel yum -y install php php-fpm php-mysql
7.启动服务
nginx
# nginx -s reload ----nginx 重新加载配置文件 # nginx -s stop ----关闭nginx systemctl start php-fpm systemctl start mariadb
8.查看服务状态
netstat -anptul | grep nginx netstat -anptul | grep php netstat -anptul | grep mariadb
配置
1.php-fpm配置文件
[root@proxy etc]# 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 //最多允许几个进程处于空闲状态
2.nginx
[root@proxy ~]# 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 $document_root$fastcgi_script_name; include fastcgi.conf; }
[root@proxy ~]# nginx -s reload
测试
1.创建PHP页面,测试LNMP架构能否解析PHP页面
[root@proxy ~]# vim /usr/local/nginx/html/test1.php <?php $i="This is a test Page"; echo $i; ?>
2.创建PHP测试页面,连接并查询MariaDB数据库
[root@proxy ~]# vim /usr/local/nginx/html/test2.php <?php $mysqli = new mysqli('localhost','root','密码','mysql'); //注意:root为mysql账户名称,密码需要修改为实际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("</br>"); printf("Name:%s",$row[1]); printf("</br>"); } ?>
查看错误日志
Nginx的默认访问日志文件为/usr/local/nginx/logs/access.log
Nginx的默认错误日志文件为/usr/local/nginx/logs/error.log
PHP默认错误日志文件为/var/log/php-fpm/www-error.log
tailf /usr/local/nginx/logs/error.log
tailf /var/log/php-fpm/www-error.log