手动配置lnmp环境

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 做php开发的,想要进一步提升自己,手动搭建开发环境,我想是必须经历的一个坎。虽然说有很多第三方集成环境可供使用,但我想说的是在你没有自己搭建过一次环境的时候,你没有太多的资本去“偷懒”。

做php开发的,想要进一步提升自己,手动搭建开发环境,我想是必须经历的一个坎。虽然说有很多第三方集成环境可供使用,但我想说的是在你没有自己搭建过一次环境的时候,你没有太多的资本去“偷懒”。虽然我自己也是个菜鸟,但我乐意分享我的成长历程和学习成果。所以今天我说说我自己搭建lnmp环境的整个流程,有表述不清的地方或者错误,希望大伙不吝指出。废话不多说了,开干。

一、主机环境:

虚拟机安装的centos 6.7 , i686


二、各应用版本:

php: 5.6.3   

mysql: 5.6.12   

nginx: 1.8.0


三、准备安装包: 

为了节省大伙时间,我已经放置在微云盘: http://share.weiyun.com/f891e9c6547bd9abf03072e554626ee6(密码:9Z4G),包含了安装过程需要用到的所有安装包。

现在,我们做如下约定:

  1. <pre name="code" class="plain">  <strong><span style="font-size:18px;">/usr/local/src</span></strong>


     这是我们存放安装包的路径
  2. <pre name="code" class="plain"> <strong><span style="font-size:18px;">/usr/local</span></strong>


     这是各应用的安装路径 

 在安装包目录下解压压缩包: 

        <pre name="code" class="plain">        <strong><span style="font-size:18px;">unzip   lnmp.zip</span></strong>

 
 

四、环境预设:

1、防火墙设置:

打开 iptables文件 

<pre name="code" class="plain"><span style="font-size:18px;"> <strong>vim   /etc/sysconfig/iptables</strong></span>


 
 

 
 添加两行: 
 

<span style="font-size:18px;">-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT</span>

第一行是为网络协议开放的端口,第二行为mysql数据库开放的端口。

编辑完要重启iptables:

<pre name="code" class="plain"><strong><span style="font-size:18px;">service iptables restart</span></strong>

 
 

 
 2、关闭SELINUX:
  
 

<strong><span style="font-size:18px;">vim /etc/selinux/config</span></strong>

</pre><pre>
注释掉 SELINUX=enforcing 和 SELINUXTYPE=targeted, 然后 在最底部添加  SELINUX=disabled 

  3、安装编译工具及依赖库文件:

直接用yum命令安装,如下:

<pre name="code" class="plain"><strong><span style="font-size:18px;">yum install -y apr* autoconf automake bison bzip2 bzip2* cloog-ppl compat* cpp curl curl-devel fontconfig fontconfig-devel freetype freetype* freetype-devel gcc gcc-c++ gtk+-devel gd gettext gettext-devel glibc kernel kernel-headers keyutils keyutils-libs-devel krb5-devel libcom_err-devel libpng libpng* libpng-devel libjpeg* libsepol-devel libselinux-devel libstdc++-devel libtool* libgomp libxml2 libxml2-devel libXpm* libX* libtiff libtiff* make mpfr ncurses* ntp openssl nasm nasm* openssl-devel patch pcre-devel perl php-common php-gd policycoreutils ppl telnet t1lib t1lib* wget zlib-devel</span></strong>


 
 
 
 

五、安装mysql

    1、由于是采用cmake方式编译安装mysql,因此得先安装cmake:

<pre name="code" class="plain"><strong><span style="font-size:18px;">cd /usr/local/src</span></strong>
<strong><span style="font-size:18px;">tar -zxvf  cmake-3.0.2.tar.gz</span></strong>
<strong><span style="font-size:18px;">cd cmake-3.0.2 
./configure && make && make install</span></strong>


 
 


  2、cmake安装完成后即可编译安装mysql了,正式安装mysql之前,我们还有个小任务
 
<pre name="code" class="plain"><strong><span style="font-size:18px;">groupadd mysql   #添加mysql组
useradd -g mysql mysql -s /bin/false  #创建用户mysql并加入到mysql组,不允许mysql用户直接登录系统
mkdir -p /data/mysql    #创建MySQL数据库存放目录
chown -R mysql:mysql /data/mysql     #设置MySQL数据库存放目录权限
mkdir -p /usr/local/mysql     #创建MySQL安装目录</span></strong>


 
 

  3、现在可以正式编译mysql了:

     

<pre name="code" class="plain"><strong><span style="font-size:18px;">tar -zxvf  mysql-5.6.21.tar.gz

cd mysql-5.6.21

cmake  .  -DCMAKE_INSTALL_PREFIX=/usr/local/mysql  -DMYSQL_DATADIR=/data/mysql  -DSYSCONFDIR=/etc  -DDEFAULT_CHARSET=utf8  -DDEFAULT_COLLATION=utf8_general_ci -DMYSQL_TCP_PORT=3306

make && make install</span></strong>


 
 

4、编译完成后我们就用进入mysql的安装目录:

<pre name="code" class="plain"><strong><span style="font-size:18px;">cd  /usr/local/mysql</span></strong>


 
 
生成mysql数据库系统:

<pre name="code" class="plain"><strong><span style="font-size:18px;">./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql</span></strong>


 
 


5、下面就要修改配置文件了:

<pre name="code" class="html">
 
 
<strong><span style="font-size:18px;">cp /usr/local/mysql/support-files/mysql.server  /etc/rc.d/init.d/mysqld</span></strong>
<strong><span style="font-size:18px;">
vim /etc/rc.d/init.d/mysqld</span></strong>

<pre name="code" class="plain">
 找到大概46、47行的位置,修改如下: 
 

<strong><span style="font-size:18px;">basedir=/usr/local/mysql
datadir=/data/mysql</span></strong>

保存退出。


6、试着启动mysql:

<pre name="code" class="plain"><strong><span style="font-size:18px;">service mysqld start</span></strong>

 
 
可以看到 "Starting  MySQL. SUCCESS !"说明启动成功。

我们通常习惯将其放置系统变量,因此打开/etc/profile 文件,在倒数第三行左右,修改如下:

<strong><span style="font-size:18px;">export PATH=$PATH:/usr/local/mysql/bin</span></strong>


然后刷新/etc/profile文件:

<pre name="code" class="plain"><strong><span style="font-size:18px;">source /etc/profile</span></strong>


 
 


7、mysql安装完成后是默认没有设置root的密码的,因此安全起见,我们设置root账户密码:

<pre name="code" class="plain"><strong><span style="font-size:18px;">mysql_secure_installation</span></strong>


 
 
输入命令后按照提示走,相信你可以的。

设置完后,我们测试root密码是否生成了:

<pre name="code" class="plain"><strong><span style="font-size:18px;">mysql -uroot -p</span></strong>


 
 
根据提示输入刚刚设置的root新密码,

如果看到上面这个界面,那么恭喜你,说明你完成了mysql的安装。


六、安装nginx

1、安装nginx所需的依赖包pcre、openssl、zlib:

<pre name="code" class="plain"><strong><span style="font-size:18px;">tar -zxvf pcre-8.36.tar.gz
cd  pcre-8.36
./configure && make && make install</span></strong>


 
 
安装openssl和zlib和安装pcre的方法是一样的,这里不多赘述。

在按装完openssl后,需要多做件事: 打开/etc/profile,在之前添加的

<strong><span style="font-size:18px;">export PATH=$PATH:/usr/local/mysql/bin</span></strong>

后面添加下面这行:

<strong><span style="font-size:18px;">export PATH=$PATH:/usr/local/openssl/bin
</span></strong>


保存后,让其生效:  

<pre name="code" class="plain"><pre name="code" class="plain"><strong><span style="font-size:18px;">source /etc/profile</span></strong>


 
 
 
 
2、编译安装nginx: 

 <pre name="code" class="plain"><strong><span style="font-size:18px;">tar -zxvf nginx-1.8.0.tar.gz
cd  nginx-1.8.0
./configure --prefix=/usr/local/nginx --without-http_memcached_module --user=nobody  --group=nobody  --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-openssl=/usr/local/src/openssl-1.0.1j --with-zlib=/usr/local/src/zlib-1.2.8 --with-pcre=/usr/local/src/pcre-8.36</strong>
make && make install</span></strong>

 
 

编译安装完成后,启动: 

<pre name="code" class="plain"><pre name="code" class="plain"><strong><span style="font-size:18px;">/usr/local/nginx/sbin/nginx</span></strong>

 
 
 
 

然后将在/etc/rc.d/init.d/下新增文件nginx,内容如下

<strong><span style="font-size:18px;">#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig:   - 85 15 
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /usr/local/nginx/conf/nginx.conf
# pidfile:     /usr/local/nginx/logs/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/local/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

lockfile=/var/lock/subsys/nginx

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    ulimit -SHn 65535
    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
}

restart() {
    configtest || return $?
    stop
    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</span></strong>


赋予其执行权限:

<pre name="code" class="plain"><strong><span style="font-size:18px;">chmod 775 /etc/rc.d/init.d/nginx</span></strong>

 
 
设置开机启动:

<pre name="code" class="plain"><strong><span style="font-size:18px;">chkconfig --add nginx
chkconfig nginx on </span></strong>


 
 
再重启nginx:

<pre name="code" class="plain"><strong><span style="font-size:18px;">service nginx restart</span></strong>

 
 
3、测试nginx是否安装成功:

在浏览器网址输入框输入:  localhost ,你会看到很鸡冻的画面。


==================================================我的鸡汤========================================================

天哪,废了好大劲才把mysql安装完成,这全部弄完不累死才怪。。。

害羞千万别这么想啊,最难过的时候,也是离成功最近的时候。所以一定要挺住,干吧得奋斗

=================================================================================================================


七、安装php

1、先安装php的相关扩展库yasm、libmcrypt、libvpx、tiff、libpng、freetype、jpeg

这些命令都是: ./configure && make && make install


安装 libgd 有些区别:

<pre name="code" class="plain"><strong><span style="font-size:18px;">./configure --prefix=/usr/local/libgd --enable-shared --with-jpeg=/usr/local/jpeg --with-png=/usr/local/libpng --with-freetype=/usr/local/freetype --with-fontconfig=/usr/local/freetype --with-xpm=/usr/ --with-tiff=/usr/local/tiff --with-vpx=/usr/local/libvpx
make && make install</span></strong>

 安装 t1lib 这里有个坑,虽小但估计害死了一批人,记得不是make而是make without_doc: 
 

<pre name="code" class="plain"><strong><span style="font-size:18px;">./configure --prefix=/usr/local/t1lib --enable-shared
make without_doc && make install</span></strong>


 
 
2、编译安装php

<pre name="code" class="plain"><strong><span style="font-size:18px;">tar -zvxf php-5.6.3.tar.gz

cd php-5.6.3

./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql-sock=/tmp/mysql.sock --with-pdo-mysql=/usr/local/mysql --with-gd --with-png-dir=/usr/local/libpng --with-jpeg-dir=/usr/local/jpeg --with-freetype-dir=/usr/local/freetype --with-xpm-dir=/usr/ --with-vpx-dir=/usr/local/libvpx/ --with-zlib-dir=/usr/local/zlib --with-t1lib=/usr/local/t1lib --with-iconv --enable-libxml --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-opcache --enable-mbregex --enable-fpm --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --enable-session --with-mcrypt --with-curl --enable-ctype

make && make test &&  make install</span></strong>

 
 
3、配置php

复制php配置文件到安装目录:

<pre name="code" class="plain"><strong><span style="font-size:18px;">cp  php.ini-production  /usr/local/php/etc/php.ini</span></strong>


 
 
将配置文件链到etc下:

<pre name="code" class="plain"><strong><span style="font-size:18px;">ln -s /usr/local/php/etc/php.ini /etc/php.ini</span></strong>


 
 


php.ini里的具体配置这里不多说。


4、由于我们使用php-fpm管理php的,因此php-fpm也需要配置

<pre name="code" class="plain"><strong><span style="font-size:18px;">cp /usr/local/php/etc/php-fpm.conf.default   /usr/local/php/etc/php-fpm.conf
vim  /usr/local/php/etc/php-fpm.conf</span></strong>


 
 
修改对应的地方(地25行左右):

<strong><span style="font-size:18px;">pid = run/php-fpm.pid #取消前面的分号</span></strong>



接下来设置php-fpm开机启动:

<pre name="code" class="plain"><strong><span style="font-size:18px;">cp /usr/local/src/php-5.6.3/sapi/fpm/init.d.php-fpm   /etc/rc.d/init.d/php-fpm 
chmod +x /etc/rc.d/init.d/php-fpm 
chkconfig php-fpm on </span></strong>


 
 
5、配置nginx支持php

所有服务都安装完了,但这时候nginx和php还不是同路人呢,得给他们牵条线,双方才能合作呢。

<pre name="code" class="plain"><strong><span style="font-size:18px;">vim /usr/local/nginx/conf/fcgi.conf</span></strong>


 内容如下: 
 

<pre name="code" class="plain"><strong><span style="font-size:18px;">fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;
</span></strong>


 
 

修改nginx配置文件

<pre name="code" class="plain"><strong><span style="font-size:18px;">vim /usr/local/nginx/conf/nginx.conf</span></strong>


 
 
内容修改如下:

<pre name="code" class="plain"><strong><span style="font-size:18px;">user  nobody nobody;
worker_processes  4;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
     server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /data/www;
            index  index.php index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        #error_page   500 502 503 504  /50x.html;
        #location = /50x.html {
        #    root   html;
        #}

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root           /data/www;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            include        fcgi.conf;   #此处就是引用刚刚新增的文件
        }
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
        location /tongji {
            stub_status on;
            access_log off;
            auth_basic "Nginx status";
            auth_basic_user_file /usr/local/nginx/.htpasswd;
        }
    }
   </span></strong>


 
 

6、测试php和nginx是否合作愉快:

在/data/www/目录下新增index.php:

写入php测试代码:

<pre name="code" class="php"><strong><span style="font-size:18px;"><?php
<span style="white-space:pre">	</span>phpinfo();
?></span></strong>


 
 
打开浏览器,输入localhost,如果你看到phpinfo函数打印出的相关信息,说明你成功了。好感动啊啊啊。。。。。


七、总结

此教程属于为新手准备的饭菜,按照此教程配置的lnmp环境只是达到了让其工作的层面,离部署代码正式上线是还有很大的差距,因此,希望各位参考为主,不应该将此配置成功用于正式环境。介于我粗劣的描述能力,不喜勿喷。大笑



相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
18天前
|
运维 前端开发 应用服务中间件
LNMP详解(八)——Nginx动静分离实战配置
LNMP详解(八)——Nginx动静分离实战配置
23 0
|
7月前
|
关系型数据库 MySQL 应用服务中间件
手动部署LNMP环境(Alibaba Cloud Linux 2)
本场景带您体验如何在Alibaba Cloud Linux 2.1903 LTS 64位操作系统的云服务器上搭建LNMP环境。
199 0
|
4月前
|
关系型数据库 应用服务中间件 nginx
基于Docker的LNMP环境微服务搭建
基于Docker的LNMP环境微服务搭建
基于Docker的LNMP环境微服务搭建
|
7月前
|
应用服务中间件 PHP nginx
基于Anolis OS 3快速搭建LNMP环境制作KodBox
本教程介绍如何搭建LNMP环境,其中本实验的LNMP分别代表Anolis OS 3、Nginx、Mariadb和PHP。
134 0
|
7月前
|
关系型数据库 MySQL 应用服务中间件
快速搭建LNMP环境
Nginx是一款小巧而高效的Web服务器软件,可帮您在Linux系统下快速方便地搭建出LNMP Web服务环境。本教程介绍如何搭建LNMP环境,其中LNMP分别代表Linux、Nginx、MySQL和PHP。
282 2
|
3月前
|
API PHP 数据库
Docker六脉神剑(四) 使用Docker-Compose进行服务编排搭建lnmp环境
Docker六脉神剑(四) 使用Docker-Compose进行服务编排搭建lnmp环境
28 0
|
6月前
|
关系型数据库 MySQL Linux
Linux环境下LNMP架构实战案例
Linux环境下LNMP架构实战案例
|
7月前
|
弹性计算 关系型数据库 MySQL
基于ROS快速部署LNMP环境(CentOS 7)
本教程提供在阿里云云服务器ECS上基于CentOS 7.9操作系统搭建LNMP环境的指引。LNMP是应用广泛的网站服务系统,由四种免费的开源软件Linux、Nginx、MySQL和PHP组成。搭建好LNMP环境后,您可以在该ECS实例上搭建网站、访问网站
406 0
|
7月前
|
关系型数据库 MySQL 应用服务中间件
手动部署LNMP环境(Ubuntu 20)
本教程介绍如何在Ubuntu 20.04操作系统的ECS实例上搭建一套Nginx、MySQL和PHP应用的开发环境。
294 0
|
7月前
|
关系型数据库 MySQL 应用服务中间件
基于Ubuntu搭建LNMP环境
本教程介绍如何在Ubuntu 18.04操作系统的ECS实例上搭建一套Nginx、MySQL和PHP应用的开发环境。
103 0