美文网首页
基于Ubuntu的LNMP环境搭建

基于Ubuntu的LNMP环境搭建

作者: 楚江云 | 来源:发表于2017-03-04 19:45 被阅读310次
    1. 装备的工具

      Ubuntu16.04 , Xshell

    2. 使用Xshell链接到Ubuntu

      1. 使用xshell链接Ubuntu不是必须的,只是为了操作的方便,同时默认是你的Ubuntu已经安装好了

      2. 在Ubuntu中打开终端 ,执行命令

      3. sudo apt-get install openssh-server
        

      因为Ubuntu16.04默认不安装ssh-server服务,要使用Xshell连接上Ubuntu才需要安装

    3. 对安装的软件源进行优化

      首先备份原有的软件安装源

      sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak 
      

      打开源文件替换成阿里的源

      vim /etc/apt/sources.list
      

      清空sources.list文件中的所有内容

      复制下面的内容全部粘贴到sources.list文件中

      deb http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
      
      deb http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse
      
      deb http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse
      
      deb http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse
      
      deb http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse
      
      deb-src http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
      
      deb-src http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse
      
      deb-src http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse
      
      deb-src http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse
      
      deb-src http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multivers
      
      //更新源
      sudo apt-get update
      
      软件源优化完毕
      
    4. 安装Nginx,MySQL,PHP

      //安装Nginx
      sudo apt-get isntall nginx
      //查看Nginx版本号
      Nginx -v         //nginx version: nginx/1.4.6 (Ubuntu)
      //测试Nginx
      curl -I 'http://127.0.0.1'
      //显示结果如下,表示安装成功
      HTTP/1.1 200 OK
      Server: nginx/1.4.6 (Ubuntu)
      Date: Sat, 04 Mar 2017 06:52:38 GMT
      Content-Type: text/html
      Content-Length: 612
      Last-Modified: Tue, 04 Mar 2014 11:46:45 GMT
      Connection: keep-alive
      ETag: "5315bd25-264"
      Accept-Ranges: bytes
      
      //安装mysql
      sudo apt-get install mysql-server
      //安装过程中会要求输入数据库密码 自行处理后 回车键
      //mysql安装结束后 测试一下
      mysql -uroot -p
      //能进入数据库就表示安装成功
      
      //安装php7.0
      sudo apt-get install php7.0
      //查看php是否安装成功
      php -v
      //安装php7.0-fpm
      sudo apt-get install php7.0-fpm
      //如果没有这个安装包执行下面的命令 加入一个ppa源
      sudo apt-add-repository ppa:ondrej/php
      
      //修改配置文件让Nginx与php-fpm集成起来
      /*
      *通常Nginx与fastcgi通信有两种,一种是UNIX socket(默认) 另一种是TCP
      *我这里使用UNIX socket方式
      */
      //首先
      sudo vim /etc/php/7.0/fpm/pool.d/www.conf
      /*
      *在www.conf的大概36行的位置
      *如果是下面的路径就是正确的
      */
      listen = /run/php/php7.0-fpm.sock
      /*
      *保存并退出www.conf文件 检查该配置文件是否正确
      *提示 test is successful 表示配置文件是正确的
      */
      sudo php-fpm7.0 -t
      //其次 修改Nginx配置文件
      sudo vim /etc/nginx/sites-enabled/default
      //修改 约在24行的配置 不修改就是默认的位置 修改之后是表示自己的项目以后的主要的根目录
      //修改 约在25行的配置 表示表示能解析的文件类型 e.g:index index.php index.html index.htm index.nginx-debian.html
      24         root /var/www;
      25         index index.php index.html index.htm;
      //修改 约在54行到64行之间的配置文件 修改后如下
      54         location ~ \.php$ {
      55         #       fastcgi_split_path_info ^(.+\.php)(/.+)$;
      56         #       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
      57         #
      58         #       # With php5-cgi alone:
      59         #       fastcgi_pass 127.0.0.1:9000;
      60         #       # With php5-fpm:
      61                 fastcgi_pass unix:/run/php/php7.0-fpm.sock;
      62         #       fastcgi_index index.php;
      63                 include fastcgi_params;
      64         }
      /*
      * 一定要确保 www.conf中的监听的路径和此处的fastcgi_pass unix的路径是一致的
      * 保存退出该配置文件
      */
      /*
      * 检测Nginx的配置文件是否正确 
      */
      sudo nginx -t
      /*
      * 如果出现如下提示,表示配置文件是正确的
      *   nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
      *   nginx: configuration file /etc/nginx/nginx.conf test is successful
      */
      /*
      * 修改php.ini配置
      */
      vim /etc/php5/fpm/php.ini
      cgi.fix_pathinfo=0
      /*
      * 重启Nginx与php-fpm
      */
      systemctl restart nginx
      systemctl restart php7.0-fpm
      

    5. LNMP环境测试

      //进入项目更根目录

      cd /var/www

      //新建一个测试文件

      vim index.php

      //写入如下内容

      <?php

      phpinfo();

      ?>

      //打开Ubuntu的浏览器
      //输入localhost 就能看到结果了


      2017-03-04.png

    php连接MySQL

    //查看软件源中有那些php7.0能安装的软件包
    apt-cache search php7.0
    //先安装php-mysql
    sudo apt-get install php7.0-mysql
    //在 /var/www 目录先新建一个文件con.php
    vim con.php
    //在该文件中写入 如下内容
    <?php
    /*
    *$link = mysqli_connect('数据库地址','数据库登录用户','你的数据库密码','使用的库');
    *例如
    */
    $link = mysqli_connect('127.0.0.1','root','123456','sys');
    if (! $link ) {
    die( 'Connect Error ('  .  mysqli_connect_errno () .  ') '
    .  mysqli_connect_error ());
    }else{
    echo "success";
    }
    ?>
    //在浏览器地址栏中输入localhost/con.php
    //显示success就表示连接成功
    
    2017-03-04.png

    安装php扩展

    //gd库扩展

    sudo apt-get install php7.0-gd

    //加密扩展

    sudo apt-get install php7.0-mcryp

    //curl扩展

    sudo apt-get install php7.0-curl

    //安装memcache扩展

    sudo apt-get install php-memcached
    .... 根据自己的需要去安装扩展

    2017-03-04.png

    相关文章

      网友评论

          本文标题:基于Ubuntu的LNMP环境搭建

          本文链接:https://www.haomeiwen.com/subject/jfujgttx.html