学习是一个逐步发现自己无知的过程!

快速搭建LNMP

创建一个工作用户:

useradd -M -s /sbin/nologin www

安装nginx

1.使用Nginx官方提供的rpm包

[root@nginx ~]# cat /etc/yum.repos.d/nginx.repo 
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

2.执行yum安装

[root@nginx ~]# yum install nginx -y
[root@nginx ~]# systemctl start nginx
[root@nginx ~]# systemctl enable nginx

安装php

移除旧版php

[root@nginx ~]# yum remove php-mysql-5.4 php php-fpm php-common

安装扩展源

[root@nginx ~]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

[root@nginx ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

安装php72版本

[root@nginx ~]# yum -y install php72w php72w-cli php72w-common php72w-devel php72w-embedded php72w-gd php72w-mbstring php72w-pdo php72w-xml php72w-fpm php72w-mysqlnd php72w-opcache

启动php

[root@nginx ~]# systemctl start php-fpm
[root@nginx ~]# systemctl enable php-fpm

安装mariadb

下载官方扩展源, 扩展源集成mysql5.6、5.7、8.0,仅5.7仓库是开启

[root@nginx ~]# rpm -ivh http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql57-community-release-el7-10.noarch.rpm

安装并启动

[root@nginx ~]# yum install mysql-community-server -y
[root@nginx ~]# systemctl start mysqld
[root@nginx ~]# systemctl enable mysqld

如果mysql登陆需要密码,请查看该文件

[root@nginx ~]# grep 'temporary password' /var/log/mysqld.log

登陆mysql重新配置密码

[root@nginx ~]# mysql -uroot -p'password'
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';

测试nginx与php

配置nginx实现动态转发请求至php

[root@nginx ~]# cat /etc/nginx/conf.d/php.conf 
server {
        server_name 10.0.1.1;
        listen 80;
        root /soft/code;
        index index.php index.html;

        location ~ \.php$ {
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  /soft/code$fastcgi_script_name;
                include        fastcgi_params;
        }
}

添加php测试页面

//测试phpinfo
[root@nginx ~]# cat /soft/code/info.php
<?php
        phpinfo();
?>

创建mysql测试用户

CREATE USER 'yankerp'@'%' IDENTIFIED BY 'Abc@1234';
说明:
username:你将创建的用户名
host:指定该用户在哪个主机上可以登陆,如果是本地用户可用localhost,如果想让该用户可以从任意远程主机登陆,可以使用通配符%

给mysql测试用户授权

grant all on *.* to yankerp@'%' identified by 'Abc@1234';
说明:
privileges:用户的操作权限,如SELECT,INSERT,UPDATE等,如果要授予所的权限则使用ALL
databasename:数据库名
tablename:表名,如果要授予该用户对所有数据库和表的相应操作权限则可用*表示,如*.*

错误处理!!!执行任何mysql命令都报错

mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

# 解决问题
mysql> alter user 'root'@'localhost' identified by 'Abc@123';
Query OK, 0 rows affected (0.00 sec)

使用mysqli模块测试连接mysql

[root@nginx ~]# cat /soft/code/mysqli.php
        <?php
        $servername = "localhost";
        $username = "yankerp";
        $password = "Abc@1234";
         
        // 创建连接
        $conn = mysqli_connect($servername, $username, $password);
         
        // 检测连接
        if (!$conn) {
            die("Connection failed: " . mysqli_connect_error());
        }
        echo "连接成功";
        ?>

赞(0)
未经允许不得转载:劉大帥 » 快速搭建LNMP

你的评论可能会一针见血! 抢沙发

登录

找回密码

注册