准备环境:
操作系统:CentOS7.9
一、下载阿里云网络yum源和epel扩展源
[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
[root@localhost ~]# wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
[root@localhost ~]# yum makeche && yum clean all
二、安装Nginx
配置Nginx源
[root@localhost ~]# vim /etc/yum.repos.d/nginx.repo
[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
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[root@localhost ~]# yum makecache
[root@localhost ~]# yum -y install nginx
[root@localhost ~]# systemctl enable nginx --now
添加防火墙规则允许nginx服务通过防火墙
[root@localhost ~]# firewall-cmd --add-port=80/tcp --permanent
[root@localhost ~]# firewall-cmd --add-port=443/tcp --permanent
[root@localhost ~]# firewall-cmd --reload
三、部署php-fpm
配置php-fpm源
[root@localhost ~]# yum install https://rpms.remirepo.net/enterprise/remi-release-7.rpm
[root@localhost ~]# yum install yum-utils ## 安装 yum-utils 软件包的命令(用于 yum-config-manager 命令)
启用存储库的命令:
[root@localhost ~]# yum-config-manager --disable 'remi-php*'
[root@localhost ~]# yum-config-manager --enable remi-php82
[root@localhost ~]# yum-config-manager --enable remi-php82
[root@localhost ~]# yum -y install php-fpm php-mysql php-gd
php-fpm: php接受动态请求程序 连接nginx
php-mysql:php连接mysql的程序
php-gd:图形库程序(GD库可以处理图片,或者生成图片)
[root@localhost ~]# systemctl enable php-fpm --now
[root@localhost ~]# netstat -anptu | grep 9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 1163/php-fpm: maste
测试php是否可以正常使用
[root@localhost ~]# vim /usr/share/nginx/html/index.php
<?php
phpinfo( );
?>
[root@localhost ~]# vim /etc/nginx/conf.d/default.conf
启动nginx_fastcgi功能
server {
location / {
...
}
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@localhost ~]# systemctl restart nginx
添加防火墙规则允许php-fpm通过防火墙
[root@localhost ~]# firewall-cmd --add-port=9000/tcp --permanent
[root@localhost ~]# firewall-cmd --reload
访问服务器IP地址加路径:192.168.43.221/index.php
四、部署MySQL
配置MySQL源(再此配置的是mysql5.7版本如有需要mysql8.0版本请看这篇文章***[请复制此链接转到这个网址]***(https://blog.csdn.net/weixin_58131623/article/details/129338575)
[root@localhost ~]#vim /etc/yum.repos.d/mysql.repo
[mysql]
name=mysql
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-5.7-community-el7-x86_64/
enabled=1
gpgcheck=0
[root@localhost ~]# yum -y install mysql-community-server.x86_64
[root@localhost ~]# systemctl enable mysqld --now
添加防火墙规则允许mysql服务通过防火墙
[root@localhost ~]# firewall-cmd --add-port=3306/tcp --permanent
[root@localhost ~]# firewall-cmd --reload
查看mysql默认密码并更改密码
查看mysql默认密码并更改密码:
grep password /var/log/mysqld.log
mysqladin -uroot -p'默认密码/老密码' password '新密码'
准备数据库存放apps
mysql> create database apps;
mysql> grant all on apps.* to phptest@'192.168.1.1'identified by '123123';
#####报错信息如下:
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
解决:
mysql> set global validate_password_policy=0;
mysql> set global validate_password_length=1;
重新授权
mysql> flush privileges;
测试php是否可以连接mysql
[root@localhost ~]# vim /usr/share/nginx/html/index.php
<?php
$link=mysqli_connect('localhost','root','123123'); ##mysql密码
if($link) echo "<h1>mysql connected success ! its ok !!</h1>";
mysqli_close($link);
?>
访问服务器IP地址加路径
192.168.43.221/index.php
到此LNMP就部署完成了接下来部署虚拟主机
LNMP虚拟主机
创建网站主页存放位置并创建页面
[root@localhost ~]# mkdir /usr/share/nginx/html/a/
[root@localhost ~]# vim /usr/share/nginx/html/a/index.php
www.a.com
[root@localhost ~]# mkdir /usr/share/nginx/html/b/
[root@localhost ~]# vim /usr/share/nginx/html/b/index.php
www.b.com
[root@localhost ~]# vim /etc/nginx/conf.d/default.conf
server {
location = /php_status {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ \.php$ {
root /usr/share/nginx/html/a;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
listen 80;
server_name www.a.com;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html/a/;
index index.php index.htm;
}
}
server {
location = /php_status {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ \.php$ {
root /usr/share/nginx/html/b;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
listen 80;
server_name www.b.com;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html/b/;
index index.php index.htm;
}
}
[root@localhost ~]# systemctl restart nginx
因没有dns服务器进行解析所有修改真机hosts文件
192.168.43.221 www.a.com www.b.com
浏览器访问www.a.com www.b.com
LNMP部署和虚拟主机就此完成,祝您好运!