1 .部署LNMP环境:
1.1–原理
目前的网站一般都会有动态和静态数据,默认nginx仅可以处理静态数据,用户访问任何数据都是直接返回对应的文件,如果如果访问的是一个脚本的话,就会导致直接返回一个脚本给用户,而用户没有脚本解释器,也看不懂脚本源代码!因此需要整合LNMP(Linux、Nginx、MySQL、PHP)实现动态网站效果。
1.2–一些介绍
LNMP在CentOS系统中,源码安装Nginx,使用RPM包安装MariaDB、PHP、PHP-FPM软件.
操作过程中需要安装的软件列表如下:
nginx
mariadb、mariadb-server、mariadb-devel
php、php-fpm、php-mysql
备注:mariadb(数据库客户端软件)、mariadb-server(数据库服务器软件)、mariadb-devel(其他客户端软件的依赖包)、php(解释器)、php-fpm(进程管理器服务)、php-mysql(PHP的数据库扩展包)。
1.3—安装步骤:
1.3.1
使用yum安装基础依赖包
yum -y install gcc openssl-devel pcre-devel
1.3.2
源码安装Nginx(如果前面课程中已经安装Nginx,则忽略这一步).
1.3.3–安装MariaDB:
yum -y install mariadb mariadb-server mariadb-deve
1.3.4–安装php和php-fpm
yum -y install php php-mysql
yum -y install php-fpm
1.4–启动服务
1.4.1–启动Nginx服务(这里需要注意的是,如果服务器上已经启动了其他监听80端口的服务软件(如httpd),则需要先关闭该服务,否则会出现冲突。)
/usr/local/nginx/sbin/nginx //启动Nginx服务
/usr/local/nginx/sbin/nginx -s stop //关闭Nginx服务
/usr/local/nginx/sbin/nginx -s reload //重新加载配置(更改Nginx主配置文件后需要执行的命令.
netstat -utnlp | grep :80 //查看服务
1.4.2–启动Mysql
systemctl start mariadb //启动服务
systemctl status mariadb //查看服务运行状态
systemctl enable mariadb //设置开机启动
1.4.3–启动PHP&PHP-FPM
systemctl start php-frm //启动服务
systemctl status php-frm //查看服务运行状态
systemctl enable php-frm //设置开机启动
1.5:创建LNMP动态网站平台
1.5.1–目标
配置Fast-CGI支持PHP网页
创建PHP测试页面,测试使用PHP连接数据库的效果
1.5.2–步骤
1.5.2.1–查看php-fpm配置文件
vim /etc/php-fpm.d/www.conf #配置文件地址
[www]
listen = 127.0.0.1:9000 //PHP端口号
pm.max_children = 32 //最大进程数量
pm.start_servers = 15 //最小进程数量
1.5.2.2–修改Nginx配置文件并启动服务
vim /usr/local/nginx/conf/nginx.conf
location / {
root html;
index index.php index.html index.htm;
#设置默认首页为index.php,当用户在浏览器地址栏中只写域名或IP,不说访问什么页面时,服务器会把默认首页index.php返回给用户
}
location ~\ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000; #将请求转发给本机9000端口,PHP解释器
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME
d
o
c
u
m
e
n
t
r
o
o
t
document_root
documentrootfastcgi_script_name;
include fastcgi.conf; #加载其他配置文件
}
1.5.2.3–创建PHP页面,测试LNMP架构能否解析PHP页面
vim /usr/local/nginx/html/test.php
firefox http://192.168.4.5/test.php
1.5.2.4–LNMP常见问题
Nginx的默认访问日志文件为/usr/local/nginx/logs/access.log
Nginx的默认错误日志文件为/usr/local/nginx/logs/error.log
PHP默认错误日志文件为/var/log/php-fpm/www-error.log
如果动态网站访问失败,可用参考错误日志,查找错误信息。
1.6–地址重写
—用户访问某一域名地址时,重定向另外一地址.例如用户输入www.360buy.com---->www.jd.com.其中的原因可能是某公司更换了新地址,对于熟知老地址的用户的一种照顾措施.
1.6.1–方案:
关于Nginx服务器的地址重写,主要用到的配置参数是rewrite:
rewrite regex replacement flag
格式-----rewrite 旧地址 新地址 [选项]
1.6.2–步骤:
1.6.2.1—修改配置文件(访问a.html重定向到b.html)
vim /usr/local/nginx/conf/nginx.conf
… …
server {
listen 80;
server_name localhost;
rewrite /a.html /b.html; #此处的a网页可以没有,因为最终都被重定向到b网页
location / {
root html;
index index.html index.htm;
}
}
echo “BB” > /usr/local/nginx/html/b.html
1.6.2.2—重新加载Nginx配置
/usr/local/nginx/sbin/nginx -s reload
1.6.3–访问a.html重定向到b.html(跳转地址栏)
1.6.3.1–修改Nginx服务配置:
vim /usr/local/nginx/conf/nginx.conf
… …
server {
listen 80;
server_name localhost; #服务主机名(主机域名)
rewrite /a.html /b.html redirect;
location / {
root html;
index index.html index.htm;
}
}
1.6.3.2–重新加载配置文件
/usr/local/nginx/sbin/nginx -s reload
1.6.3.2–客户端测试(仔细观察浏览器地址栏的变化)
firefox http://192.168.4.5/a.html
1.7—修改配置文件(访问192.168.4.5的请求重定向至www.tmooc.cn)
1.7.1–修改Nginx服务配置
vim /usr/local/nginx/conf/nginx.conf
… …
server {
listen 80;
server_name localhost;
rewrite ^/ http://www.tmooc.cn/;
location / {
root html;
index index.html index.htm;
#rewrite /a.html /b.html redirect;
}
}
1.7.2–重新加载配置文件
/usr/local/nginx/sbin/nginx -s reload
1.7.3–客户端测试(真实机测试,真实机才可以连接tmooc)
firefox http://192.168.4.5
1.8—修改配置文件(访问192.168.4.5/下面子页面,重定向至www.tmooc.cn/下相同的页面)
1.8.1–修改Nginx服务配置
vim /usr/local/nginx/conf/nginx.conf
… …
server {
listen 80;
server_name localhost;
rewrite ^/(.)$ http://www.tmooc.cn/KaTeX parse error: Expected 'EOF', got '}' at position 65: …l index.htm; } }̲ 1.8.2--重新加载配置文…http_user_agent ~ firefox) { //识别客户端firefox浏览器
rewrite ^(.*)$ /firefox/$1;
}
}
1.9.3–重新加载配置文件
/usr/local/nginx/sbin/nginx -s reload
1.9.4–客户端测试
firefox http://192.168.4.5/test.html
curl http://192.168.4.5/test.html
2.总结:
地址重写格式【总结】
rewrite 旧地址 新地址 [选项];
last 不再读其他rewrite
break 不再读其他语句,结束请求
redirect 临时重定向
permanent 永久重定向