实验 详解LNMP环境部署

一 安装Nginx

1.1 软件支持,创建运行用户,组

[root@localhost ~]# yum -y install pcre-devel zlib-devel
[root@localhost ~]# useradd -M -s /sbin/nologin nginx

1.2 编译安装

[root@localhost ~]# cd /opt/
[root@localhost opt]# tar xf nginx-1.15.9.tar.gz
[root@localhost opt]# cd nginx-1.15.9/
[root@localhost nginx-1.15.9]# ./configure 
--prefix=/usr/local/nginx 
--user=nginx 
--group=nginx 
--with-http_stub_status_module
[root@localhost nginx-1.15.9]# make && make install

1.3 路径优化

[root@localhost nginx-1.15.9]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost nginx-1.15.9]# ls -l /usr/local/sbin/nginx

1.4 检查配置文件

[root@localhost nginx-1.15.9]# nginx -t

1.5 启动,停止Nginx

[root@localhost nginx-1.15.9]# nginx 
[root@localhost nginx-1.15.9]# netstat -anpt | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      25166/nginx: master 
[root@localhost nginx-1.15.9]# yum -y install lynx
[root@localhost nginx-1.15.9]# lynx 127.0.0.1

在这里插入图片描述

[root@localhost nginx-1.15.9]# yum install psmisc  ##最小安装没有killall命令
[root@localhost nginx-1.15.9]# killall -s HUP nginx   ##重新加载
[root@localhost nginx-1.15.9]# killall -s QUIT nginx  ##停止服务

1.6 添加Nginx系统服务,要刷的配置参数

[root@localhost nginx-1.15.9]# vi /lib/systemd/system/nginx.service
[Unit]
Description=nginx         #####描述
After=network.target      ####描述服务类别
[Service]
Type=forking              ####后台运行形式
PIDFile=/usr/local/nginx/logs/nginx.pid         ####PID文件位置
ExecStart= /usr/local/nginx/sbin/nginx           #####启动服务
ExecReload= /usr/bin/kill -s QUIT $MAINPID               ####根据PID重新配置
ExecStop= /usr/bin/kill -s QUIT $MAINPID                  ####根据PID终止进程
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@localhost nginx-1.15.9]# chmod 754 /lib/systemd/system/nginx.service
[root@localhost nginx-1.15.9]# systemctl enable nginx.service

1.7 检查能否正常启动,停止,重启,重载Nginx

[root@localhost nginx-1.15.9]# netstat -antp | grep nginx
[root@localhost nginx-1.15.9]# killall -s HUP nginx
[root@localhost nginx-1.15.9]# netstat -antp | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      25496/nginx: master 

1.8 配置文件Nginx.conf

[root@localhost nginx-1.15.9]# vi /usr/local/nginx/conf/nginx.conf
##全局配置
/usr/local/nginx/conf/nginx.conf
#user  nobody;
worker_processes  1;
#error_log  logs/error.log;
#pid        logs/nginx.pid;

##I/O时间配置
events {
    use epoll;
    worker_connections  4096;
}

##HTTP配置
http {
  . . . . . . . . . . . . 
    access_log  logs/access.log  main;
    sendfile        on;
    . . . . . . . . 
    keepalive_timeout  65;
    . . . . . . . . 
    server {
        listen       80;
        server_name  www.51xit.top;
        charset utf-8;
         . . . . . . . . 
        location / {
            root   html;
            index  index.html index.php;
        }
                . . . . . . . . 
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
[root@localhost nginx-1.15.9]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

二 安装MySQL

见之前LAMP环境部署文档

三 安装PHP

3.1 安装依赖关系包

[root@localhost ~]# yum -y install \
libjpeg \
libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 \
libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel

3.2 编译安装

[root@localhost opt]# cd /opt   ##提前把安装包传到此目录下
[root@localhost opt]# tar xf php-7.1.10.tar.bz2
[root@localhost opt]# cd php-7.1.10/
[root@localhost php-7.1.10/]# ./configure \
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip
[root@localhost php-7.1.10/]# make && make install

[root@localhost php-7.1.10/]# cp php.ini-development /usr/local/php/lib/php.ini
[root@localhost php-7.1.10/]# vi /usr/local/php/lib/php.ini
mysqli.default_socket = /usr/local/mysql/mysql.sock
date.timezone = Asia/Shanghai	
[root@localhost php-7.1.10]# /usr/local/php/bin/php -m  ##验证安装的模块

3.3 配置及优化FPM模块

[root@localhost php-7.1.10]# cd /usr/local/php/etc/
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
[root@localhost etc]# cd /usr/local/php/etc/php-fpm.d/
[root@localhost php-fpm.d]# cp www.conf.default www.conf
[root@localhost php-fpm.d]# cd /usr/local/php/etc/
[root@localhost etc]# vi php-fpm.conf
pid = run/php-fpm.pid
;user = nginx
;group = nginx

[root@localhost etc]# /usr/local/php/sbin/php-fpm -c /usr/local/php/lib/php.ini
[root@localhost etc]# netstat -natp |grep 9000
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      99975/php-fpm: mast 
[root@localhost etc]# ln -s /usr/local/php/bin/* /usr/local/bin/  
##创建软链接
[root@localhost etc]# ps aux | grep -c "php-fpm”

3.4 让nginx支持PHP功能

[root@localhost etc]# vi /usr/local/nginx/conf/nginx.conf
location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }
[root@localhost etc]# vi /usr/local/nginx/html/index.php
 <?php
phpinfo();
?>  
[root@localhost etc]# killall -s HUP nginx   ##重启nginx

浏览器测试:http://20.0.0.21/index.php
在这里插入图片描述

3.5 测试数据库工作是否正常

[root@localhost ~]# mysql -u root -p  
CREATE DATABASE bbs;
GRANT all ON bbs.* TO 'bbsadm'@'%' IDENTIFIED BY 'admin123';
GRANT all ON bbs.* TO 'bbsadm'@'localhost' IDENTIFIED BY 'admin123';
flush privileges;  ##最后exit退出
[root@localhost ~]# vi /usr/local/nginx/html/index.php  
<?php
$link=mysqli_connect('20.0.0.11','bbsadm','admin123');
if($link) echo "<h1>Success!!</h1>";
else echo "Fail!!";
?>
[root@localhost ~]# killall -s HUP nginx   ##重启

浏览器测试:http://20.0.0.21/index.php
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值