CentOS7 搭建LNMP搭建流程及遇到的问题(配置yum源,centos开放端口,MySQL首次访问拒绝,nginx配置,浏览器访问PHP文件出现直接下载,nginx报错,nginx自启etc)

涉及解决LNMP安装后出现的问题有

      修改nginx默认web目录 ,  浏览器访问.php文件直接下载 ,linux重启后nginx报错 ,设置nginx,php-fpm开机自启 

目录

楼主操作环境

1.centos7.0配置yum源

2.安装php7.0  参考链接

3.安装MySQL

4.安装nginx

至此LNMP搭建完成

5. window浏览器访问vmware centos下的web

6. LNMP出现的问题


 


楼主操作环境

vmware 下 centos7.0 

nginx1.4  mysql5.7 php7.0

最终结果使用window浏览器访问


1.centos7.0配置yum源

  1.1 可能默认没有安装wget 已经安装可跳过

       yum install wget

1.2 备份原yum源码

       mv  /etc/yum.repos.d/CentOS-Base.repo  /etc/yum.repos.d/CentOS-Base.repo.backup

1.3 下载阿里yum源

     wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

1.4 生成缓存,配置完成

     yum makecache

2.安装php7.0  参考链接

2.1安装epel-release

yum -y install epel-release

2.2 安装PHP7.0

rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

2.3 安装php扩展

     2.3.1 查看扩展

   yum search php70w

    2.3.2 推荐扩展,安装命令(过程需要输入  y 确认)

yum install php70w php70w-fpm php70w-cli php70w-common php70w-devel php70w-gd php70w-pdo php70w-mysql php70w-mbstring php70w-bcmath

2.4 确认安装成功输出php版本

php -v

3.安装MySQL

        3.1 下载mysql 并安装

wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum install mysql-community-server

      3.2 重启mysql服务

service mysqld restart

       3.3  执行 mysql -u root 访问MySQL成功  设置密码 

   set password for 'root'@'localhost'=password('password');

      如果密码级别与默认的级别要求不符时候会报

     Your password does not satisfy the current policy requirements 

    可以使用如下命令修改默认级别

     set global validate_password_policy=0;      ---级别

    set global validate_password_length=4;     ---最小的默认密码位数

     3.4  执行 mysql -u root 访问MySQL出现 Access denied for user 'root'@'localhost' 

          3.4.1 停止mysql服务

          systemctl stop mysqld.service

          3.4.2 修改mysql配置文件实现无密码登陆

                  vim  /etc/my.cnf

                //  i 进入编辑  在最后一行加上

                  skip-grant-tables

                 //  wq保存退出

           3.4.3 启动mysql

                 systemctl  start  mysqld.service

           3.4.4 登录mysql (无 -p 登陆)命令如下

                  mysql -u root

            3.4.5 mysql5.7修改密码

                  use mysql;

                  update mysql.user set authentication_string=password('123456') where user='root' ;  

           3.4.6 还原3.4.2步的配置文件并重启MySQL

                // vim  /etc/my.cnf

                //  i 进入编辑  删除最后一行 skip-grant-tables

                 //  wq保存退出

              //重启  systemctl  start  mysqld.service

4.安装nginx

    4.1安装依赖模块 已安装可跳过

  yum install gcc-c++  
  yum install pcre pcre-devel  
  yum install zlib zlib-devel  
  yum install openssl openssl--devel  

    4.2 下载安装包

   wget http://nginx.org/download/nginx-1.7.4.tar.gz

    4.3安装nginx (我使用的是默认安装路径)

         4.3.1解压并打开目录

       tar -zxvf nginx-1.7.4.tar.gz 
       cd  nginx-1.7.4

         4.3.1安装

            ./configure --prefix=/usr/local/nginx

            make

            make install

    4.4 查看nginx目录

           whereis nginx

    4.5 启动nginx

     命令格式      nginx安装目录地址 -c nginx配置文件地址

      默认(但不一定,详见4.4结果)        /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf


至此LNMP搭建完成


5. window浏览器访问vmware centos下的web

     5.1 linux虚拟机的网络选择Bridged 桥接到本机网卡。

           有坑待填.....

     5.2 centos 开放 80端口(永久)

            firewall-cmd --zone=public --add-port=80/tcp --permanent

      5.3 获取linux ip 使用 window 浏览器访问

            命令      ip a 

           

          浏览器输入ip即可访问

6. LNMP出现的问题

       6.1 nginx默认web目录修改

             小坑待填......

       6.2 浏览器访问.php文件会直接下载,不识别

             6.2.1 打开nginx.conf配置,确认如下设置

           location ~ .*\.php$ {
           fastcgi_pass   127.0.0.1:9000;
           }

            6.2.2 确认开放 9000端口 (亦可以设置其他端口,6.2.1需与之对应)

firewall-cmd --permanent --zone=public --add-port=9000/tcp

firewall-cmd --reload

             6.2.3 启动9000端口

            php-cgi -b 127.0.0.1:9000 &

            6.2.4 重启nginx

                nginx安装目录 -s reload  或   systemctl restart nginx

      6.3 linux关机重启后 启动nginx报错  

             nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already i....

           多数情况是80端口问题 通用方法 kill -9 

           6.3.1 查看80端口进程情况

                 lsof -i :80 (如果没有安装lsof   执行安装命令  yun install -y lsof)

           6.3.2 根据 6.3.1 的pid关闭进程

                   kill -9 pid

           6.3.3 重启nginx

                 systemctl restart nginx

     6.4 centos7 设置nginx开机自启 

           6.4.1 再/etc/init.d/目录下创建nginx文件,命令内容如下

               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

make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -n "$user" ]; then
      if [ -z "`grep $user /etc/passwd`" ]; then
         useradd -M -s /bin/nologin $user
      fi
      options=`$nginx -V 2>&1 | grep 'configure arguments:'`
      for opt in $options; do
          if [ `echo $opt | grep '.*-temp-path'` ]; then
              value=`echo $opt | cut -d "=" -f 2`
              if [ ! -d "$value" ]; then
                  # echo "creating" $value
                  mkdir -p $value && chown -R $user $value
              fi
          fi
       done
    fi
}

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    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
}

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

      注意  这里的路径使用的是nginx默认的安装路径  如果是  自定义编译安装的nginx,需要修改如下配置

             nginx=”/usr/sbin/nginx”                                            #nginx执行程序的路径

             NGINX_CONF_FILE=”/etc/nginx/nginx.conf”         #配置文件的路径

        参考链接

         6.4.2 设置文件的执行权限

                 chmod a+x /etc/init.d/nginx

         6.4.3 将nginx服务加入chkconfig管理列表

         chkconfig --add /etc/init.d/nginx

          现在可以使用service对nginx进行启动,停止,重启等操作

       6.4.4 设置开机自启 

        chkconfig nginx on

 6.5 centos7 设置php-fpm开机自启 

         如果使用本文安装的PHP 可以使用 如下命令设置

         systemctl enable php-fpm.service

        如果设置失败,可以使用如下思路

          创建nginx.service文件   加入开机自启  百度可得

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值