linux 下安装 LNMP 最简单的安装方法

56 篇文章 115 订阅 ¥9.90 ¥99.00
144 篇文章 4 订阅
本文介绍了在CentOS 7环境下,如何一步步安装Nginx 1.12、MySQL 5.7和PHP 5.6。包括下载、编译、配置、安装和测试等关键步骤,旨在提供一个简单易懂的LNMP搭建指南。
摘要由CSDN通过智能技术生成

最近在安装linux  lnmp centos7环境的时候还是有点小问题,从头总结了一下。

安装的版本是 nginx 1.12  mysql 5.7  php 5.6 

一,安装nginx 

nginx的官方网站:

http://nginx.org/en/download.html

Mainline version  主线版本

Stable version  稳定版本

Legacy versions  遗产版本 /历史版本

1.下载

安装前确认安装扩展 没有的直接 yum install wget gcc gcc-c++ pcre-devel zlib-devel openssl openssl-devel      

[root@localhost ~]# cd /usr/local/src/
[root@localhost src]# wget http://nginx.org/download/nginx-1.12.2.tar.gz

2.解压

     [root@localhost src]# tar zxvf nginx-1.12.2.tar.gz 

/****** 取消Debug编译模式   START*******/

cd nginx-1.12.2

vi auto/cc/gcc    #将这句注释掉 取消Debug编译模式 大概在172行
#CFLAGS="$CFLAGS -g"


/**********取消Debug编译模式   END**************/

3. 预编译

 cd nginx-1.12.2 

./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module --with-pcre --with-http_gzip_static_module --with-http_dav_module   --with-http_addition_module  --with-http_sub_module --with-http_flv_module  --with-http_mp4_module



解释

--with-http_gzip_static_module :支持压缩

--with-http_stub_status_module :支持nginx状态查询

--with-http_ssl_module :支持https

--with-pcre :为了支持rewrite重写功能,必须制定pcre

 --with-http_dav_module             #启用支持(增加PUT,DELETE,MKCOL:创建集合,COPY和MOVE方法)                        
--with-http_addition_module         #启用支持(作为一个输出过滤器,支持不完全缓冲,分部分相应请求)
--with-http_sub_module              #启用支持(允许一些其他文本替换Nginx相应中的一些文本)
--with-http_flv_module              #启用支持(提供支持flv视频文件支持)
--with-http_mp4_module              #启用支持(提供支持mp4视频文件支持,提供伪流媒体服务端支持)

make -j 4 && make install 4核编译

4. [root@localhost src]# make && make install 

5.添加系统变量(方便启停服务)

 [root@localhost nginx-1.12.2]# vim /etc/profile

  我一般是在56行添加    export PATH=/usr/local/nginx/sbin:$PATH

重启配置 source /etc/profile

[root@localhost nginx-1.12.2]# nginx -V

添加软连  ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

生成服务启动脚本

vim /etc/init.d/nginx

#!/bin/bash
# chkconfig: - 99 2
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
        start)
        $PROG
        ;;
        stop)
        kill -3 $(cat $PIDF)
        ;;
        restart)
        $0 stop &> /dev/null
        if [ $? -ne 0 ] ; then continue ; fi
        $0 start
        ;;
        reload)
        kill -1 $(cat $PIDF)
        ;;
        *)
        echo "Userage: $0 { start | stop | restart | reload }"
        exit 1
esac
exit 0

配置服务开机自动启动
[root@localhost ~]# chmod +x /etc/init.d/nginx
[root@localhost ~]# chkconfig --add nginx
[root@localhost ~]# chkconfig nginx on

首次启动   /usr/local/nginx/sbin/nginx

二、安装mysql 5.7

用的是rpm 好处是不用配置那么多东西 。 配置不用管。

 

   [root@localhost ~]# cd /usr/local/src/
   [root@localhost src]# wget http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm 

   [root@localhost src]# rpm -ivh mysql57-community-release-el7-8.noarch.rpm 

   [root@localhost src]#  yum -y install mysql-server 

也可以指定安装目录     yum --installroot=/usr/local/mysql --releasever=/ -y install mysql-server 可以自己研究

 

根据步骤安装就可以了,

默认配置文件路径: 
配置文件:/etc/my.cnf 
日志文件:/var/log/var/log/mysqld.log 
服务启动脚本:/usr/lib/systemd/system/mysqld.service 

socket文件:/var/run/mysqld/mysqld.pid

 启动mysql服务

service mysqld restart

 重置密码

    [root@localhost ~]# grep "password" /var/log/mysqld.log  

  

可以看到  输入 mysql -u root -p   密码 进入      第一次登陆 ,需要重置密码 要不什么也不能操作 

  接下来重置密码:5.7.20 为了安全密码           必须包含 数字字母符号

 alter user 'root'@'localhost' identified by 'Root!!2018'; 

   也可以 直接再添加新用户     

  grant all on *.* to 'rootadmin'@'%' identified by 'Root@@'  with grant option;

  增加root用户指定可以任意IP登录,如果想限制只能让指定IP登录请把%替换成IP地址

最后记得刷新权限;

 flush privileges ;

三、安装php

 

需要的插件 包

yum -y install gcc gcc-c++ libxml2 libxml2-devel bzip2 bzip2-devel libmcrypt libmcrypt-devel openssl openssl-devel libcurl-devel libjpeg-devel libpng-devel freetype-devel readline readline-devel libxslt-devel perl perl-devel psmisc.x86_64 recode recode-devel libtidy libtidy-devel  epel-release libmcrypt-devel  autoconf

1.下载

      [root@localhost ~]# cd /usr/local/src/

      [root@localhost src]# wget http://cn2.php.net/distributions/php-5.6.32.tar.gz

2.解压

     [root@localhost src]# tar zxvf php-5.6.32.tar.gz

3. 预编译

进入目录 [root@localhost src]# cd php-5.6.32

创建php-fpm用户,并禁止登录;
[root@localhost php-5.6.32]# useradd -s /sbin/nologin php-fpm 

./configure --prefix=/usr/local/php --sysconfdir=/usr/local/php/etc --with-config-file-path=/usr/local/php/etc/    --with-fpm-user=php-fpm  --with-fpm-group=php-fpm  --enable-fpm --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-mhash  --with-openssl --with-zlib --with-bz2 --with-curl --with-libxml-dir --with-gd --with-jpeg-dir  --with-png-dir --with-zlib --enable-mbstring --with-mcrypt --enable-sockets --with-iconv-dir   --enable-zip --with-pcre-dir --with-pear --enable-session  --enable-gd-native-ttf  --enable-xml --with-freetype-dir --enable-gd-jis-conv --enable-inline-optimization --enable-shared  --enable-soap --enable-bcmath --enable-sysvmsg --enable-sysvsem --enable-sysvshm  --enable-mbregex --enable-pcntl --with-xmlrpc --with-gettext --enable-exif --with-readline   --enable-ftp   --enable-redis

提示错误mcrypt.h没有找到,安装libmcrypt-devel包,默认的yum源,没有这个包,需要安装epel扩展源后,才可以安装。

[root@localhost php-5.6.32]# yum install -y epel-release

[root@localhost php-5.6.32]# yum install -y libmcrypt

[root@localhost php-5.6.32]# yum install -y libmcrypt-devel

 

再次执行./configure,没有错误提示,出现Thank you for using PHP,配置OK。

完成后使用echo $?查看是否安装正确;
[root@localhost  php-5.6.32]# make && make install 

[root@localhost  php-5.6.32]# echo $?

0 0表示上一步的结果成功。

 

配置文件

需要将当前目录下的php.ini文件拷贝到 php的安装目录etc下

[root@localhost php-5.6.32]# cp php.ini-production /usr/local/php/etc/php.ini

php.ini 文件是在包目录下的 php.ini-development(开发), php.ini-production(生产)  

拷贝php启动脚本,php-fpm配置文件,更改php-fpm权限为755;添加php-fpm开机启动;

[root@ php-5.6.32]# cp /usr/local/src/php-5.6.32/sapi/fpm/init.d.php-fpm   /etc/init.d/php-fpm 
(启动脚本)
[root@ php-5.6.32]# mv /usr/local/php/etc/php-fpm.conf.default   /usr/local/php/etc/php-fpm.conf  (就是去掉了末尾的.default )
[root@ php-5.6.32]# chmod 755 /etc/init.d/php-fpm 
[root@lphp-5.6.32]# chkconfig --add php-fpm 
[root@lphp-5.6.32]# service php-fpm start 
Starting php-fpm  done
[root@php-5.6.32]# chkconfig php-fpm on 

将php的安装目录也加入到系统的环境变量  在最后一行加入

vim /etc/profile

export PATH=/usr/local/php/bin:$PATH

source /etc/profile 重新加载

[root@localhost ~]# php -v
PHP 5.6.32 (cli) (built: Mar 12 2018 17:43:15) 
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies完成 接下来就是测试

-----php---安装成功

三、测试   在地址栏输入你的ip。然后测试PHP安装是否成功。确保nginx 和PHP都是运行的。

1.写测试页面  进入nginx的html    cd /usr/local/nginx/html/

编辑    vim index.php

<?php
phpinfo();
?>

2. 配置nginx

核心配置的两个 加入到nginx.conf

vim /usr/local/nginx/conf/nginx.conf

找到 location   添加  index.php

将请求转给php的9000端口  确保nginx 和PHP都是运行的哈。  

  location ~ \.php$ {

            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

安装上面的应该没问题,有问题的留言,大家一块解决。

**************************************************************************************

 

 

参考

这个是添加pathinfo的  除了首页能访问别的页面都是404的问题 

          try_files $uri $uri/ /index.php?$query_string;
               if (!-e $request_filename){  
                         rewrite ^(.*)$ /index.php?s=$1 last;  break;
                      } 

这是 server{} 里面的完整配置 

server {
        listen       80;
        server_name  localhost;
        root   /www/yiqi/public/;


        location / {
            root   /www/yiqi/public/;
            index  index.php index.html index.htm;
                        try_files $uri $uri/ /index.php?$query_string;
                                 if (!-e $request_filename) {
                                   rewrite ^(.*)$ /index.php?s=$1 last;  break;
                                 }

        }



         location ~ .*\.(php|php5)?$ {
            root   /www/yiqi/public/;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
            fastcgi_connect_timeout 75;
			fastcgi_read_timeout 600;
			fastcgi_send_timeout 600;
        }

         error_page 404 /404.html;
         #access_log  logs/80.access.log  main;
         #error_log   logs/80.error.log  info;

        }

 

 

评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

舰长115

码字不易如果觉得还不错谢谢鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值