LNMP部署

Top

NSD OPERATION DAY02

  1. 案例1:部署LNMP环境
  2. 案例2:构建LNMP平台
  3. 使用socket方式连接php-fpm
  4. 案例3:地址重写
  5. 地址重写的选项

1 案例1:部署LNMP环境

1.1 问题

安装部署LNMP环境实现动态网站解析

静态网站 在不同环境下访问,网站内容不会变化

动态网站 在不同环境下访问,网站内容有可能发生变化

  • 安装部署Nginx、MariaDB、PHP、PHP-FPM;
  • 启动Nginx、MariaDB、FPM服务;
  • 并测试LNMP是否工作正常。

1.2 方案

目前的网站一般都会有动态和静态数据,默认nginx仅可以处理静态数据,用户访问任何数据都是直接返回对应的文件,如果如果访问的是一个脚本的话,就会导致直接返回一个脚本给用户,而用户没有脚本解释器,也看不懂脚本源代码!网站拓扑如图-1所示。

因此需要整合LNMP(Linux、Nginx、MySQL、PHP)实现动态网站效果。

图1

操作过程中需要安装的软件列表如下:

  • nginx
  • mariadb、mariadb-server、mariadb-devel
  • php、php-fpm、php-mysqlnd

备注:mariadb(数据库客户端软件)、mariadb-server(数据库服务器软件)、mariadb-devel(其他客户端软件的依赖包)、php(解释器)、php-fpm(进程管理器服务)、php-mysqlnd(PHP的数据库扩展包)。

1.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:安装软件

1)使用yum安装基础依赖包

为了不受到之前实验的影响,可以先删除nginx,重安装

 
  1. [root@proxy nginx]#killall nginx #停止nginx程序
  2. [root@proxy nginx]#cd /root/lnmp_soft/nginx-1.22.1
  3. [root@proxy nginx-1.22.1]#rm -rf /usr/local/nginx #删除nginx原有目录
  4. [root@proxy nginx-1.22.1]# yum -y install gcc make openssl-devel pcre-devel
  5. [root@proxy nginx-1.22.1]# ./configure \
  6. > --user=nginx --group=nginx \
  7. > --with-http_ssl_module
  8. [root@proxy nginx-1.22.1]# make && make install
  9. .. ..

3)安装MariaDB

 
  1. [root@proxy ~]# yum -y install mariadb mariadb-server mariadb-devel

4)php和php-fpm

 
  1. [root@proxy ~]# yum -y install php php-mysqlnd
  2. [root@proxy ~]# yum -y install php-fpm

步骤二:启动服务

1)启动Nginx服务

如果服务器上已经启动了其他监听80端口的服务(如httpd),则需要先关闭该服务。

 
  1. [root@proxy ~]# systemctl stop httpd                 #如果该服务存在则关闭该服务
  2. [root@proxy ~]# /usr/local/nginx/sbin/nginx             #启动Nginx服务
  3. [root@proxy ~]# ss -utnlp | grep :80

2)启动MySQL服务

 
  1. [root@proxy ~]# systemctl start mariadb #启动服务器
  2. [root@proxy ~]# systemctl status mariadb #查看服务状态
  3. [root@proxy ~]# systemctl enable mariadb #设置开机启动

3)启动PHP-FPM服务

 
  1. [root@proxy ~]# systemctl start php-fpm #启动服务
  2. [root@proxy ~]# systemctl status php-fpm #查看服务状态
  3. [root@proxy ~]# systemctl enable php-fpm #设置开机启动

4)使用PHP测试页面

 
  1. cp ~/lnmp_soft/php_scripts/test.php /usr/local/nginx/html #拷贝动态网站测试页面到nginx中

使用浏览器访问192.168.99.5/test.php 则无法看到页面内容,而是会当成要下载的文件,因为浏览器无法解析php代码!

2 案例2:构建LNMP平台

2.1 问题

沿用练习一,通过调整Nginx服务端配置,实现以下目标:

  1. 配置Fast-CGI支持PHP网页解析

Fast-CGI是快速公共(通用)网关接口,可以连接如nginx等网站程序到网站的语言解释器(比如php) ,php-fpm进程使用了Fast-CGI解析动态网站页面

  1. 创建PHP测试页面,测试使用PHP连接数据库的效果

2.2 方案

需要延续练习一的实验内容,通过修改Nginx及php-fpm配置文件实现对PHP页面的支持。

2.3 步骤

实现此案例需要按照如下步骤进行。

步骤一: php-fpm配置文件

1)打开php-fpm配置文件,注意该配置文件中;(分号)是注释

 
  1. [root@proxy etc]# vim /etc/php-fpm.d/www.conf
  2. [www]
  3. listen = 127.0.0.1:9000                    #php-fpm端口号(使用网络通信)
  4. ;listen = /run/php-fpm/www.sock            #注释该行
  5. pm.max_children = 50                #最大进程数量
  6. pm.start_servers = 5                #最小进程数量
  7. [root@proxy etc]#systemctl restart php-fpm #重启服务

步骤二:修改Nginx配置文件并启动服务

 
  1. [root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
  2. ...
  3. location ~ \.php$ {        #~是使用正则表达式,匹配以.php结尾
  4. root html;
  5. fastcgi_pass 127.0.0.1:9000; #将请求转发给本机9000端口
  6. fastcgi_index index.php;
  7. #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  8. include fastcgi.conf; #加载fastcgi配置文件
  9. }

步骤三:测试LNMP架构能否解析PHP页面

启动或者重加载nginx

 
  1. [root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload
  2. #请先确保nginx是启动状态,否则运行该命令会报错,报错信息如下:
  3. #[error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)

再次使用浏览器访问192.168.99.5/test.php 可以看到页面内容!

2)再测试连接数据库的PHP页面

可以参考lnmp_soft/php_scripts/mysql.php

 
  1. cp ~/lnmp_soft/php_scripts/mysql.php /usr/local/nginx/html #拷贝动态网站测试页面到nginx中

3)客户端使用浏览器访问服务器PHP首页文档,检验是否成功

http://192.168.99.5/mysql.php

然后修改数据库内容进行测试

 
  1. mysql #进入数据库
  2. create user dc@localhost identified by '123'; #创建测试账户
  3. quit; #退出

再刷新192.168.99.5/mysql.php 可以看到新创建的用户

LNMP常见问题

如果仅访问ip就能看到动态网站的默认页,可以按下列方法配置

 
  1. ...
  2. location / {
  3. root html;
  4. index index.php index.html index.htm;
  5. #设置默认首页为index.php,当用户在浏览器地址栏中只写域名或IP,不说访问什么页面时,服务器会把默认首页index.php返回给用户
  6. }
  7. ...

Nginx的默认访问日志文件为/usr/local/nginx/logs/access.log

Nginx的默认错误日志文件为/usr/local/nginx/logs/error.log

PHP默认错误日志文件为/var/log/php-fpm/www-error.log

如果动态网站访问失败,可用参考错误日志,查找错误信息。

看到以上test.php、mysql.php两个页面说明nginx不但能支持静态网站,也能解析动态网站了,这种情况也可以记作nginx实现了动静分离

3 使用socket方式连接php-fpm

实现此案例需要按照如下步骤进行。

步骤一: php-fpm配置文件

1)打开php-fpm配置文件

 
  1. [root@proxy etc]# vim /etc/php-fpm.d/www.conf
  2. [www]
  3. ;listen = 127.0.0.1:9000                    #注释或删除该行
  4. listen = /run/php-fpm/www.sock                #socket方式(使用进程通信)
  5. listen.acl_users = apache,nginx,nobody        #添加nobody账户
  6. [root@proxy etc]#systemctl restart php-fpm #重启服务

步骤二:修改Nginx配置文件并启动服务

 
  1. [root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
  2. ...
  3. location ~ \.php$ {                    #~是使用正则表达式,匹配以.php结尾
  4. root html;
  5. #fastcgi_pass 127.0.0.1:9000;        #注释或删除该行
  6. fastcgi_pass unix:/run/php-fpm/www.sock; #将请求转发给php-fpm进程
  7. fastcgi_index index.php;
  8. #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  9. include fastcgi.conf;            #加载fastcgi配置文件
  10. }
  11. [root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload

4 案例3:地址重写

4.1 问题

  1. 通过调整Nginx服务端配置,实现地址重写功能

4.2 方案

关于Nginx服务器的地址重写,主要用到的配置参数是rewrite

  • rewrite regex replacement flag
  • rewrite 旧地址 新地址 [选项]

4.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:修改配置文件(访问a.html重定向到b.html)

1)修改Nginx服务配置:

 
  1. [root@proxy nginx]# cp conf/nginx.conf.default conf/nginx.conf #还原配置文件
  2. [root@proxy nginx]# cp:是否覆盖"conf/nginx.conf"? y
  3. [root@proxy nginx]# vim conf/nginx.conf
  4. .. ..
  5. server {
  6. listen 80;
  7. server_name localhost;
  8. rewrite /a.html /b.html;            
  9. location / {
  10. root html;
  11. index index.html index.htm;
  12. }
  13. }
  14. [root@proxy ~]# echo "nginx-B~~" > /usr/local/nginx/html/b.html

2)重新加载配置文件

 
  1. [root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload

3)客户端测试

http://192.168.99.5/a.html

步骤二:测试redirect选项

1)修改Nginx服务配置:

 
  1. [root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
  2. .. ..
  3. server {
  4. listen 80;
  5. server_name localhost;
  6. rewrite ^/a.html$ /b.html redirect;            
  7. location / {
  8. root html;
  9. index index.html index.htm;
  10. }
  11. }

2)重新加载配置文件

 
  1. [root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload

3)客户端测试,地址栏同时发生变化

http://192.168.99.5/a.html

步骤三:不同网站间跳转

修改Nginx服务配置实现访问192.168.99.5的请求重定向至www.tmooc.cn

 
  1. [root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
  2. .. ..
  3. server {
  4. listen 80;
  5. server_name localhost;
  6. rewrite / http://www.tmooc.cn/;
  7. location / {
  8. root html;
  9. index index.html index.htm;
  10. }
  11. }

2)重新加载配置文件

 
  1. [root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload

3)客户端测试

http://192.168.99.5

步骤四:修改配置文件(访问192.168.99.5/下面子页面,重定向至www.tmooc.cn/下相同的子页面)

1) 修改Nginx服务配置

 
  1. [root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
  2. .. ..
  3. server {
  4. listen 80;
  5. server_name localhost;
  6. rewrite /(.*) http://www.tmooc.cn/$1;
  7. location / {
  8. root html;
  9. index index.html index.htm;
  10. }
  11. }

2)重新加载配置文件

 
  1. [root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload

3)客户端测试

http://192.168.99.5/test

步骤五:实现不同浏览器跳转到不同页面

1) 创建网页目录以及对应的页面文件:

 
  1. [root@proxy nginx]# mkdir html/firefox
  2. [root@proxy nginx]# echo firefox~~ > html/firefox/abc.html #火狐专用页面
  3. [root@proxy nginx]# echo others~~ > html/abc.html            #其他浏览器专用页面

火狐访问192.168.99.5/abc.html时可以看到html/firefox/abc.html里面内容

其他浏览器访问192.168.99.5/abc.html时可以看到html/abc.html里面内容

2) 修改Nginx服务配置

 
  1. [root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
  2. .. ..
  3. server {
  4. listen 80;
  5. server_name localhost;
  6. location / {
  7. root html;
  8. index index.html index.htm;
  9. }
  10. if ($http_user_agent ~* firefox) {        #如果用户使用了火狐浏览器
  11. rewrite (.*) /firefox/$1; #就进行地址重写,让用户看到火狐专用页面$http_user_agent是nginx的内置变量,存储了用户的信息,比如用的什么浏览器,~是匹配正则 *是忽略大小写
  12. }
  13. }

3)重新加载配置文件

 
  1. [root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload

4)客户端测试

 
  1. 分别用火狐浏览器与其他浏览器访问相同地址http://192.168.99.5/abc.html,可以得到不同结果

5 地址重写的选项

redirect 临时重定向,状态码302,爬虫不更新URI

permanent 永久重定向,状态码301,爬虫更新URI

last 不再读其他语句,但还会继续匹配其他location语句

 
  1. echo "nginx-c~~" > html/c.html        #准备素材c页面
  2. rewrite /a.html /b.html last;        #没有其他location语句时,打开b页面
  3. rewrite /b.html /c.html ;

break 不再读其他语句,结束请求

 
  1. location / {                #此处为默认的location
  2. rewrite /a.html /b.html break;        #break可以阻止后面的语句
  3. root html;
  4. index index.html index.htm;
  5. }
  6. location /b.html {                #这里是新添加的location
  7. rewrite /b.html /c.html;
  8. }
  • 22
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Ansible角色是Ansible中的一种组织方式,它是一组任务、变量、文件和模板的集合,用于实现某个特定功能。在这个问题中,我们可以使用Ansible角色来部署LNMP(Linux、Nginx、MySQL和PHP)堆栈。 以下是一个简单的LNMP Ansible角色部署示例: 1. 创建一个名为“lnmp”的Ansible角色目录: ``` mkdir roles/lnmp ``` 2. 在lnmp目录中创建一个tasks目录: ``` mkdir roles/lnmp/tasks ``` 3. 在tasks目录中创建一个main.yml文件,其中包含以下任务: ``` - name: Install Nginx yum: name: nginx state: present - name: Start Nginx service service: name: nginx state: started - name: Install MySQL yum: name: mysql-server state: present - name: Start MySQL service service: name: mysqld state: started - name: Install PHP yum: name: php state: present - name: Install PHP-FPM yum: name: php-fpm state: present - name: Start PHP-FPM service service: name: php-fpm state: started ``` 这些任务将安装和启动Nginx、MySQL和PHP-FPM服务。 4. 在lnmp目录中创建一个vars目录: ``` mkdir roles/lnmp/vars ``` 5. 在vars目录中创建一个main.yml文件,其中包含以下变量: ``` --- nginx_conf_file: /etc/nginx/nginx.conf mysql_root_password: mysecretpassword php_conf_dir: /etc/php.d/ ``` 这些变量将用于配置Nginx、MySQL和PHP的设置。 6. 在lnmp目录中创建一个templates目录: ``` mkdir roles/lnmp/templates ``` 7. 在templates目录中创建一个nginx.conf.j2模板文件: ``` worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; server { listen 80; server_name localhost; location / { root /var/www/html; index index.php index.html index.htm; } location ~ \.php$ { root /var/www/html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } } ``` 这个模板将用于生成Nginx的配置文件。 8. 在tasks目录中创建一个configure.yml文件,其中包括以下任务: ``` - name: Copy Nginx configuration file template: src: nginx.conf.j2 dest: "{{ nginx_conf_file }}" mode: '0644' - name: Set MySQL root password mysql_user: name: root password: "{{ mysql_root_password }}" login_unix_socket: /var/lib/mysql/mysql.sock - name: Copy PHP configuration file copy: src: php.ini dest: "{{ php_conf_dir }}" mode: '0644' ``` 这些任务将生成Nginx配置文件、设置MySQL root密码和复制PHP配置文件。 9. 在lnmp目录中创建一个files目录: ``` mkdir roles/lnmp/files ``` 10. 在files目录中创建php.ini文件: ``` memory_limit = 128M upload_max_filesize = 64M post_max_size = 64M ``` 这个文件将被复制到PHP配置目录中。 11. 在lnmp目录中创建一个meta目录: ``` mkdir roles/lnmp/meta ``` 12. 在meta目录中创建一个main.yml文件,其中包含以下元数据: ``` --- dependencies: - { role: geerlingguy.repo-epel } - { role: geerlingguy.mysql } ``` 这些元数据将指定依赖项,以便安装EPEL存储库和MySQL角色。 13. 在playbook中使用lnmp角色: ``` - hosts: webserver become: true roles: - lnmp ``` 这个playbook将在webserver主机上使用lnmp角色。 这就是一个简单的LNMP Ansible角色部署示例。当然,还有很多其他的配置选项和任务可以添加到这个角色中,以满足不同的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值