第三十四天-三十五天作业

第三十四天-三十五天作业

0 当打开web.it.com 自动跳转到 www.baidu.com 实战演示 工作肯定会用到重点
^	指定匹配字符串开始
$	指定匹配字符串结束
.	任意非空字符
+	量词,匹配1次或多次
*	匹配0次或多次
[]	匹配括号中的任意一项
?	量词,匹配0次或1次
()	作为一个整体匹配






需求1 打开网站跳转到百度
server {
        listen       80;
        server_name  www.it.com it.com localhost ;
        access_log  /data/logs/www.log  main;
        rewrite ^/(.*) https://www.baidu.com/$1 permanent;



需求2 打开网站跳转到https协议

http: 80

https:443





#步骤

#tips:需了解字符

^	指定匹配字符串开始
$	指定匹配字符串结束
.	任意非空字符
+	量词,匹配1次或多次
*	匹配0次或多次
[]	匹配括号中的任意一项
?	量词,匹配0次或1次
()	作为一个整体匹配



#需求1 打开网站跳转到百度

1.#配置nginx网站配置目录

cd /usr/local/nginx/conf/vhost

vi www.conf


server {
        listen       80;
        server_name  www.it.com it.com localhost ;
        access_log  /data/logs/www.log  main;
        rewrite ^/(.*) https://www.baidu.com/$1 permanent;   #主要添加的就是这一行


2.#检查-重启

nginx -t
nginx -s reload

nginx -s stop
nginx



tips:打开网站看跳不跳转百度页面

www.it.com



#tips:重定向脚本注解


这段配置指令来自于 Nginx 服务器的重写模块(rewrite module),它的作用是将接收到的请求地址进行重定向。具体来说:

nginx
rewrite ^/(.*) https://www.baidu.com/$1 permanent;


- rewrite:指令关键字,用于在Nginx内部执行URL的重写操作。

- ^/(.*):正则表达式匹配部分,这里的 ^ 表示匹配URL的开始,(.*) 是一个捕获组,它会匹配任何非空字符序列(除了换行符),也就是说,它会匹配从URL开始到结束的所有路径部分。

- https://www.baidu.com/$1:重写的目标地址,其中 $1 是对前面正则表达式中第一个捕获组内容的引用,这意味着原URL中的任何路径都将被复制并粘贴到目标地址的对应位置。

- permanent:重写类型,表示此次重写应以301 Moved Permanently(永久重定向)HTTP状态码的形式告知客户端浏览器,搜索引擎和其他服务这次重定向是永久性的,以便它们可以更新索引和缓存。

因此,这条规则的工作原理是,当Nginx接收到任何以服务器域名开头的请求时,它都会将请求的完整路径(包括查询字符串)重定向到 https://www.baidu.com/ 加上前缀的相同路径。










#需求2 打开网站跳转到https协议
需求解释:就是原先的http80端口不安全,需要把http80端口跳转到安全的https协议443端口,所以


http: 80            

https:443

(需要ssl的证书)


1.#配置nginx网站配置目录

cd /usr/local/nginx/conf/vhost

vi www.conf


server {
        listen       80;
        server_name  www.it.com it.com localhost ;
        access_log  /data/logs/www.log  main;
        rewrite ^/(.*) https://www.baidu.com/$1 permanent;   #主要添加的就是这一行


2.#检查-重启

nginx -t
nginx -s reload

nginx -s stop
nginx



#tips:一般掌握重定向就可以了,https这个都是在生产环境中成型的,不太需要配置,除非新上线的网站,域名需要配置https















#总结:为什么需要重定向?什么场合?


1.网站迁移或改版: 当网站的域名发生变化、或者网站结构调整(例如从HTTP迁移到HTTPS,或者目录结构变动),可以通过重定向将旧网址引导到新网址,保持用户访问的连贯性,同时也利于搜索引擎更新索引,减少因网址变更带来的流量损失。

2.URL规范化: 为了统一网站的访问入口,可以设置重定向规则,例如去除URL末尾的斜杠“/”、转换大小写字母、去掉多余的参数等,以提升SEO效果及用户体验。

3.维护公告或临时关闭: 当网站进行临时维护时,可以将所有访问重定向到一个公告页面,通知用户网站目前不可用,并给出预计恢复时间。

4.移动端适配: 对于响应式设计之外的网站,可以针对不同设备或分辨率进行重定向,例如将手机和平板用户的访问重定向到专门优化过的移动版网站。

5.清除会话或登录状态: 当用户注销账户后,可以通过重定向将其导航到首页或者其他预设页面,确保用户在注销后不会停留在含有敏感信息的页面。



效果如下

https://images.cnblogs.com/cnblogs_com/blogs/816085/galleries/2387059/t_240325104606_重定向.png

1 测试环境任务学习: 使用yum 安装lnmp 发布论坛
#步骤
tips:安装时如果没有依赖包,这时就要更新一下yum源了

没有更新yum源的,可以更新一下

1. 备份
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

2. 下载新的 CentOS-Base.repo 到 /etc/yum.repos.d/

2.1各版本
centos8

wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
或者
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo


centos6
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-6.10.repo
或者
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-6.10.repo


CentOS 7
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
或者
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo



3.各版本epel源

3.1 备份(如有配置其他epel源)
mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup
mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup

3.2 下载新repo 到/etc/yum.repos.d/

epel(RHEL 8)
1)安装 epel 配置包

yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm

2)将 repo 配置中的地址替换为阿里云镜像站地址
sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*



epel(RHEL 7)

wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo


epel(RHEL 6) (epel6官方源已下线,建议切换epel-archive源)

wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-archive-6.repo



4.更新缓存和软件
yum clean all
yum makecache
yum update


tips:建议在这里设置一下虚拟机快照,不然每次都要这样,会很耽误时间









#1.  安装nginx 
yum install epel-release -y
yum install nginx -y
[root@node-1 ~]# systemctl start nginx.service
[root@node-1 ~]# systemctl status nginx.service  看一下状态
[root@node-1 ~]# systemctl enable nginx.service   开机自启动

#2. 安装开发包
yum -y install ntp make openssl openssl-devel pcre pcre-devel libpng libpng-devel libjpeg-6b libjpeg-devel-6b freetype freetype-devel gd gd-devel zlib zlib-devel gcc gcc-c++ libXpm libXpm-devel ncurses ncurses-devel libmcrypt libmcrypt-devel libxml2 libxml2-devel imake autoconf automake screen sysstat compat-libstdc++-33 curl curl-devel

#3.  安装数据库

# wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
#rpm -ivh mysql-community-release-el7-5.noarch.rpm
# yum -y install mysql-community-server


[root@node-1 ~]#systemctl start mysql
[root@node-1 ~]#systemctl status mysql     看一下状态
[root@node-1 ~]# systemctl start mysql.service  设置自启动



#3.1设置MySQL root账户的密码
tips:如果你有就不用设置了


为root账户设置密码
[root@node-1 ~]#mysql_secure_installation  #初始化MySQL
Enter current password for root (enter for none):   <---输入现在的root密码,因为我们还没设置,直接回车
Set root password? [Y/n] Y                                    <---是否设定root密码,当然设置了,输入Y回车
New password:                                                      <---输入root密码,并回车,输入的过程中不会有任何显示
Re-enter new password:                                        <---再次输入root密码,并回车,输入的过程中不会有任何显示
Remove anonymous users? [Y/n] Y                      <---是否删除匿名用户,删除,输入Y回车
Disallow root login remotely? [Y/n] Y                     <---是否删禁止root用户远程登录,当然禁止,输入Y回车
Remove test database and access to it? [Y/n]      <---是否删除测试数据库test,看个人喜好
Reload privilege tables now? [Y/n] Y                    <---刷新权限,输入Y回车
最后出现:Thanks for using MySQL!
MySql密码设置完成,重新启动 MySQL:


tips:数据库密码一般设置就比较复杂的,在生产环境中,不过这里就不设置复杂的了,直接123

#4. 安装php
 yum -y install php php-cli php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-mssql php-snmp php-soap




#4.2  安装php扩展支持mysql 
 [root@node-1 ~]# yum install  php-tidy php-common php-devel php-fpm php-mysql -y
[root@node-1 ~]# systemctl start php-fpm.service
[root@node-1 ~]# systemctl status php-fpm.service
[root@node-1 ~]# systemctl enable php-fpm.service   设置自启动




tips:有一个9000端口代表php启动了
[root@node-1 www]# ss -tulpn
Netid State      Recv-Q Send-Q      Local Address:Port                     Peer Address:Port              
tcp   LISTEN     0      128             127.0.0.1:9000                                *:*           




#4.1配置nginx
 
进入nginx 配置文件目录
tips:由于我们是yum安装的,所以配置文件都在etc下面,如果这个conf.d这个文件夹,可以新建一下,再进去创建www.conf配置文件,将内容填充进去



cd /etc/nginx/conf.d

vi www.conf 添加如下


server {
       listen        80;
       server_name   www.it.com;
       location  / {
         root  /html/www;         #发布目录                                                                                                   
         index index.html index.php;
       }
location ~ \.php$ {
 fastcgi_pass   127.0.0.1:9000;
 fastcgi_index  index.php;
fastcgi_param SCRIPT_FILENAME /html/www$fastcgi_script_name;                                      #修改成发布目录
include        fastcgi_params;        
}
    }
    
    
    
#可能会遇到的问题 
如果多域名配置没有打开 ,在这/etc/nginx/conf.d新建配置是不会生效的 

在 vi /etc/nginx/nginx.conf配置文件需要有这个配置文件才会生效 检查下
include /etc/nginx/conf.d/*.conf;
配置文件详细内容
cd /etc/nginx/
vim nginx.conf




[root@node-1 nginx]# vim nginx.conf

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;    重点就是这个位置





#5.发布测试页测试是否ok
tips:如果没有这个目录自己新建一下mkdir -p /html/www

cd  /html/www

vi index.php

将以下内容填充进去


<?php 
        phpinfo(); 
?>









效果图如下

img

2 工作任务:使用LNMP 编译环境 发布 论坛
环境:虚拟机

ip:10.0.1.0

网关:10.0.1.2

子网掩码:255.255.255.0

测试机器ip:10.0.1.102

tips:机器不可以有nginx,所以这里建议你重新开个新机器或者克隆一个模版机 




#步骤
tips:安装时如果没有依赖包,这时就要更新一下yum源了

没有更新yum源的,可以更新一下

1. 备份
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

2. 下载新的 CentOS-Base.repo 到 /etc/yum.repos.d/

2.1各版本
centos8

wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
或者
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo


centos6
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-6.10.repo
或者
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-6.10.repo


CentOS 7
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
或者
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo



3.各版本epel源

3.1 备份(如有配置其他epel源)
mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup
mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup

3.2 下载新repo 到/etc/yum.repos.d/

epel(RHEL 8)
1)安装 epel 配置包

yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm

2)将 repo 配置中的地址替换为阿里云镜像站地址
sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*



epel(RHEL 7)

wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo


epel(RHEL 6) (epel6官方源已下线,建议切换epel-archive源)

wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-archive-6.repo



4.更新缓存和软件
yum clean all
yum makecache
yum update


tips:建议在这里设置一下虚拟机快照,不然每次都要这样,会很耽误时间








编译安装--lnmp
#0. 安装开发包
yum -y install ntp make openssl openssl-devel pcre pcre-devel libpng libpng-devel libjpeg-6b libjpeg-devel-6b freetype freetype-devel gd gd-devel zlib zlib-devel gcc gcc-c++ libXpm libXpm-devel ncurses ncurses-devel libmcrypt libmcrypt-devel libxml2 libxml2-devel imake autoconf automake screen sysstat compat-libstdc++-33 curl curl-devel

1.1#安装一下gcc编译工具和依赖
yum -y install pcre-devel  openssl-devel zlib-devel gcc
1.2#创建用户组和设置此用户组不可以登录(如果你有了,就不用创建了)
groupadd nginx
useradd -g nginx -s /sbin/nologin nginx
1.3#解压一下(一般你自己选个解压目录,便于管理和配置)
切换到解压后的nginx目录中执行:
cd /usr/local/src
wget http://nginx.org/download/nginx-1.18.0.tar.gz

解压压缩包
tar zxvf nginx-1.18.0.tar.gz    (解压)

tar zcvf nginx-1.18.0.tar.gz   (压缩)


1.4#编译一下他的用户和所属组以及配置安装路径--生成马克file文件
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module



注解:
您提供的命令是对 ./configure 脚本的调用,该脚本通常位于采用 GNU Autotools 构建系统的软件项目源代码目录中。这个脚本用于为项目的编译和安装做前期准备,根据用户提供的配置选项生成相应的 Makefile 文件。具体到您给出的命令:

[root@node-1 ~]
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx \
--with-http_ssl_module --with-http_stub_status_module


各个选项含义如下:

- --user=nginx: 指定编译安装后,Nginx 服务运行时所属的系统用户为 nginx。这意味着当 Nginx 服务启动时,将以 nginx 用户的身份运行。

- --group=nginx: 指定编译安装后,Nginx 服务运行时所属的系统组为 nginx。这意味着当 Nginx 服务启动时,将以 nginx 组的成员身份运行。

- --prefix=/usr/local/nginx: 设置 Nginx 的安装目录为 /usr/local/nginx。这意味着编译完成后,Nginx 的可执行文件、配置文件、库文件、文档等将被安装到这个路径下。例如,Nginx 主程序通常会被安装到 /usr/local/nginx/sbin/nginx,配置文件则位于 /usr/local/nginx/conf/nginx.conf。

- --with-http_ssl_module: 启用 SSL/TLS 支持模块。配置此项后,编译出的 Nginx 将具备处理 HTTPS 请求的能力,允许网站提供安全的加密通信。

- --with-http_stub_status_module: 启用 stub_status 模块。该模块提供了一个简单的 HTTP 接口,可以用来查询 Nginx 服务器的基本状态信息,如当前活动连接数、处理过的请求总数等。这对于监控 Nginx 服务器的运行状态非常有用。

执行以上命令后,./configure 脚本将检查系统环境,确认依赖项是否满足,并根据指定的选项生成相应的 Makefile。接下来,用户通常会执行 make 命令编译源代码,然后执行 make install 命令将编译好的程序安装到指定的位置。这样,一个带有 SSL 支持和 stub_status 模块的 Nginx 服务器就安装完成了,并将以 nginx 用户和组的身份运行。



make  

make install  

启动nginx:/usr/local/nginx/sbin/nginx
关闭nginx:/usr/local/nginx/sbin/nginx -s stop
检查nginx 是否异常:/usr/local/nginx/sbin/nginx  -t

重启nginx: nginx  -s reload




1.5#定义一下软连接(提高效率)
每次都需要输入路径才可以重启 检查等操作比较繁琐

用软连接定义
ln -s /usr/local/nginx/sbin/nginx /bin/nginx

这样就可以了

启动  : nginx 
停止:nginx -t stop
重启 :  nginx -s reload
检查:   nginx -t








1.6 #编译php环境

cd /usr/local/src/
rz命令上传
#tips:这里上传php的源码包
tar zxvf php-7.2.29.tar.gz 

cd ./php-7.3.9


#tips:编译安装
./configure --prefix=/usr/local/php --exec-prefix=/usr/local/php --with-mysqli --with-pdo-mysql --with-gd --bindir=/usr/local/php/bin --sbindir=/usr/local/php/sbin --includedir=/usr/local/php/include --libdir=/usr/local/php/lib/php --mandir=/usr/local/php/php/man --with-config-file-path=/usr/local/php/etc --with-openssl --enable-mbstring --enable-fpm


make && make install    #编译 编译安装



cp php.ini-production /usr/local/php/etc/php.ini
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
cp /usr/local/php/etc/php-fpm.conf.default  /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default  /usr/local/php/etc/php-fpm.d/www.conf
ln -s /usr/local/php/bin/php /usr/local/bin
echo -e "\033[31m php部署成功 \033[0m"


启动php: /etc/init.d/php-fpm start   

使用netstat -tuln 查看 如果有 9000 端口就是正常 


1.7 安装数据库

#tips:这里就用这个yum安装吧 ,编译MySQL耗时间

wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
 yum -y install mysql-community-server



[root@node-1 ~]#systemctl start mysql
[root@node-1 ~]#systemctl status mysql     看一下状态
[root@node-1 ~]# systemctl start mysql.service  设置自启动

ss -tulpn |grep mysql

数据库接口 3306


为root账户设置数据库密码
[root@node-1 ~]#mysql_secure_installation  #初始化MySQL
Enter current password for root (enter for none):   <---输入现在的root密码,因为我们还没设置,直接回车
Set root password? [Y/n] Y                                    <---是否设定root密码,当然设置了,输入Y回车
New password:                                                      <---输入root密码,并回车,输入的过程中不会有任何显示
Re-enter new password:                                        <---再次输入root密码,并回车,输入的过程中不会有任何显示
Remove anonymous users? [Y/n] Y                      <---是否删除匿名用户,删除,输入Y回车
Disallow root login remotely? [Y/n] Y                     <---是否删禁止root用户远程登录,当然禁止,输入Y回车
Remove test database and access to it? [Y/n]      <---是否删除测试数据库test,看个人喜好
Reload privilege tables now? [Y/n] Y                    <---刷新权限,输入Y回车
最后出现:Thanks for using MySQL!
MySql密码设置完成,重新启动 MySQL:


tips:数据库密码一般设置就比较复杂的,在生产环境中,不过这里就不设置复杂的了,直接123


mysql -uroot -p"123"     #创建一个root账户的数据库密码


create database  wordpress;    #新建一个数据库

show databases;    #查看一下是否存在




1.8#修改nginx配置文件
cd /usr/local/nginx/conf
打开 多域名配置如下:

vi /usr/local/nginx/conf/nginx.conf
vim nginx.conf
参考这个配置文件

[root@web3 conf]#cat nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       81;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
include /usr/local/nginx/conf/vhost/*.conf; 
}

 

重启nginx 
在cd /usr/local/nginx/conf/vhost 目录新建配置文件
 
vi www.conf 添加如下

  server {
       listen        80;
       server_name   www.it.com localhost;
       location  / {
         root  /html/www;
         index index.html index.php;
       }


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





    }



nginx -t   检查一下
nginx  -s relaod 重启启动nginxls



#!/bin/[root@node-1 ~]
yum install -y gcc gcc-c++ make zlib zlib-devel pcre pcre-devel
yum install -y libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel
yum install -y  glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel 
yum install -y e2fsprogs e2fsprogs-devel krb5 krb5-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers




1.9 # 检查测试一下php

vi index.php

<?php 
        phpinfo(); 
?>


nginx -t
nginx -s reload
或者  

 /usr/local/nginx/sbin/nginx -s reload
 
 
 
查看index.php页面显示
http://10.0.1.102




2.0 #测试php和mysql是否成功测试

vim test_mysql.php
       <?php
        $servername = "localhost";
        $username = "root";
        $password = "oldboy123";
        //$link_id=mysql_connect('主机名','用户','密码');
        //mysql -u用户 -p密码 -h 主机
        $conn = mysqli_connect($servername, $username, $password);
        if ($conn) {
              echo "mysql successful by root !\n";
           }else{
              die("Connection failed: " . mysqli_connect_error());
           }
       ?>
       
       
       
       

Nginx访问PHP文件的File not found错误处理,两种情况
转载 2017年05月09日 14:05:27
标签:
php /
nginx
这个错误很常见,原有有下面两种几种
php-fpm找不到SCRIPT_FILENAME里执行的php文件
php-fpm不能访问所执行的php,也就是权限问题
第一种情况
更改配置文件nginx.conf 
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 
替换成下面
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
然后重新加载nginx配置文件 
/etc/init.d/nginx reload










#tips:如果你开启了多域名的话,可能出现你的ip只会访问某一个发布目录下的网页,因为我们在主配置文件里设置了多域名引用,那么多域名引用,你用ip去访问只能显示一个,所以我么需要设置域名解析,来帮助我们识别每个网站。
如果你只能用ip访问,就需要站是把其他网站的配置文件删掉,做个备份,只用一个测试网站的配置文件





发布一个博客


1.#切换到发布目录下
cd /html/www
rz  上传你的源码包


tar 格式
tar zxvf 源码包      解压
tar zcvf 源码包      压缩

zip 格式
unzip  源码包



#tips1:上传的是zip格式的,就需要安装yum -y install unzip 来解压
#tips2:不要忘了你的nginx配置目录要配置正确的发布目录,否则会报403错误


2.配置nginx.conf ---发布目录
cd /usr/local/nginx/conf/vhost/
ls
vim www.conf


3.安装论坛  
tips:如果输入ip地址或者域名无法跳转的的话,那就复制下面我这段地址,把其他的ip地址替换你自己的就可以了或者域名



http://10.0.1.102/upload/install/

chmod -R 777 upload/      #给一下权限,如果不给的话,目录文件权限会检查到的,安装会出问题






#温馨小提示

如果出现无法连接数据库,在数据库服务器哪里输入127.0.0.1,就可以了




效果如下图

img

3 Nginx配置,访问www.abc.com/refuse,禁止访问,返回403
#步骤

环境:虚拟机

ip:10.0.1.0

网关:10.0.1.2

子网掩码:255.255.255.0

测试机器ip: 10.0.1.134








1.打开网站配置目录
cd /usr/local/nginx/conf/vhost/
vim www.conf


server {
       listen        80;
       server_name   www.it.com www.abc.com localhost;
       location  /refuse {         #主要就是这两段
        return 403;
         root  /html/www;
         index index.html index.php;
       }


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





    }



注解:
location  /refuse {        
        return 403;
        
 #主要就是这两段,设置这个域名,返回一个403,只要又问访问这个域名,就会403,其他不影响


2.重启--测试

nginx -t
nginx -s reload
nginx -s stop
nginx


http://10.0.1.134/  #正常访问页面
http://10.0.1.134/refuse     #禁止访问页面





结果如下图

https://images.cnblogs.com/cnblogs_com/blogs/816085/galleries/2387059/t_240325105239_refuse禁止访问403.png

其他页面不影响

https://images.cnblogs.com/cnblogs_com/blogs/816085/galleries/2387059/t_240325105703_10.0.1.134正常.png

4 创建数据库管理用户: it 密码为Qwe123456
测试机器:10.0.1.134


在CentOS 7中,可以通过以下步骤创建数据库管理用户:

1. 首先,确保已经安装了MySQL数据库。如果没有安装,可以使用以下命令进行安装:
  
   sudo yum install mysql-server
  

2. 安装完成后,启动MySQL服务:
  
   sudo systemctl start mysqld
  

3. 运行MySQL安全脚本来提高安全性并设置root密码:
  
   sudo mysql_secure_installation
  

4. 进入MySQL命令行界面:
  
   mysql -u root -p "123"   #tips:这里我之前自己设置了密码
   mysql -uroot -p"123"  

5. 在MySQL命令行界面中,创建一个新的数据库管理用户,并设置密码:
  
   CREATE USER 'it'@'localhost' IDENTIFIED BY 'Qwe123456';
  

6. 授予该用户所有数据库的管理权限:
  
   GRANT ALL PRIVILEGES ON *.* TO 'it'@'localhost';
  

7. 刷新权限使更改生效:
  
   FLUSH PRIVILEGES;
  

现在,你已经成功创建了一个名为"it"的数据库管理用户,并设置了密码为"Qwe123456"。该用户具有对所有数据库的管理权限。


8.退出--测试

如果你想测试可不可以登录的话,exit退出原先的用户,重新以刚刚创建的用户登录




mysql -uit -p"Qwe123456"

登录进去就成功了


如下
[root@master vhost]# mysql -uit -p"Qwe123456"
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 64
Server version: 5.6.51 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| ultrax             |
| wordpress          |
+--------------------+
5 rows in set (0.00 sec)

mysql> 


5 演示自己在自己搭建的博客 发布一个博文上传一个图片 然后服务器找到那个图片在哪里
环境:虚拟机

ip:10.0.1.0

网关:10.0.1.2

子网掩码:255.255.255.0

测试机器ip: 10.0.1.134


tips:机器不可以有nginx,所以这里建议你重新开个新机器或者克隆一个模版机 




#步骤
tips:安装时如果没有依赖包,这时就要更新一下yum源了

没有更新yum源的,可以更新一下

1. 备份
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

2. 下载新的 CentOS-Base.repo 到 /etc/yum.repos.d/

2.1各版本
centos8

wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
或者
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo


centos6
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-6.10.repo
或者
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-6.10.repo


CentOS 7
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
或者
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo



3.各版本epel源

3.1 备份(如有配置其他epel源)
mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup
mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup

3.2 下载新repo 到/etc/yum.repos.d/

epel(RHEL 8)
1)安装 epel 配置包

yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm

2)将 repo 配置中的地址替换为阿里云镜像站地址
sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*



epel(RHEL 7)

wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo


epel(RHEL 6) (epel6官方源已下线,建议切换epel-archive源)

wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-archive-6.repo



4.更新缓存和软件
yum clean all
yum makecache
yum update


tips:建议在这里设置一下虚拟机快照,不然每次都要这样,会很耽误时间








编译安装--lnmp
#0. 安装开发包
yum -y install ntp make openssl openssl-devel pcre pcre-devel libpng libpng-devel libjpeg-6b libjpeg-devel-6b freetype freetype-devel gd gd-devel zlib zlib-devel gcc gcc-c++ libXpm libXpm-devel ncurses ncurses-devel libmcrypt libmcrypt-devel libxml2 libxml2-devel imake autoconf automake screen sysstat compat-libstdc++-33 curl curl-devel

1.1#安装一下gcc编译工具和依赖
yum -y install pcre-devel  openssl-devel zlib-devel gcc
1.2#创建用户组和设置此用户组不可以登录(如果你有了,就不用创建了)
groupadd nginx
useradd -g nginx -s /sbin/nologin nginx
1.3#解压一下(一般你自己选个解压目录,便于管理和配置)
切换到解压后的nginx目录中执行:
cd /usr/local/src
wget http://nginx.org/download/nginx-1.18.0.tar.gz

解压压缩包
tar zxvf nginx-1.18.0.tar.gz    (解压)

tar zcvf nginx-1.18.0.tar.gz   (压缩)


1.4#编译一下他的用户和所属组以及配置安装路径--生成马克file文件
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module



注解:
您提供的命令是对 ./configure 脚本的调用,该脚本通常位于采用 GNU Autotools 构建系统的软件项目源代码目录中。这个脚本用于为项目的编译和安装做前期准备,根据用户提供的配置选项生成相应的 Makefile 文件。具体到您给出的命令:

[root@node-1 ~]
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx \
--with-http_ssl_module --with-http_stub_status_module


各个选项含义如下:

- --user=nginx: 指定编译安装后,Nginx 服务运行时所属的系统用户为 nginx。这意味着当 Nginx 服务启动时,将以 nginx 用户的身份运行。

- --group=nginx: 指定编译安装后,Nginx 服务运行时所属的系统组为 nginx。这意味着当 Nginx 服务启动时,将以 nginx 组的成员身份运行。

- --prefix=/usr/local/nginx: 设置 Nginx 的安装目录为 /usr/local/nginx。这意味着编译完成后,Nginx 的可执行文件、配置文件、库文件、文档等将被安装到这个路径下。例如,Nginx 主程序通常会被安装到 /usr/local/nginx/sbin/nginx,配置文件则位于 /usr/local/nginx/conf/nginx.conf。

- --with-http_ssl_module: 启用 SSL/TLS 支持模块。配置此项后,编译出的 Nginx 将具备处理 HTTPS 请求的能力,允许网站提供安全的加密通信。

- --with-http_stub_status_module: 启用 stub_status 模块。该模块提供了一个简单的 HTTP 接口,可以用来查询 Nginx 服务器的基本状态信息,如当前活动连接数、处理过的请求总数等。这对于监控 Nginx 服务器的运行状态非常有用。

执行以上命令后,./configure 脚本将检查系统环境,确认依赖项是否满足,并根据指定的选项生成相应的 Makefile。接下来,用户通常会执行 make 命令编译源代码,然后执行 make install 命令将编译好的程序安装到指定的位置。这样,一个带有 SSL 支持和 stub_status 模块的 Nginx 服务器就安装完成了,并将以 nginx 用户和组的身份运行。



make  

make install  

启动nginx:/usr/local/nginx/sbin/nginx
关闭nginx:/usr/local/nginx/sbin/nginx -s stop
检查nginx 是否异常:/usr/local/nginx/sbin/nginx  -t

重启nginx: nginx  -s reload




1.5#定义一下软连接(提高效率)
每次都需要输入路径才可以重启 检查等操作比较繁琐

用软连接定义
ln -s /usr/local/nginx/sbin/nginx /bin/nginx

这样就可以了

启动  : nginx 
停止:nginx -t stop
重启 :  nginx -s reload
检查:   nginx -t








1.6 #编译php环境

cd /usr/local/src/
rz命令上传
#tips:这里上传php的源码包
tar zxvf php-7.2.29.tar.gz 

cd ./php-7.3.9


#tips:编译安装
./configure --prefix=/usr/local/php --exec-prefix=/usr/local/php --with-mysqli --with-pdo-mysql --with-gd --bindir=/usr/local/php/bin --sbindir=/usr/local/php/sbin --includedir=/usr/local/php/include --libdir=/usr/local/php/lib/php --mandir=/usr/local/php/php/man --with-config-file-path=/usr/local/php/etc --with-openssl --enable-mbstring --enable-fpm


make && make install    #编译 编译安装



cp php.ini-production /usr/local/php/etc/php.ini
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
cp /usr/local/php/etc/php-fpm.conf.default  /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default  /usr/local/php/etc/php-fpm.d/www.conf
ln -s /usr/local/php/bin/php /usr/local/bin
echo -e "\033[31m php部署成功 \033[0m"


启动php: /etc/init.d/php-fpm start   

使用netstat -tuln 查看 如果有 9000 端口就是正常 


1.7 安装数据库

#tips:这里就用这个yum安装吧 ,编译MySQL耗时间

wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
 yum -y install mysql-community-server



[root@node-1 ~]#systemctl start mysql
[root@node-1 ~]#systemctl status mysql     看一下状态
[root@node-1 ~]# systemctl start mysql.service  设置自启动

ss -tulpn |grep mysql

数据库接口 3306


为root账户设置数据库密码
[root@node-1 ~]#mysql_secure_installation  #初始化MySQL
Enter current password for root (enter for none):   <---输入现在的root密码,因为我们还没设置,直接回车
Set root password? [Y/n] Y                                    <---是否设定root密码,当然设置了,输入Y回车
New password:                                                      <---输入root密码,并回车,输入的过程中不会有任何显示
Re-enter new password:                                        <---再次输入root密码,并回车,输入的过程中不会有任何显示
Remove anonymous users? [Y/n] Y                      <---是否删除匿名用户,删除,输入Y回车
Disallow root login remotely? [Y/n] Y                     <---是否删禁止root用户远程登录,当然禁止,输入Y回车
Remove test database and access to it? [Y/n]      <---是否删除测试数据库test,看个人喜好
Reload privilege tables now? [Y/n] Y                    <---刷新权限,输入Y回车
最后出现:Thanks for using MySQL!
MySql密码设置完成,重新启动 MySQL:


tips:数据库密码一般设置就比较复杂的,在生产环境中,不过这里就不设置复杂的了,直接123


mysql -uroot -p"123"     #创建一个root账户的数据库密码


create database  wordpress;    #新建一个数据库

show databases;    #查看一下是否存在




1.8#修改nginx配置文件
cd /usr/local/nginx/conf
打开 多域名配置如下:

vi /usr/local/nginx/conf/nginx.conf
vim nginx.conf
参考这个配置文件

[root@web3 conf]#cat nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       81;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
include /usr/local/nginx/conf/vhost/*.conf; 
}

 

重启nginx 
在cd /usr/local/nginx/conf/vhost 目录新建配置文件
 
vi www.conf 添加如下

  server {
       listen        80;
       server_name   www.it.com localhost;
       location  / {
         root  /html/www;
         index index.html index.php;
       }


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





    }



nginx -t   检查一下
nginx  -s relaod 重启启动nginxls



#!/bin/[root@node-1 ~]
yum install -y gcc gcc-c++ make zlib zlib-devel pcre pcre-devel
yum install -y libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel
yum install -y  glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel 
yum install -y e2fsprogs e2fsprogs-devel krb5 krb5-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers




1.9 # 检查测试一下php

vi index.php

<?php 
        phpinfo(); 
?>


nginx -t
nginx -s reload
或者  

 /usr/local/nginx/sbin/nginx -s reload
 
 
 
查看index.php页面显示
http://10.0.1.102




2.0 #测试php和mysql是否成功测试

vim test_mysql.php
       <?php
        $servername = "localhost";
        $username = "root";
        $password = "oldboy123";
        //$link_id=mysql_connect('主机名','用户','密码');
        //mysql -u用户 -p密码 -h 主机
        $conn = mysqli_connect($servername, $username, $password);
        if ($conn) {
              echo "mysql successful by root !\n";
           }else{
              die("Connection failed: " . mysqli_connect_error());
           }
       ?>
       
       
       
       

Nginx访问PHP文件的File not found错误处理,两种情况
转载 2017年05月09日 14:05:27
标签:
php /
nginx
这个错误很常见,原有有下面两种几种
php-fpm找不到SCRIPT_FILENAME里执行的php文件
php-fpm不能访问所执行的php,也就是权限问题
第一种情况
更改配置文件nginx.conf 
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 
替换成下面
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
然后重新加载nginx配置文件 
/etc/init.d/nginx reload










#tips:如果你开启了多域名的话,可能出现你的ip只会访问某一个发布目录下的网页,因为我们在主配置文件里设置了多域名引用,那么多域名引用,你用ip去访问只能显示一个,所以我么需要设置域名解析,来帮助我们识别每个网站。
如果你只能用ip访问,就需要站是把其他网站的配置文件删掉,做个备份,只用一个测试网站的配置文件





发布一个博客


1.#切换到发布目录下
cd /html/www
rz  上传你的源码包


tar 格式
tar zxvf 源码包      解压
tar zcvf 源码包      压缩

zip 格式
unzip  源码包



#tips1:上传的是zip格式的,就需要安装yum -y install unzip 来解压
#tips2:不要忘了你的nginx配置目录要配置正确的发布目录,否则会报403错误


2.配置nginx.conf ---发布目录
cd /usr/local/nginx/conf/vhost/
ls
vim www.conf


3.安装博客或者论坛
#tips:如果输入ip地址或者域名无法跳转的的话,那就复制下面我这段地址,把其他的ip地址替换你自己的就可以了或者域名

cd /html/www
rz

http://10.0.1.102/upload/install/

chmod -R 777 upload/      #给一下权限,如果不给的话,目录文件权限会检查到的,安装会出问题






#温馨小提示

如果出现无法连接数据库,在数据库服务器哪里输入127.0.0.1,就可以了




4.找到博客图片--更换图片


4.1查询页面地址(在你需要的图片位置用鼠标停留复制图片地址)

http://10.0.1.134/wordpress/wp-content/themes/twentyseventeen/assets/images/header.jpg

http://10.0.1.134/upload/static/image/common/logo_88_31.gif

4.2根据图片地址推断图片位置
在发布目录下去找,因为你的网站的显示都是在发布目录下

http://10.0.1.134/wordpress/wp-content/themes/twentyseventeen/assets/images/header.jpg
推:cd /html/www/wordpress/wp-content/themes/twentyseventeen/assets/images/
rz  
chmod +x 新图片(给一下权限)

http://10.0.1.134/upload/static/image/common/logo_88_31.gif
推:cd /html/www/upload/static/image/common/
rz
chmod +x 新图片(给一下权限)

4.3测试
打开浏览器测试就可以了
http://10.0.1.134/upload/
http://10.0.1.134/wordpress/



#tips:如果遇到页面图片没有更换成功,这个时候需要清理一下你自己的缓存就可以了










效果如下图

https://images.cnblogs.com/cnblogs_com/blogs/816085/galleries/2387059/t_240325102616_更换1.png

https://images.cnblogs.com/cnblogs_com/blogs/816085/galleries/2387059/t_240325102737_更换2.png

查看mysql 有什么账户列表命令:
SELECT User, Host FROM mysql.user;

select user.host from mysql.user;   


mysql> SELECT User, Host FROM mysql.user;
+------+-----------+
| User | Host      |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1       |
| it   | localhost |
| root | localhost |
+------+-----------+
4 rows in set (0.00 sec)、

mysql> select user.host from mysql.user;
+-----------+
| host      |
+-----------+
| 127.0.0.1 |
| ::1       |
| localhost |
| localhost |
+-----------+
4 rows in set (0.00 sec)



进入数据库命令 mysql -uroot -p
mysql -uit -p"Qwe123456"






具体演示效果
root@master vhost]# mysql -uit -p"Qwe123456"
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 66
Server version: 5.6.51 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SELECT User, Host FROM mysql.user;
+------+-----------+
| User | Host      |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1       |
| it   | localhost |
| root | localhost |
+------+-----------+
4 rows in set (0.00 sec)

mysql> 

补充

在命令行模式中,mysql命令行不分大小写

posted @ 2024-03-25 21:45  三思博客  阅读(4)  评论(0编辑  收藏  举报