centos7.4 安装lnmp

liyuzhao7年前学习记录2752

最近购买了两天云服务器,所以环境需要重新配置下。采用php7.2+nginx+mysql5.7 .

安装步骤:

yum的安装

1
yum update

yum安装nginx

安装nginx最新源

1
2
yum localinstall http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum repolist enabled | grep "nginx*"

安装nginx

1
yum -y install nginx

启动nginx

1
systemctl start nginx.service

设置nginx服务器开机自启动

1
systemctl enable nginx.service

检查开机自动是否设置成功

1
systemctl list-dependencies | grep nginx

浏览器中输入公网ip,检测是否安装成功

使用yum安装mysql5.7

安装mysql源

1
2
yum -y localinstall http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
yum repolist enabled | grep "mysql.*-community.*"

安装mysql

1
yum -y install mysql-community-server install mysql-community-devel

启动mysql

1
systemctl start mysqld.service

检查mysql启动是否正常

1
service mysqld status 或者 ps -ef | grep mysql

设置mysqld服务开机自启动

1
systemctl enable mysqld.service

检查mysqld开机自启动是否设置成功

1
systemctl list-dependencies | grep mysqld

mysql5.7以后的争强了安全机制, 所以使用yum安装,启动会系统会自动生成一个随机的密码,修改mysql密码

查看mysql的随机密码

1
grep 'temporary password' /var/log/mysqld.log

使用查询得到的随机密码在终端登录

1
2
mysql -u root -p 更改密码(mysql文档规定,密码必须包括大小写字母数字加特殊符号>8位)
ALTER USER 'root'@'localhost' IDENTIFIED BY 'Yourpassword';

退出mysql客户端,用刚才修改的密码登录确保密码修改成功

1
2
exit;
mysql -u root -p

安装php7.2

安装php源

1
2
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

检查源是否安装成功

1
yum repolist enabled | grep "webtatic*"

安装php扩展源

yum -y install php72w php72w-cli php72w-common php72w-devel php72w-embedded php72w-fpm php72w-gd php72w-mbstring php72w-mysqlnd php72w-opcache php72w-pdo php72w-xml php72w-pecl-memcached php72w-pecl-redis

验证php7.2.x和扩展是否安装成功 

验证php是否安装成功

1
php -v

验证对应的扩展是否安装成功

1
php -m

设置php-fpm并检测php-fpm的运行状态

启动php-fpm

1
systemctl start php-fpm.service

检查启动是否成功

1
service php-fpm status

设置开机自启动

1
systemctl enable php-fpm.service

检查开机自启动是否设置成功


1
2
systemctl list-dependencies | grep php-fpm
ps -ef | grep php-fpm


注意:到此步,nginx只是一个web服务器,但是还没能处理php的文件,所以要对nginx进行配置。

我安装的nginx是最新的版本,所以配置文件在/etc/nginx/nginx.conf  但是没有必要修改这里面的配置,如果你要修改成负载均衡的配置,建议直接上网找,

我们不讨论这些,我需要让我本机支持php的解析

第一步配置nginx:

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

复制内容:

server {

    listen       80;

    server_name  #;  #这里填你的ip或者访问域名

    root /usr/share/nginx/html;

    index  index.html index.htm index.php;


    error_page  404              /404.html;

    location = /404.html {

        return 404 'Sorry, File not Found!';

    }

    error_page  500 502 503 504  /50x.html;

    location = /50x.html {

        root   /usr/share/nginx/html; # windows用户替换这个目录

    }


    location / {

        try_files $uri @rewrite;

    }


    location @rewrite {

        set $static 0;

        if  ($uri ~ \.(css|js|jpg|jpeg|png|gif|ico|woff|eot|svg|css\.map|min\.map)$) {

            set $static 1;

        }


        if ($static = 0) {

            rewrite ^/(.*)$ /index.php?s=/$1;

        }


    }


    location ~ /Uploads/.*\.php$ {

        deny all;

    }


    location ~ \.php/ {

       if ($request_uri ~ ^(.+\.php)(/.+?)($|\?)) { }

       fastcgi_pass 127.0.0.1:9000;

       include fastcgi_params;

       fastcgi_param SCRIPT_NAME     $1;

       fastcgi_param PATH_INFO       $2;

       fastcgi_param SCRIPT_FILENAME $document_root$1;

    }


    location ~ \.php$ {

        fastcgi_pass 127.0.0.1:9000;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        include fastcgi_params;

    }


    location ~ /\.ht {

        deny  all;

    }

}



也可以参照该网站的nginx配置:nginx的配置

,踩坑了,我不打算开启这个pathinfo,所以用thinkphp的时候,采用兼容url访问的时候出错了。找到了一篇文章能解决这个问题的。解决thinkphp5路由访问的问题


第二步配置PHP处理器(该步可以不做):

理由:关闭此项是为了防止漏洞,但是可能会导致php一些全局变量失效,如果开启会有漏洞,那就是图片能携带执行php的代码上传。具体操作自行百度。

vim /etc/php.ini

查找cgi.fix_pathinfo

将 ;cgi.fix_pathinfo=1改为cgi.fix_pathinfo=0

修改完成,重启nginx,命令:systemctl restart nginx.service 


以上第二步找到了三种方法来解决该问题(复制网上的方法):

方法一:修改php.ini,设置cgi.fix_pathinfo = 0;然后重启php-cgi。此修改会影响到使用PATH_INFO伪静态的应用,例如:http://xiumu.blog.51cto.com/520.html 就不能访问了。


方法二:在nginx的配置文件添加如下内容后重启:if ( $fastcgi_script_name ~ \..*\/.*php ) {return 403;}。该匹配会影响类似 http://xiumu.blog.51cto.com/5.0/helloworld.php的访问。


方法三:

----------代码源自网络----------

if ($request_filename ~* (.*)\.php) {
    set $php_url $1;
}
if (!-e $php_url.php) {
    return 403;
}

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        $uri;
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;


----------结束----------

新建一个名为:fastcgi.conf,将以上内容保存进去,同时在localtion里面做如下设置:


----------代码源自网络----------

location ~* .*\.php($|/)
{
      if ($request_filename ~* (.*)\.php) {
            set $php_url $1;   #请根据实际情况设置
      }
      if (!-e $php_url.php) {
            return 403;
      }

      fastcgi_pass  127.0.0.1:9000;
      fastcgi_index index.php;
      include fcgi.conf;
}

----------结束----------




测试:

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

输入:

<?php

phpinfo();

?>

保存: !wq

打开浏览器输入  xxx/test.php

image.png


以上是参考该文章,只做了一些小修改lnmp安装

相关文章

探讨mysql的权限问题

不管是本地连接数据库还是远程连接数据库,要想连接成功必须要有四样:1:host   –>连接地址2:user  –>连接账号3:password  –>...

探索sql合并插入+事务性能问题

探索sql合并插入+事务性能问题准备:1:为了方便默认用thinkphp5框架来测试,直接下载官方的thinkphp5框架,部署到本地的环境,开启debug调试来追踪sql语句。2:新建一个表为tes...

探讨mysql的游标cursor

探讨mysql的游标cursor

以下是我的做法步骤代码://新建动物表create table dw(dw_id int,dw_name varchar(20),dw_num varchar(10));//插入几条数据insert...

用mysql创建触发器

学习创建触发器:触发器的定义就是,当执行增删改查等操作就会触发执行sql语句。 先创建两个表:dongwu表和购买buy表,数据字典如下:动物表:Create table dongwu(Dw...

数据库实现1+到N得出总和的编程

数据库实现1+到N得出总和的编程

delimiter $    //这个是把结束符号变为$create procedure p1(N int)begindeclare total int default 0;dec...

redis在centos7 自启动,并用service启动或者关闭

redis在centos7 自启动,并用service启动或者关闭

1.为了让redis-server能在系统启动时自动运行,需要将redis服务作为守护进程(daemon)来运行,我们回/usr/local/cluster/7000/目录中找到一个redis.con...

评论列表

1
2024-07-25 07:39:54

识别码:fowtvu54720IY-这游戏真不错!http://www.a5km.com/yxgl/dnf/22762.html

11
2024-07-25 07:40:38

识别码:wbviof38052MI-这游戏真不错!http://www.dnf70.com/2137.html

追忆
2024-07-26 15:22:47

识别码:igtwcn85139DR-段落清楚有层次成语运用极佳http://www.jlxihu.com/post/17049.html

直播APP
2024-07-30 14:40:32

识别码:vjswuk62179FH-17.3 about a sexhttp://www.sbzedu.com

1
2024-08-19 11:40:11

识别码:xtfmgk52301UZ-这游戏真不错!http://www.3553km.com

11
2024-08-19 11:40:55

识别码:rqyhxa56170FI-这游戏真不错!http://www.3553km.com

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。