环境:VWmare上配置的 ubuntu19.04 + mysql5.7.25 + php7.2 + nginx1.15.9 +apache2.4.38
目的:1、动静态分离:nginx处理css、js、jpg、png、html等静态资源,apache处理php动态文件
2、负载均衡:将客户端的请求分发到不同的服务器上处理请求
一、安装环境:
root@ubuntu:~# apt-get install nginx
root@ubuntu:~# apt-get install php
root@ubuntu:~# apt-get install mysql-server
root@ubuntu:~# apt-get install apache
root@ubuntu:~# apt-get install redis-server
解决Apache和Nginx端口冲突的问题。
root@ubuntu:~# vim /etc/apache2/ports.conf
# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.confListen 8080 #默认的是80 修改为自己定义的端口
<IfModule ssl_module>
Listen 444 #默认的是443 修改为自己定义的端口
</IfModule><IfModule mod_gnutls.c>
Listen 444 #默认的是443 修改为自己定义的端口
</IfModule># vim: syntax=apache ts=4 sw=4 sts=4 sr noet
至少,到这里,我的环境上已经没有问题了,apache和nginx都可以正常启动,127.0.0.1和127.0.0.1:8080都可以正常访问,如果还报错
root@ubuntu:~# sudo vim /etc/apache2/apache2.conf
在文件最后一行加上
ServerName localhost 或者 ServerName localhost:8080
二、安装php扩展(按需安装,以下仅为部分)
root@ubuntu:~# apt-get install php7.2-gd
root@ubuntu:~# apt-get install php7.2-mysql
root@ubuntu:~# apt-get install php7.2-xml
root@ubuntu:~# apt-get install php-redis
root@ubuntu:~# apt-get install php7.2-fpm
root@ubuntu:~# apt-get install php7.2-mbstring php7.2-dev php7.2-zip php7.2-curl php-xdebug libapache2-mod-php
默认的nginx配置文件路径 /etc/nginx/nginx.conf
默认的nginx网站配置文件路径 /etc/nginx/sites-available/default
默认的nginx访问日志目录 /var/log/nginx/access.log
默认的 apache配置文件路径 /etc/apache/apache.conf
默认的apache网站配置文件路径 /etc/apache2/sites-available/000-default.conf
默认的apache访问日志目录 /var/log/apache/access.log
默认的mysql配置路径 /etc/mysql/mysql.conf.d/mysqld.cnf
默认的网站根目录 /var/www/html
三、部署网站。
(1)在网站之家上随便下载了一个企业站,解压后扔到网站目录
root@ubuntu:~# cd /var/www/html
root@ubuntu:/var/www/html# mkdir test.locals.com
root@ubuntu:/var/www/html/test.locals.com# cd test.locals.com
root@ubuntu:/var/www/html/test.locals.com# wget http://tj.mycodes.net/201904/PHPOK5.2.060.zip
root@ubuntu:/var/www/html/test.locals.com# unzip PHPOK5.2.060.zip
root@ubuntu:/var/www/html/test.locals.com# cd ../
root@ubuntu:/var/www/html# chown -R www-data:www-data test.locals.com
root@ubuntu:/var/www/html# chmod -R 755 test.locals.com/
(2)创建并编辑nginx网站配置文件:nginx监听80端口
root@ubuntu:~# cd /etc/nginx/sites-available/
root@ubuntu:/etc/nginx/sites-available# cp default test.locals.com
root@ubuntu:/etc/nginx/sites-available# vim test.locals.com
#负载均衡start
upstream test_lanmp{
ip_hash;#没有做负载均衡的状态下注释此行
server test.locals.com:8080 weight=1 max_fails=2 fail_timeout=30s;#server ........ #这里可设置多个目标地址
}#负载均衡end
#设置日志格式,log_format具体参数参考 https://nginx.org/en/docs/http/ngx_http_core_module.html
log_format main '$remote_addr $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'$http_user_agent $http_x_forwarded_for $request_time $upstream_response_time $upstream_addr $upstream_status';#
#$remote_addr 远程地址
#$remote_user 远程用户
#$time_local 远程用户访问的时间
#$request 请求形式GET/POST等
#$status 请求状态 200 404 500...
#$body_bytes_sent 请求体body长度/字节数
#$http_referer 上一个页面来源
#$http_user_agent 用户代理/蜘蛛
#$http_x_forwarded_for 在经过代理时,代理将本来ip加载此头信息,传输原始ip
#$request_time
#
server {
listen 80;
listen [::]:80;root /var/www/html/test.locals.com;
index index.html index.php;server_name test.locals.com;
access_log /var/log/nginx/test_locals_com_access.log main;
location / {
#开启rewrite start (thinkphp 隐藏index)
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}
autoindex on;
try_files $uri $uri/ /index.php?s=$uri&$args;
#开启rewrite end
}#nginx配置图片等资源缓存7天,防盗链,不记录日志
location ~* \.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$ {
expires 7d;
valid_referers none blocked server_names *.abc.com *.a.com *.b.com *.baidu.com *.google.com *.google.cn *.soso.com;
if ($invalid_referer) {
return 403;
}
access_log off;
}#nginx配置js,css缓存1天,不记录日志
location ~* \.(js|css)$ {
expires 24h;
access_log off;
}
location ~ \.php$ {#反向代理、动静态分离start
proxy_set_header Host $host:$server_port; #设置请求的头部中主机名为请求的主机名,而不是代理的nginx的主机名proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://test_lanmp;client_max_body_size 10m; #允许客户端请求的最大单文件字节数
client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数,
proxy_connect_timeout 90; #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_send_timeout 90; #后端服务器数据回传时间(代理发送超时)
proxy_read_timeout 90; #连接成功后,后端服务器响应时间(代理接收超时)
proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的设置
proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
proxy_temp_file_write_size 64k;#设定缓存文件夹大小,大于这个值,将从upstream服务器传
#proxy_pass http://test.locals.com:8080;#没有做负载均衡的状态下直接指定代理的url,如果没有做负载均衡,注释掉上一行,放行此行
#反向代理、动静态分离end
}
}
(3)创建并编辑nginx网站配置文件:apache监听8080端口
root@ubuntu:~# cd /etc/apache/sites-available/
root@ubuntu:~# cd /etc/apache/sites-available/
root@ubuntu:/etc/apache/sites-available# cp 000-default.conf test.local.com.conf
root@ubuntu:/etc/apache/sites-available# vim test.local.com.conf
<VirtualHost *:8080>
ServerName test.locals.comServerAdmin webmaster@localhost
DocumentRoot /var/www/html/test.locals.comErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost><Directory /var/www/html/test.locals.com>
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
(4)编辑hosts文件
root@ubuntu:~# vim /etc/hosts
127.0.0.1 localhost
127.0.1.1 ubuntu
127.0.0.1 test.locals.com
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
四、创建软链接,检查nginx配置文件无误,开启apache rewrite后,重启服务。
root@ubuntu:~# ln -s /etc/apache2/sites-available/test.local.com.conf /etc/apache2/sites-enabled/
root@ubuntu:~# ln -s /etc/nginx/sites-available/test.local.com /etc/nginx/sites-enabled/
root@ubuntu:~# nginx -t
root@ubuntu:~# a2enmod rewrite
root@ubuntu:~# service nginx restart
root@ubuntu:~# service apache restart
五、查看结果。
在浏览器输入http://test.locals.com/和http://test.locals.com:8080,需要同时都可以访问才可以,否则请检查自己的网站配置。
查看apache访问日志
root@ubuntu:~# cat /var/log/apache/access.log
查看nginx访问日志
root@ubuntu:~# cat /var/log/nginx/test_locals_com_access.log
由上可以看出来,nginx请求的是静态文件,而apache只处理后端。
只做负载均衡的请看另一篇文章 ubuntu16.04 配置 nginx 简单负载均衡
至此,动静态分离和负载均衡配置完成。