NginxWEB架构实战篇

1.动态网站架构

资源文件识别 语言识别 框架识别
index.php 开源的php Windows/Linux+nginx+php+mysql
index.py 开源python Windows/Linux+apache+python+mysql
index.jsp 商业JAVA windows/Linux+tomcat+JDK+Oracle
index.asp 商业c# Windows+iis+asp.net+sql-server/oracle/mogodb

2.LNMP动态网站环境部署

2.1Linux部署

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0

2.2Nginx部署

[root@localhost ~]# yum install -y nginx

2.3php-fpm部署

部署方法2选1

1.RPM部署

[root@localhost ~]# yum install -y php-fpm php-mysql php-gd
#php-fpm:php接收动态请求的程序
#php-mysql:php链接mysql的程序
#php-gd:图形库程序(GD库可以处理图片,或者生成图片)

[root@localhost ~]# systemctl restart php-fpm
#启动php-fpm

[root@localhost ~]# systemctl enable php-fpm
#开机启动php-fpm

[root@localhost ~]# netstat -anpt | grep 9000
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      1362/php-fpm: maste 

[root@localhost ~]# vim /usr/share/nginx/html/index.php
#测试php页面(php基本信息)
<?php
phpinfo();
?>
#测试语句

[root@localhost ~]# 
vim /etc/nginx/conf.d/default.conf 
	server {
   
location / {
   
...
index index.php index.html;
#增加PHP主页名称:index.php
...
}
}
[root@localhost ~]# vim /etc/nginx/conf.d/default.conf 
server {
   
location / {
   
index index.php;
}
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;
#启动nginx_fastcgi功能,解除#注释修改路径即可。
}
}
[root@localhost ~]# systemctl restart nginx

浏览器观察http://192.168.36.154/index.php
在这里插入图片描述
fastcgi_param:这个配置的意思是 在浏览器中访问的.php文件,实际读取的是$ document _root(网站根目录)下的.php文件。也就是说当访问127.0.0.1/index.php的时候,需要读取网站根目录下面的index.php文件,如果没有配置这一配置项时,nginx不回去网站根目录下访问.php文件,所以返回空白。
通过location指令,将所有以php为后缀的文件都交给127.0.0.1:9000来处理,而这里的IP地址和端口就是FastCGI进程监听的IP地址和端口。
fastcgi_param指令指定放置PHP动态程序的主目录,也就是$fastcgi_script_name前面指定的路径,这里是/usr/local/nginx/html目录,建议将这个目录与Nginx虚拟主机指定的根目录保持一致,当然也可以不一致。
fastcgi_params文件是FastCGI进程的一个参数配置文件,在安装Nginx后,会默认生成一个这样的文件,这里通过include指令将FastCGI参数配置文件包含了进来。接下来,启动nginx服务。/usr/local/nginx/sbin/nginx 到此为止,Nginx+PHP已经配置完成。

2.源码部署(可选)
部署PHP-fpm
1. 以php-fpm的方式安装php

[root@webserver ~]# yum -y install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel \
libxml2 libxml2-devel libcurl libcurl-devel libxslt-devel openssl-devel

[root@webserver ~]# tar xf php-5.6.29.tar.xz 
[root@webserver ~]# cd php-5.6.29/
[root@webserver ~]# ./configure \
--prefix=/usr/local/php \
--with-curl \
--with-freetype-dir \
--with-gd \
--with-gettext \
--with-iconv-dir \
--with-jpeg-dir \
--with-kerberos \
--with-libdir=lib64 \
--with-libxml-dir \
--with-mysql \
--with-mysqli \
--with-openssl \
--with-pcre-regex \
--with-pdo-mysql \
--with-pdo-sqlite \
--with-pear \
--with-png-dir \
--with-xmlrpc \
--with-xsl \
--with-zlib \
--enable-fpm \
--enable-bcmath \
--enable-libxml \
--enable-inline-optimization \
--enable-gd-native-ttf \
--enable-mbregex \
--enable-mbstring \
--enable-opcache \
--enable-pcntl \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-sysvsem \
--enable-xml \
--enable-zip
[root@webserver php-5.6.29]# make
[root@webserver php-5.6.29]# make install

2. php-fpm配置文件(影响php处理php程序的性能,例如php进程数、最大连接数配置等,运维人员关注)

[root@webserver php-5.6.29]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

3. php置文件(影响php代码,例如允许客户端最大上传文件的大小,设置的timezone,php所支持的扩展功能例如是否可以连接MySQL、Memcache,程序员关注)

[root@webserver php-5.6.29]# cp php.ini-production /usr/local/php/lib/php.ini

4. init script centos6 [可选]centos6
[root@webserver php-5.6.29]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
[root@webserver php-5.6.29]# chmod a+x /etc/rc.d/init.d/php-fpm
[root@webserver php-5.6.29]# chkconfig --add php-fpm
[root@webserver php-5.6.29]# chkconfig php-fpm on
[root@webserver php-5.6.29]# service php-fpm start
Starting php-fpm  done

5. Systemd Script centos7 [可选]

[root@webserver ~]# vim /usr/lib/systemd/system/php-fpm.service
[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target

[Service]
Type=simple
PIDFile=/run/php-fpm.pid
ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/bin/kill -SIGINT $MAINPID

[Install]
WantedBy=multi-user.target

[root@webserver ~]# systemctl start php-fpm.service 
[root@webserver ~]# systemctl status php-fpm.service


[root@localhost ~]# ss -an |egrep ':80|:9000'
tcp    LISTEN     0      128    127.0.0.1:9000                  *:*                  
tcp    LISTEN     0      128                  *:80                  *:*  

2.4MySQL部署

部署方法2选1

1.RPM部署

[root@localhost ~]# yum -y install mariadb-server mariadb
	#安装mysql服务器程序和客户机程序。
[root@localhost ~]# systemctl start mariadb
	#启动mysql服务器
[root@localhost ~]# systemctl enable mariadb
	#开机启动mysql服务器
[root@localhost ~]# mysqladmin password '123456'
	#修改mysql的root密码为‘123456’
[root@localhost ~]# mysql -uroot -p'123456'
MariaDB[(none)]> create database bbs;
	#准备数据库,存放app
MariaDB[(none)]> grant all on bbs.* to phptest@'192.168.36.%' identified by '123456';
	#授权phptest用户管理bbs库
	#请注意用户名密码主机参数需要更换。
MariaDB[(none)]> flush privileges;
	#刷新权限
[root@localhost ~]# vim /usr/share/nginx/html/index.php
<?php
$link=mysql_connect('192.168.36.154','phptest','123456');
if ($link)
              echo "Successfuly";
else
              echo "Faile";
mysql_close();
?>
	#修改主页,测试MYSQL的链接状态
	#如果测试为faile,请检查数据库授权结果。
2.源码部署(了解)

2.5业务上线(开发那点事)

  • 技术点

UI:构图
前端:通过表单,文本框,提交按钮,页面布局
后端:php连接函数
后端:php插入函数
DBA:实现后台数据库的写入。
OP:业务上线

  • 准备前台html页面
[root@localhost ~]# vim /usr/share/nginx/html/1.html
<html>
<body>
<img src="logo.jpg" />

<form action="insert.php" method="post">
Firstname: <input type="text" name="firstname" />
Lastname: <input type="text" name="lastname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>

</body>
</html>

#html 页面的开始
#body页面内容的开始
#form 表单,整理input
#input,文本框
#/form 表单结束
#/body 页面内容结束
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值