美文网首页服务器
centos7.4 yum安装nginx+php7.1+mysq

centos7.4 yum安装nginx+php7.1+mysq

作者: 中v中 | 来源:发表于2018-09-21 19:54 被阅读58次

    环境: centos7.4

    本文介绍快速安装lnmp环境,以及redis,php-redis扩展

    1. 安装nginx

    yum install nginx
    ##开启nginx
    service nginx start
    

    安装成功后,浏览器访问主机公网IP,或者本机的127.0.0.1。会出现以下界面

    image.png

    2.安装MYSQL

    yum localinstall http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
    
    yum install mysql-community-server
    
    //开启mysql
    service mysqld start
    
    //查看mysql的root账号的密码
    grep 'temporary password' /var/log/mysqld.log
    

    //由于mysql5.7之后开启了密码强度校验,具体参看相关文档
    MySQL5.7安装及密码安全策略
    //在修改密码之前开发机建议去掉或降低mysql5.7的安全策略 本文采取去掉校验

    //如果不需要密码策略,添加my.cnf文件中添加如下配置禁用即可:
    validate_password = off
    //之后重启mysql服务

    //登录mysql
    mysql -uroot -p
    
    //修改密码
    ALTER USER 'root'@'localhost' IDENTIFIED BY 'password';
    
    //修改root用户可远程登录
    GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION; 
    
    //刷新
    flush privileges;
    
    image.png

    3.安装php

    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
    
    //查看
    yum search php71w
    
    //安装php以及扩展
    yum install php71w php71w-fpm php71w-cli php71w-common php71w-devel php71w-gd php71w-pdo php71w-mysql php71w-mbstring php71w-bcmath
    
    //开启服务
    service php-fpm start
    
    //修改/etc/nginx/nginx.conf  使其支持php 见下
    //重启nginx
    service nginx restart
    
    //server配置
    // 将 /etc/nginx/nginx.conf 的server { 80 端口的内容屏蔽或者修改为其他端口}
    // 在/etc/nginx/conf.d/ 里追加test.conf (需要在nginx.conf 里开启 include /etc/nginx/conf.d/*.conf;)
    // test.conf 内容为
    server {
        listen       80;#端口
        server_name localhost admin.com  www.admin.com; # 域名
    
        root /data/www/; # 网站根目录
        index index.php index.html index.htm;#默认的index
    
        # 建议放内网
        # allow 192.168.0.0/24;
        # deny all;
    
        location / {
    
    if (!-e $request_filename) {
     rewrite ^/(.*)$ /index.php?$1 last;#去除url中的index.php 不需要可以不写
    
    }
        }
    
        location ~ \.php$ {
            try_files $uri = 404;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
    
    

    测试文件见最后。测试结果如下:(同时在数据库里安装test库和表a)

    为什么不是5.7???


    image.png

    结果如下(说明db已经连接上并可以访问):


    image.png

    4.安装redis

    yum install redis
    //修改配置 
    vi /etc/redis.conf
    //daemonize yes 后台运行
    //appendonly yes 数据持久化
    service redis start
    

    redis的实际操作参考如下:

    CentOS7 linux下yum安装redis以及使用

    5.安装php-redis扩展

    //先装git
    yum install git
    
    //git下扩展
    cd /usr/local/src
    git clone https://github.com/phpredis/phpredis.git
    
    //安装扩展
    cd phpredis
    phpize
    
    [root@VM_103_117_centos phpredis]# phpize
    Configuring for:
    PHP Api Version:         20160303
    Zend Module Api No:      20160303
    Zend Extension Api No:   320160303
    
    // 编译php
    执行完上一步,我们就有了 configure 配置文件了,接下来配置
    [root@VM_103_117_centos phpredis]#./configure
    或者执行(php-config可以用 find / -name php-config来查找位置)
    [root@VM_103_117_centos phpredis]#./configure --with-php-config=/usr/bin/php-config
    
    接下来是编译安装
    [root@VM_103_117_centos phpredis]#make 
    [root@VM_103_117_centos phpredis]# make install
    Installing shared extensions: /usr/lib64/php/modules/
    
    //修改php配置
    vi /etc/php.ini  
    加入下面几行:
    [redis]
    extension_dir =/usr/lib64/php/modules/
    extension = redis.so
    
    redis.so文件的路径可以在make install的时候看到
    
    //重启php
    service php-fpm restart
    

    重启之后我们打开info.php,已经可以看到redis的扩展信息了


    image.png

    测试结果:


    image.png

    参考:

    CentOS7为php7.2安装php-redis扩展

    测试nginx,db和redis的testdb.php文件:

    <?php
    $servername = "localhost";
    $username = "root";
    $password = "root";
    $dbname = "test";
     
    // 创建连接
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("连接失败: " . $conn->connect_error);
    } 
     
    $sql = "SELECT a,b FROM a";
    $result = $conn->query($sql);
     
    if ($result->num_rows > 0) {
        // 输出数据
        while($row = $result->fetch_assoc()) {
            echo "a: " . $row["a"]. " - b: " . $row["b"]. "<br>";
        }
    } else {
        echo "0 结果";
    }
    $conn->close();
    
    // 测试redis
    $redis = new Redis();
    $redis->connect('127.0.0.1', 6379);
    $redis->set("name", "nango");
    echo "redis 测试 <br/>";
    echo $redis->get('name');
    
    ?>
    

    将其制作开机自启动
    centos7 支持 systemctl enable xxx来设置自启动。

    [root@iz2zeda8uhrd6yloi1xs0dz ~]# systemctl enable nginx
    Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
    [root@iz2zeda8uhrd6yloi1xs0dz ~]# systemctl enable mysqld
    Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /usr/lib/systemd/system/mysqld.service.
    [root@iz2zeda8uhrd6yloi1xs0dz ~]# systemctl enable php-fpm
    Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
    [root@iz2zeda8uhrd6yloi1xs0dz ~]# systemctl enable redis  
    Created symlink from /etc/systemd/system/multi-user.target.wants/redis.service to /usr/lib/systemd/system/redis.service.
    

    用netstat -ntlp 可以查看哪些端口被占用,即服务启动

    [root@iz2zeda8uhrd6yloi1xs0dz ~]# netstat -tlnp
    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
    tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      10656/redis-server  
    tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      15564/nginx: master 
    tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1127/sshd           
    tcp        0      0 0.0.0.0:88              0.0.0.0:*               LISTEN      15564/nginx: master 
    tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      15576/php-fpm: mast 
    tcp6       0      0 :::88                   :::*                    LISTEN      15564/nginx: master 
    tcp6       0      0 :::3306                 :::*                    LISTEN      3156/mysqld         
    

    相关文章

      网友评论

        本文标题:centos7.4 yum安装nginx+php7.1+mysq

        本文链接:https://www.haomeiwen.com/subject/dauznftx.html