LNMP环境搭建
- Linux部署(略)
- Nginx部署
- php-fpm部署
- mysql部署
Linux部署(略)
Nginx部署
Nginx官网:http://www.nginx.org
Ngnix版本类型
- Mainline version:主线版,即开发版
- Stable version:最新稳定版,生产环境上建议使用的版本
- legacy versions:遗留的老版本的稳定版
安装所需工具
[root@hzp ~]# yum -y install yum-utils
配置yum源
[root@hzp ~]# vim /etc/yum.repos.d/nginx.repo
//在yum仓库配置文件中键入下列
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0 //如果想要使用主线版本,可以将此设为1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
安装Nginx
[root@hzp ~]# yum -y install nginx
[root@hzp ~]# systemctl start nginx //开启nginx
[root@hzp ~]# systemctl enable nginx //开机自启动
php-fpm部署
[root@hzp ~]# yum -y install php-fpm php-mysql php-gd
php -fpm:PHP接收动态请求的程序
php-mysql:PHP连接mysql的程序
php-gd:PHP图形库程序(GD库可以处理图片,或者生成图片)
[root@hzp ~]# systemctl restart php-fpm //服务开启后,服务端口为9000
[root@hzp ~]# systemctl enable php-fpm
[root@hzp ~]# vim /etc/nginx/conf.d/default.conf //编辑nginx默认子配置文件
server {
location / {
...
index index.php index.html; //添加默认主页名
...
}
//启动nginx_fastcgi功能,解除#注释修改路径即可
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
//保存退出
[root@hzp ~]# systemctl restart nginx //重启nginx
[root@hzp ~]# vim /usr/share/nginx/html/index.php //写入测试内容
<?php
phpinfo
?>
浏览器访问http://主机IP、看到如下页面表示成功
mysql部署
下载mysql官方yum仓库
[root@hzp html]# wget https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm
[root@hzp html]# yum -y install mysql80-community-release-el7-3.noarch.rpm //安装
[root@hzp html]# vim /etc/yum.repos.d/mysql-community.repo //配置yum仓库
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1 //将5.7版本开启
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/
enabled=0 //将8.0版本关闭
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
//保存退出
安装mysql
[root@hzp html]# yum -y install mysql-community-server*
[root@hzp html]# systemctl start mysqld
[root@hzp html]# systemctl enable mysqld
[root@hzp html]# cat /var/log/mysqld.log | grep password //查看初始密码
2021-06-13T13:18:13.393607Z 1 [Note] A temporary password is generated for root@localhost: g,p0p>kufPYf //g,p0p>kufPYf为初始密码
修改mysql密码
[root@hzp html]# mysqladmin -uroot -p'g,p0p>kufPYf' password '[email protected]'
[root@hzp html]# mysql -uroot -p'[email protected]' //登录mysql
创建数据库及用户授权
mysql> create database bbs; //创建数据库
mysql> grant all on bbs.* to phptest@'192.168.100.%' identified by '[email protected]'; //创建phptest用户并赋予bbs库权限
mysql> flush privileges; //刷新数据库
测试数据库与php的连接状况
[root@hzp ~]# vim /usr/share/nginx/html/index.php //重新编写测试文件
<?php
$link=mysql_connect('192.168.100.10','phptest','123456');
if($link)
echo "Successfuly";
else
echo "False";
mysql_close();
?>
浏览器访问http://服务器IP,出现Successfuly表示成功,如果显示False,请检查授权