搭建LNMP环境实现动态网页效果

483 阅读2分钟
搭建Nginx服务器

[root@localhost opt]# tar -xf lnmp_soft.tar.gz

//释放源码包

[root@localhost opt]# yum -y install gcc pcre-devel openssl-devel

//gcc是nginx的依赖包;pcre-devel可以让nginx支持正则;openssl-devel可以支持安全的加密网站

[root@localhost nginx-1.12.2]# ./configure --with-http_ssl_module

//在编译时添加安全模块

[root@localhost nginx-1.12.2]# make && make install

//安装

[root@localhost nginx]# sbin/nginx

//启动服务

[root@localhost nginx]# ss -ntulp |grep nginx tcp LISTEN 0 128 *:80 *:* users:(("nginx",pid=3953,fd=6),("nginx",pid=3952,fd=6))

//检测端口是否成功开启

测试

[root@localhost nginx]# systemctl stop firewalld

//关闭防护墙

//在浏览器输入本机ip地址,可以看到nginx的测试页面,即为成功


安装数据库与PHP服务

[root@localhost nginx]# yum -y install mariadb mariadb-server mariadb-devel

//安装数据库服务

[root@localhost nginx]# yum -y install php php-mysql php-fpm

//安装PHP服务

[root@localhost nginx]# vim conf/nginx.conf

//修改配置文件,开启动态页面解析功能

65 location ~ \.php$ { 66 root html; 67 fastcgi_pass 127.0.0.1:9000; 68 fastcgi_index index.php; 69 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 70 include fastcgi.cof; 71 }

[root@localhost nginx]# systemctl restart mariadb

//开启mariadb服务

[root@localhost nginx]# ss -ntulp | grep :3306 tcp LISTEN 0 50 *:3306 *:* users:(("mysqld",pid=4815,fd=14))

//检查端口是否正常开启

//开启php-fpm服务

[root@localhost nginx]# ss -ntulp | grep php-fpm tcp LISTEN 0 128 127.0.0.1:9000 *:* users:(("php-fpm",pid=4864,fd=0),("php-fpm",pid=4863,fd=0),("php-fpm",pid=4862,fd=0),("php-fpm",pid=4861,fd=0),("php-fpm",pid=4860,fd=0),("php-fpm",pid=4858,fd=6))

//检查端口是否正常开启

[root@localhost nginx]# sbin/nginx -s reload

//重新载入配置文件

配置完成,进行页面测试

可以将动态页面拷贝到/html目录下,在浏览器输入地址访问PHP页面


数据库的应用

创建一个dc用户,然后在刷新网页

[root@localhost nginx]# mysql
//进入数据库
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> create user dc@localhost identified by '123';
//创建dc用户
Query OK, 0 rows affected (0.00 sec)


在nginx中实现地址重写
[root@localhost nginx]# vim conf/nginx.conf
//修改配置文件
 35     server {
 36         listen       80;
 37         server_name  localhost;
 38         rewrite /a.html /b.html
//此处的a.html表示用户输入的网址中只要包含a.html即可跳转到b.html
实现用户在不同环境中访问不同页面的跳转功能
 35     server {
 36         listen       80;
 37         server_name  localhost;
 38    #    rewrite /a.html /b.html redirect
 39    #    rewrite ^/(.*)$ http://www.test.com/$1;
 40         if ($http_user_agent ~* firefox) {
 41         rewrite ^(.*)$  /firefox/$1
 42 }   
//~代表正则匹配;*表示不区分大小写;小括号中所有内容表示当发现用户使用火狐浏览器时,执行下面的跳转页面