Nginx的静态处理能力很强,但是动态处理能力不足,因此,在企业中常用动静分离技术
针对PHP的动静分离
静态页面交给 Nginx处理
动态页面交给 PHP-FPM模块或 Apache处理
在 Nginx的配置中,是通过 ocation配置段配合正则匹配实现静态与动态页面的不同处理方式
Nginx不仅能作为Web服务器,还具有反向代理、负载均衡和缓存的功能
Nginx通过 proxy模块实现将客户端的请求代理至上游服务器,此时ngInx与上游服务器的连接是通过http协议进行的
Nginx在实现反向代理功能时的最重要指令为proxy pass,它能够并能够根据UR、客户端参数或其它的处理逻辑将用户请求调度至上游服务器
根据需要,将配置 Nginx实现动静分离,对php页面的请求转发给LAMP处理,而静态页面交给Nginx处理,以实现动静分离
一台apache 服务器解析 动态 192.168.100.50
一台nginx 服务器解析 静态 192,168.100.48
yum 安装httpd
[root@sha ~]# yum install httpd httpd-devel -y
[root@sha ~]# systemctl start httpd.service
[root@sha ~]# firewall-cmd --permanent --zone=public --add-service=http
success
[root@sha ~]# firewall-cmd --permanent --zone=public --add-service=https
Success
[root@sha ~]# firewall-cmd --reload
success
[root@sha ~]# yum install mariadb-server mariadb-libs mariadb-devel -y
[root@sha ~]# systemctl start mariadb.service
[root@sha ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
You already have a root password set, so you can safely answer 'n'.
Change the root password? [Y/n] y //是否修改ROOT密码
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] n //是否删除匿名用户
... skipping.
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] n //是否拒绝ROOT 用户远程登录
... skipping.
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] n //是否删除测试数据库
... skipping.
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y //是否加载里面的全新列表
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
[root@sha ~]# yum install php -y
[root@sha ~]# yum install php-mysql -y
安装PHP插件
[root@sha ~]# yum install -y php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap curl curl-devel php-bcmath
[root@sha ~]# cd /var/www
[root@sha www]# ls
cgi-bin html
[root@sha www]# cd html/
[root@sha html]# vi index.php
<?php
phpinfo();
?>
~
~
[root@sha www]# systemctl restart httpd.service
[root@sha html]# vi index.php
创建程序账户
useradd -M -s /sbin/nologin nginx
安装nginx 环境软件
yum -y install gcc \
gcc-c++ \
make \
pcre-devel \
expat-devel \
perl \
zlib-devel \
pcre
编译安装
[root@sha nginx-1.12.2]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module
[root@sha nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@sha nginx-1.12.2]# cd /usr/local/sbin
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
cd /usr/local/sbin
ls
cd /opt/nginx-1.12.2/
yum install vim -y
[root@localhost nginx-1.12.2]# vim /lib/systemd/system/nginx.service
[Unit]!
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile =/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/bin/kill -S HUP $MAINPID
ExecStop=/usr/bin/kill -S QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
Systemsctl stop httpd
Systemsctl start nginx
[root@sha sbin]# yum install elinks
[root@sha sbin]# netstat -natp | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 19041/nginx: master
tcp 0 0 192.168.100.48:37568 36.156.207.240:80 TIME_WAIT -
tcp 0 0 192.168.100.48:59348 36.156.207.236:80 TIME_WAIT -
[root@sha sbin]# elinks http://192.168.100.48
[root@sha sbin]# vim /usr/local/nginx/conf/nginx.conf
访问动态 192.168.100.48/index.html
访问静态 192.168.100.48/index.php