本文档介绍如何使用一台普通配置的云服务器ECS实例搭建LNMP平台的web环境。

  • Linux:自由和开放源码的类UNIX操作系统。
  • Nginx:轻量级网页服务器、反向代理服务器。
  • MySQL:关系型数据库管理系统。
  • PHP:主要适用于Web开发领域的一种脚本语言。

适用对象

适用于熟悉Linux操作系统,刚开始使用阿里云进行建站的个人用户。

基本流程

使用云服务器 ECS 搭建LNMP平台的操作步骤如下:

  1. 准备编译环境
  2. 安装nginx
  3. 安装mysql
  4. 安装php-fpm
  5. 测试访问

步骤一:准备编译环境

本文主要说明手动安装LNMP平台的操作步骤,您也可以在 云市场 购买LNMP镜像直接启动ECS,以便快速建站。

  1. 系统版本说明

     
    # cat /etc/redhat-release
    CentOS release 6.5 (Final)
     
    说明 这是本文档实施时参考的系统版本。您的实际使用版本可能与此不同,下文中的nginx,mysql,及php版本,您也可以根据实际情况选择相应版本。
  2. 关闭SELINUX

    修改配置文件,重启服务后永久生效。

     
    # sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config

    命令行设置立即生效。

     
    # setenforce 0
  3. 安全组设置

    在ECS安全组放行需访问的端口和访问白名单,下面的示例表示允许所有IP访问服务器的80端口。您可以根据实际情况放行允许访问的客户端IP。

步骤二:安装nginx

Nginx是一个小巧而高效的Linux下的Web服务器软件,是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,已经在一些俄罗斯的大型网站上运行多年,目前很多国内外的门户网站、行业网站也都在是使用Nginx,相当稳定。

  1. 添加运行nginx服务进程的用户。

     
    # groupadd -r nginx
    # useradd -r -g nginx nginx
  2. 下载源码包解压编译。
     
    # wget http://nginx.org/download/nginx-1.10.2.tar.gz
    # tar xvf nginx-1.10.2.tar.gz -C /usr/local/src
    # yum groupinstall "Development tools"
    # yum -y install gcc wget gcc-c++ automake autoconf libtool libxml2-devel libxslt-devel perl-devel perl-ExtUtils-Embed pcre-devel openssl-devel
    # cd /usr/local/src/nginx-1.10.2
    # ./configure \
    --prefix=/usr/local/nginx \
    --sbin-path=/usr/sbin/nginx \
    --conf-path=/etc/nginx/nginx.conf \
    --error-log-path=/var/log/nginx/error.log \
    --http-log-path=/var/log/nginx/access.log \
    --pid-path=/var/run/nginx.pid \
    --lock-path=/var/run/nginx.lock \
    --http-client-body-temp-path=/var/tmp/nginx/client \
    --http-proxy-temp-path=/var/tmp/nginx/proxy \
    --http-fastcgi-temp-path=/var/tmp/nginx/fcgi \
    --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
    --http-scgi-temp-path=/var/tmp/nginx/scgi \
    --user=nginx \
    --group=nginx \
    --with-pcre \
    --with-http_v2_module \
    --with-http_ssl_module \
    --with-http_realip_module \
    --with-http_addition_module \
    --with-http_sub_module \
    --with-http_dav_module \
    --with-http_flv_module \
    --with-http_mp4_module \
    --with-http_gunzip_module \
    --with-http_gzip_static_module \
    --with-http_random_index_module \
    --with-http_secure_link_module \
    --with-http_stub_status_module \
    --with-http_auth_request_module \
    --with-mail \
    --with-mail_ssl_module \
    --with-file-aio \
    --with-ipv6 \
    --with-http_v2_module \
    --with-threads \
    --with-stream \
    --with-stream_ssl_module
    # make && make install
    # mkdir -pv /var/tmp/nginx/client
  3. 添加SysV启动脚本。
     
    # vim /etc/init.d/nginx
    #!/bin/sh
    #
    # nginx - this script starts and stops the nginx daemon
    #
    # chkconfig: - 85 15
    # description: Nginx is an HTTP(S) server, HTTP(S) reverse \
    # proxy and IMAP/POP3 proxy server
    # processname: nginx
    # config: /etc/nginx/nginx.conf
    # config: /etc/sysconfig/nginx
    # pidfile: /var/run/nginx.pid
    # Source function library.
    . /etc/rc.d/init.d/functions
    # Source networking configuration.
    . /etc/sysconfig/network
    # Check that networking is up.
    [ "$NETWORKING" = "no" ] && exit 0
    nginx="/usr/sbin/nginx"
    prog=$(basename $nginx)
    NGINX_CONF_FILE="/etc/nginx/nginx.conf"
    [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
    lockfile=/var/lock/subsys/nginx
    start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
    }
    stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
    killall -9 nginx
    }
    restart() {
    configtest || return $?
    stop
    sleep 1
    start
    }
    reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
    }
    force_reload() {
    restart
    }
    configtest() {
    $nginx -t -c $NGINX_CONF_FILE
    }
    rh_status() {
    status $prog
    }
    rh_status_q() {
    rh_status >/dev/null 2>&1
    }
    case "$1" in
    start)
    rh_status_q && exit 0
    $1
    ;;
    stop)
    rh_status_q || exit 0
    $1
    ;;
    restart|configtest)
    $1
    ;;
    reload)
    rh_status_q || exit 7
    $1
    ;;
    force-reload)
    force_reload
    ;;
    status)
    rh_status
    ;;
    condrestart|try-restart)
    rh_status_q || exit 0
    ;;
    *)
    echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
    exit 2
    esac
  4. 赋予脚本执行权限。
     
    # chmod +x /etc/init.d/nginx
  5. 添加至服务管理列表,设置开机自启。
     
    # chkconfig --add nginx
    # chkconfig nginx on
  6. 启动服务。
     
    # service nginx start
  7. 浏览器访问可看到默认欢迎页面。

步骤三:安装mysql

  1. 准备编译环境。

     
    # yum groupinstall "Server Platform Development"  "Development tools" -y
    # yum install cmake -y
  2. 准备mysql数据存放目录。
     
    # mkdir /mnt/data
    # groupadd -r mysql
    # useradd -r -g mysql -s /sbin/nologin mysql
    # id mysql
    uid=497(mysql) gid=498(mysql) groups=498(mysql)
  3. 更改数据目录属主属组。
     
    # chown -R mysql:mysql /mnt/data
  4. 解压编译在 MySQL官网 下载的稳定版源码包,这里使用的是5.6.24版本。
     
    # tar xvf mysql-5.6.24.tar.gz -C  /usr/local/src
    # cd /usr/local/src/mysql-5.6.24
    # cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
    -DMYSQL_DATADIR=/mnt/data \
    -DSYSCONFDIR=/etc \
    -DWITH_INNOBASE_STORAGE_ENGINE=1 \
    -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
    -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
    -DWITH_READLINE=1 \
    -DWITH_SSL=system \
    -DWITH_ZLIB=system \
    -DWITH_LIBWRAP=0 \
    -DMYSQL_TCP_PORT=3306 \
    -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
    -DDEFAULT_CHARSET=utf8 \
    -DDEFAULT_COLLATION=utf8_general_ci
    # make && make install
  5. 修改安装目录的属组为mysql。
     
    # chown -R mysql:mysql /usr/local/mysql/
  6. 初始化数据库。
     
    # /usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/mnt/data/
     
    说明 在CentOS 6.5版操作系统的最小安装完成后,在/etc目录下会存在一个my.cnf,需要将此文件更名为其他的名字,如:/etc/my.cnf.bak,否则,该文件会干扰源码安装的MySQL的正确配置,造成无法启动。
  7. 拷贝配置文件和启动脚本。
     
    # cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
    # chmod +x /etc/init.d/mysqld
    # cp support-files/my-default.cnf /etc/my.cnf
  8. 设置开机自动启动。
     
    # chkconfig mysqld  on
    # chkconfig --add mysqld
  9. 修改配置文件中的安装路径及数据目录存放路径。
     
    # echo -e "basedir = /usr/local/mysql\ndatadir = /mnt/data\n" >> /etc/my.cnf
  10. 设置PATH环境变量。
     
    # echo "export PATH=$PATH:/usr/local/mysql/bin" > /etc/profile.d/mysql.sh
    # source /etc/profile.d/mysql.sh
  11. 启动服务。
     
    # service mysqld start
    # mysql -h 127.0.0.1

步骤四:安装php-fpm

Nginx本身不能处理PHP,作为web服务器,当它接收到请求后,不支持对外部程序的直接调用或者解析,必须通过FastCGI进行调用。如果是PHP请求,则交给PHP解释器处理,并把结果返回给客户端。PHP-FPM是支持解析php的一个FastCGI进程管理器。提供了更好管理PHP进程的方式,可以有效控制内存和进程、可以平滑重载PHP配置。

    1. 安装依赖包。

       
      # yum install libmcrypt libmcrypt-devel mhash mhash-devel libxml2 libxml2-devel bzip2 bzip2-devel
    2. 解压官网下载的源码包,编译安装。
       
      # tar xvf php-5.6.23.tar.bz2 -C /usr/local/src
      # cd /usr/local/src/php-5.6.23
      # ./configure --prefix=/usr/local/php \
      --with-config-file-scan-dir=/etc/php.d \
      --with-config-file-path=/etc \
      --with-mysql=/usr/local/mysql \
      --with-mysqli=/usr/local/mysql/bin/mysql_config \
      --enable-mbstring \
      --with-freetype-dir \
      --with-jpeg-dir \
      --with-png-dir \
      --with-zlib \
      --with-libxml-dir=/usr \
      --with-openssl \
      --enable-xml \
      --enable-sockets \
      --enable-fpm \
      --with-mcrypt \
      --with-bz2
      # make && make install
    3. 添加php和php-fpm配置文件。
       
      # cp /usr/local/src/php-5.6.23/php.ini-production /etc/php.ini
      # cd /usr/local/php/etc/
      # cp php-fpm.conf.default php-fpm.conf
      # sed -i 's@;pid = run/php-fpm.pid@pid = /usr/local/php/var/run/php-fpm.pid@' php-fpm.conf
    4. 添加php-fpm启动脚本。
       
      # cp /usr/local/src/php-5.6.23/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
      # chmod +x /etc/init.d/php-fpm
    5. 添加php-fpm至服务列表并设置开机自启。
       
      # chkconfig --add php-fpm
      # chkconfig --list php-fpm
      # chkconfig php-fpm on
    6. 启动服务。
       
      # service php-fpm start
    7. 添加nginx对fastcgi的支持,首先备份默认的配置文件。
       
      # cp /etc/nginx/nginx.conf /etc/nginx/nginx.confbak
      # cp /etc/nginx/nginx.conf.default /etc/nginx/nginx.conf

      编辑/etc/nginx/nginx.conf,在所支持的主页面格式中添加php格式的主页,类似如下:

       
      location / {
      root /usr/local/nginx/html;
      index index.php index.html index.htm;
      }

      取消以下内容前面的注释:

       
      location ~ \.php$ {
      root /usr/local/nginx/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;
      }

      重新载入nginx的配置文件。

       
      # service nginx reload

      在/usr/local/nginx/html/新建index.php的测试页面,内容如下。

       
      # cat index.php
      <?php
      $conn=mysql_connect('127.0.0.1','root','');
      if ($conn){
      echo "LNMP platform connect to mysql is successful!";
      }else{
      echo "LNMP platform connect to mysql is failed!";
      }
      phpinfo();
      ?>

      浏览器访问测试,如看到以下内容则表示LNMP平台构建完成。

搭建LNMP环境(CentOS 6)的更多相关文章

  1. CentOS 7 源码搭建LNMP环境

    搭建 LNMP 环境 源码包版本 :  CentOS Linux  7 nginx-1.15.1.tar.gz  mysql-boost-5.7.21.tar.gz  php-7.2.7.tar.gz ...

  2. Centos 7 下yum搭建lnmp环境(yum安装方式)

    我们都知道linux下安装软件主要有三种方式: 1.源码编译安装,即下载软件源代码,利用gcc g++ make 等编译工具进行编译安装: 此方式的优点:可以指定软件版本,可选择性好:编译时可以手动指 ...

  3. Vmware搭建LNMP环境(Centos7+Nginx+Mysql+PHP7.1.8)

    参考:1.Linux学习之CentOS(一)----在VMware虚拟机中安装CentOS 7(图文教程) 2.Centos7搭建LNMP环境 3.MySQL5.7修改默认root密码 4.CentO ...

  4. WIN10 vagrant和virtualbox虚拟机和一键搭建lnmp环境配置thinkphp虚拟主机

    版本:win10系统 virtualbox:5.1.26 vagrant :1.9.7 centos 7.0 xshell/git 首先下载好对应版本的软件 配置vagrant和virtualbox ...

  5. docker搭建lnmp环境(问题,资料,命令)

    入门参考 http://www.runoob.com/docker/docker-install-nginx.html 十大常用命令玩转docker 1. #从官网拉取镜像 docker pull & ...

  6. centos7 yum搭建lnmp环境及配置wordpress超详细教程

    yum安装lnmp环境是最方便,最快捷的一种方法.源码编译安装需要花费大量的人类时间,当然源码编译可以个性化配置一些其它功能.目前来说,yum安装基本满足我们搭建web服务器的需求. 本文是我根据近期 ...

  7. Docker之使用Docker-compose搭建LNMP环境

    之前有随笔介绍使用Docker-compose搭建LNMP环境(centos6 php5.6) https://www.cnblogs.com/minseo/p/10146982.html 本文介绍D ...

  8. CentOS7——搭建LNMP环境(WordPress案例)

    CentOS7--搭建LNMP环境(WordPress案例) LNMP组成介绍 LNMP(Linux-Nginx-MySQL-PHP)网站架构是目前国际流行的Web框架,该框架包括:Linux操作系统 ...

  9. 阿里云体验实验室 教你如何《快速搭建LNMP环境》

    ## 体验平台简介 面向开发者和中小企业打造的一站式.全云端的开发平台,打开浏览器就可以开发.调试.上线,所测即所得,并结合无服务器的模式,重新定义云原生时代的研发工作方法论.旨在降低开发者上手成本和 ...

  10. 搭建 LNMP 环境

    搭建 LNMP 环境 搭建 Nginx 静态服务器 安装 Nginx 使用 yum 安装 Nginx: yum install nginx -y 修改 /etc/nginx/conf.d/defaul ...

随机推荐

  1. C# QuartZ使用实例写成服务

    官方学习文档:http://www.quartz-scheduler.net/documentation/index.html 官方的源代码下载:http://sourceforge.net/proj ...

  2. MVC中返回json数据的两种方式

    MVC里面如果直接将数据返回到前端页面,我们常用的方式就是用return view(): 那么我不想直接用razor语法,毕竟razor这玩意儿实在是太难记了,还不如写ajax对接来得舒服不是 那么我 ...

  3. blfs(systemd版本)学习笔记-安装lrzsz软件包实现ssh远程传输文件到lfs系统

    我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! 安装lrzsz软件包实现ssh远程传输文件到lfs系统 这个软件包在lfs系列的书中没有,这里是参照lrzsz官网的说明进行编译 ...

  4. js中数组常用的api 及其作用

  5. 【读书笔记】iOS-照相机与摄像头

    一,增强现实 增强现实(AR)是一种实时地计算摄影机影像的位置及角度并加上相应图像的技术,这种技术的目标是在屏幕上把虚拟世界套在现实世界并进行互动.这种技术估计由1990年提出.随着随身电子产品运算能 ...

  6. 使用jquery获取iframe内的元素属性

    当需要获取iframe里的内容时需要有几个前提,否则你是获取不到的: 1:当前页面与iframe的src的页面需要在同一个域名下: 2:必须要等iframe里边的页面加载完成才能获取,否则你要获取的标 ...

  7. .NET代码设计简单规范

    以下转载于:http://www.it28.cn/ASPNET/825095.html 下面这个规范是我为朋友写的几点建议,写的很范,作为BLOG,愿与大家一起分享.只给出部分设计规范样例,关于.NE ...

  8. 一条sql语句引发的遐想:select t.*, t.rowid from STUDENT t

    在学习oracle 过程当中,当在看tables时,比如STUDENT,右击——查看——查询,会自动有这样的一条查询语句: select t.*, t.rowid from STUDENT_TJB t ...

  9. mysql 优化配置参数(my.cnf)

    max_connections:允许客户端并发连接的最大数量,默认值是151,一般将该参数设置为500-2000max_connect_errors:如果客户端尝试连接的错误数量超过这个参数设置的值, ...

  10. loadrunner 场景设计-添加Windows Resources计数器

    场景设计-添加Windows Resources计数器 by:授客 QQ:1033553122 目的 监控要测试的windows服务器的资源使用情况 步骤 1.添加视图,方法双击.拖动左侧的Windo ...