CentOs lnmp快速搭建手把手图文教程

前言:算起来,笔者已经有一段时间没有自己搭建原生lnmp环境了。现在更多用的的是一键安装,今天尝试下,为大家摸摸路,少走弯路,不摸黑前进

lnmp:centos6.5+nginx1.10.2+mysql5.7+php7.2.0

(^-^)前期准备

(1)安装依赖包(大体都是安装php所需要的依赖):

yum install gcc bison bison-devel zlib-devel libmcrypt-devel mcrypt mhash-devel openssl-devel libxml2-devel libcurl-devel bzip2-devel readline-devel libedit-devel sqlite-devel libpng-devel libjpeg-devel freetype freetype-devel

该步骤几乎不会出现问题,我们进行下一步的操作~

(2)创建www用户

groupadd www
useradd -g www -s /sbin/nologin -M www

均提示:

好的嘛,既然提示我们用户已存在,那我们通过命令去查看一下!
(3)查找www用户

cat /etc/passwd|grep 用户名

既然www已经存在,我们进行下步操作

(4)查看centos版本:(当前服务器为centOs6.5。此步没什么用,但是最好知道自己的Linux操作系统版本)

cat /etc/issue

(^-^)开始安装php7.2+mysql5.7+nginx1.10.2

一、安装Nginx
yum install -y nginx 安装nginx

提示我们已经安装完毕
nginx -v  查看nginx版本

nginx安装完毕,让我们开始安装mysql5.7

二、安装mysql5.7

(1)下载mysql的repo源(下载时间较长,耐心等待耐心):

    wget https://dev.mysql.com/get/mysql57-community-release-el6-9.noarch.rpm

(2)安装mysql57-community-release-el6-9.noarch.rpm包

    rpm -Uvh mysql57-community-release-el6-9.noarch.rpm

(3)安装mysql,过程下载软件包会询问,请一直yes,此过程不截图

    yum install mysql-community-server

(4)启动mysql

    service mysqld start

(5)生成默认密码,5.7的默认密码存放在/var/log/mysqld.log | Awk命令:输出最后一个字段的内容,也就是打印出出事默认密码

   grep 'temporary password' /var/log/mysqld.log  | awk '{print $NF}'

(6)登陆mysql

   mysql -uroot -p

然后请按回车(进入输入密码界面):

请复制刚才的默认密码,这样就使用mysql了
(7)当然,我们可以根据自己的实际要求去设置密码(想使用初始密码的同志请绕道,?)
set global validate_password_policy=0;(设置mysql密码设置规则,0表示只检查密码长度)
set global validate_password_length=6; (设置设置的密码长度为6个字符)


篇幅有限,mysql密码设置规则有兴趣大家可以去了解一下
SET PASSWORD FOR 'root'@'localhost' =PASSWORD('******');
设置密码成功,不瞒各位,密码就是123456         ?

输入exit退出mysql,我们接下来进行php7.2.0的安装。

三、安装PHP7.2.2

(1)源码编译安装,想要其他版本请修改一下数字就ok了,请耐心等待下载
wget http://cn2.php.net/distributions/php-7.2.0.tar.gz

如果我们下载下来看到并没有我们要的php-7.2.0.tar.gz 类似的压缩文件,而是一个mirror的文件,很简单,执行下面命令给文件重命名就OK了:mv mirror php-7.2.0.tar.gz
(2)如果有,请解压文件,并进入文件php-7.2.0
tar zxvf php-7.2.0.tar.gz
cd /usr/local/php-7.2.0
(3)设置编译需要加载的模块(此时fpm-user使用用户为www,前期的www用户准备是为了这个)
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysqli --with-pdo-mysql --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --enable-ftp  --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --disable-fileinfo --enable-maintainer-zts --with-libdir=lib64

注意点:上面configure步骤,可能会因为缺少依赖包而报错,你的服务器可能安装过其他一些其他的包,不同的机器情况可能不一样,在编译的时候根据报错信息
用yum search 查找依赖包并安装,编译完成之后是没有error的!!!
完成下面(4)步骤后也是没有error的才可以进行后面的步骤。

(4)进行编译:
make clean && make && make install
(5)复制配置文件
cp php.ini-development /usr/local/php/lib/php.ini
(6)设置全局的php命令
vim /etc/profile
在文件最后添加:
PATH=$PATH:/usr/local/php/bin
export PATH
然后执行 命令 source /etc/profile
此时php就是全局命令了,可以通过php -v 查看php版本信息或者php -m 看看刚刚编译加载的模块了

(7)配置PHP-fpm(这几步都是没有回应的命令。只要没报错,就是成功的)

  (7-1)cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

  (7-2)cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf

  (7-3)cp /usr/local/php-7.2.0/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

  (7-4)chmod +x /etc/init.d/php-fpm

  (7-5)cp /usr/local/php/etc/php-fpm.d/www.conf.default/usr/local/php/etc/php-fpm.d/www.conf

(8)启动php-fpm /etc/init.d/php-fpm start

(9)(可选)配置php-fpm自启动,如果存在这个文件(文件路径/etc/init.d/php-fpm),这步省略

创建php-fpm启动脚本
vim /etc/init.d/php-fpm(如果你使用该命令发现已经有内容,那么请忽略该步骤,从添加到开机启动项开始)

插入如下内容:

#!/bin/sh  
# chkconfig:   2345 15 95

# description:  PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation \

# with some additional features useful for sites of any size, especially busier sites.
# DateTime: 2016-09-20

# Source function library.  
. /etc/rc.d/init.d/functions  

# Source networking configuration.  
. /etc/sysconfig/network  

# Check that networking is up.  
[ "$NETWORKING" = "no" ] && exit 0  

phpfpm="/usr/local/php/sbin/php-fpm"  
prog=$(basename ${phpfpm})  

lockfile=/var/lock/subsys/phpfpm

start() {  
    [ -x ${phpfpm} ] || exit 5  
    echo -n $"Starting $prog: "  
    daemon ${phpfpm}
    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 ${phpfpm} -HUP  
    RETVAL=$?  
    echo  
}  

force_reload() {  
    restart  
}  

configtest() {  
  ${phpfpm} -t
}  

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  
        ;;  
    status)  
        rh_status  
        ;;  
    *)  
        echo $"Usage: $0 {start|stop|status|restart|reload|configtest}"  
        exit 2  
esac

添加到开机启动项
chkconfig --add php-fpm

此时也可以使用service来启动php-fpm了

service php-fpm start
service php-fpm stop

恭喜你,如果完成到这里,说明你已经优秀得完成了lnmp的安装。你在成功的路上?????????????????????


接下来,我们进行简单的测试环境并运行环境

(1)创建phpinfo页面

vim /usr/share/nginx/html/phpinfo.php

<?php
    echo  phpinfo();

(2)编辑nginx访问配置文件

vim /etc/nginx/conf.d/default.conf

删除原有内容,请copy下面内容
 

server {
     listen       80;
     server_name  localhost;

     #charset koi8-r;
     #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index index.php index.html index.htm;
    }
    location ~ \.php$ {
       root           html;
       fastcgi_pass   127.0.0.1:9000;
       fastcgi_index  index.php;
       fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
       include        fastcgi_params;
 }


    #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   /usr/share/nginx/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           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
     #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
     #    include        fastcgi_params;
     #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
     #location ~ /\.ht {
    #    deny  all;

     #}
 }

重新加载Nginx,重启php-fpm

/etc/init.d/nginx restart
/etc/init.d/php-fpm restart

测试:

# curl 你的ip地址或域名/a.php

 或者直接到网页上访问。

最后nginx,MYSQL,PHP-FPM的运行命令献上:

mysql启动: service mysqld start
mysql停止: service mysqld stop
mysql重启服务: service mysqld restart

nginx启动:service nginx start
nginx关闭:service nginx stop
nginx重启:service nginx restart

php-fpm启动:service php-fpm start
php-fpm关闭:service php-fpm stop
php-fpm重启:service php-fpm restart

码字不易,请珍惜,有问题大家随时留言,笔者欢迎大家探讨?

 

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值