LNMPA LEMPA Ubuntu 16.04 配置Nginx前端Web服务器 Apache后端

  • [color=blue]更新系统 [/color]

sudo apt-get update sudo apt-get upgrade

  • [color=blue]安装apache [/color]

sudo apt-get install apache2 update-rc.d apache2 enable

  • [color=blue]安装apache [/color]

sudo apt install php7.0-fpm php7.0-mbstring php7.0-xml php7.0-mysql php7.0-common php7.0-gd php7.0-json php7.0-cli php7.0-curl libapache2-mod-php7.0

  • [color=blue]配置Apache和PHP[/color]

编辑配置文件 80端口改为8080 /etc/apache2/sites-available/000-default.conf /etc/apache2/ports.conf

  • [color=blue]配置防火墙[/color]

IPTABLE用户 iptables -A INPUT -m state NEW -p tcp --dport 8080 -j ACCEPT ufw用户ufw allow 8080 重新启动apache sudo service apache2 restart 通过名称info.php创建一个php文件并验证服务器属性/参数 cd /var/www/html echo "<?php echo phpinfo(); ?>" >> info.php 访问浏览器指向http://demohost.com:8080/info.php并检查服务器(apache)属性/参数

安装Nginx后,将其配置为在端口8080上运行的apache Web服务器的反向代理(NGINX反向代理)。浏览nginx配置目录/ etc / nginx并编辑文件“nginx.conf” cd /etc/nginx vi nginx.conf 删除gzip部分中的注释,在Nginx中启用gzip压缩 \# Gzip Settings gzip on; gzip_disable "msie6"; gzip_vary on; gzip_proxied any; gzip_comp_level 6; gzip_buffers 16 8k; gzip_http_version 1.1; gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; 在gzip设置下面,添加代理缓存设置,代理缓存的目录是/ var / cache proxy_cache_path /var/cache levels=1:2 keys_zone=reverse_cache:60m inactive=90m max_size=1000m;

  • [color=blue]配置Nginx 默认主机 [/color] server段配置 server { access_log off; location / { try_files $uri $uri/ =404; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host; proxy_pass http://127.0.0.1:8080; } location ~ \.php$ { fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; } }
  • [color=blue]配置Nginx 主机 [/color]
# cd /etc/nginx/sites-available
 # vi reverse.conf
server {
             listen 80;

             # Site Directory same in the apache virtualhost configuration
             root /var/www/html;
             index index.php index.html index.htm;

            # Domain
             server_name www.reverse.com reverse.com;

            location / {
                                 try_files $uri $uri/ /index.php;
            }

# Reverse Proxy and Proxy Cache Configuration
 location ~ \.php$ {

                               proxy_set_header X-Real-IP $remote_addr;
                               proxy_set_header X-Forwarded-For $remote_addr;
                               proxy_set_header Host $host;
                               proxy_pass http://127.0.0.1:8080;

                    # Cache configuration
                               proxy_cache reverse_cache;
                               proxy_cache_valid 3s;
                               proxy_no_cache $cookie_PHPSESSID;
                               proxy_cache_bypass $cookie_PHPSESSID;
                               proxy_cache_key "$scheme$host$request_uri";
                               add_header X-Cache $upstream_cache_status;
 }

# Enable Cache the file 30 days
 location ~* .(jpg|png|gif|jpeg|css|mp3|wav|swf|mov|doc|pdf|xls|ppt|docx|pptx|xlsx)$ {
                             proxy_cache_valid 200 120m;
                             expires 30d;
                             proxy_cache reverse_cache;
                             access_log off;
 }

# Disable Cache for the file type html, json
 location ~* .(?:manifest|appcache|html?|xml|json)$ {
                             expires -1;
 }

location ~ /\.ht {
                              deny all;
        }
 }

激活新的虚拟主机配置 cd /etc/nginx/sites-available ln -s /etc/nginx/sites-available/reverse.conf /etc/nginx/sites-enabled/ 测试NGINX反向代理配置并重新启动nginx nginx -t service nginx restart

  • [color=blue]测试Nginx反向代理[/color]

curl -I reverse.com/info.php 显示有:get X-Cache: HIT curl -I reverse.com/index.html 显示有: Cache-Control: no-cache

  • [color=blue]配置apache来记录访问的真实IP[/color] 配置apache来记录访问者的真实IP而不是本地IP。安装apache模块“libapache2-mod-rpaf”并编辑模块配置文件 11.02.33.03设置为你的服务器ip sudo apt-get install libapache2-mod-rpaf cd /etc/apache2/mods-available/ vi rpaf.conf RPAFproxy_ips 127.0.0 11.02.33.03 ::1 service apache2 restart tail -f /var/log/apache2/access.log 您会发现,接入IP(127.0.0.1)现在被用户/浏览器从请求到达的实际IP替换。 就这样。我们配置了NGINX反向代理。如果访问者请求一个php文件,请求将被传递到8080端口的apache,您可以在apache日志文件中看到真正的ip访问者。

来源 http://www.dedwijadas.in/setup-nginx-reverse-proxy-apache-ubuntu/

安装Apache和PHP-FPM,在Apache中安装mod_rpaf,将访问者IP转发到Apache。否则,您的脚本将读取REMOTE_ADDR值作为服务器IP。

sudo apt-get update
sudo apt-get install apache2 libapache2-mod-fastcgi libapache2-mod-rpaf php-fpm
配置Apache使用mod_fastcgi
sudo a2dismod php7.0
mod_fastcgi依赖于它mod_action。mod_action默认情况下禁用,所以我们首先需要启用它:sudo a2enmod actions
sudo apt install php7.0-fpm php7.0-mbstring php7.0-xml php7.0-mysql php7.0-common php7.0-gd php7.0-json php7.0-cli php7.0-curl libapache2-mod-php7.0

###配置Apache和PHP-FPM
sudo nano /etc/apache2/ports.conf
Listen 88
注意:127.0.0.1:8080配置反向代理时,通常将Web服务器设置为侦听,但这样做会将PHP环境变量SERVER_ADDR的值设置为环回IP地址而不是服务器的公共IP。我们的目标是设置Apache,使其网站在其前面看不到反向代理。所以我们将配置它来监听8080所有的IP地址。
编辑Apache的默认虚拟主机文件
sudo nano /etc/apache2/sites-available/000-default.conf
<VirtualHost *:88>
保存文件并重新加载Apache
sudo systemctl reload apache2
验证Apache现在正在监听
sudo netstat -tlpn
输出应该像下面的例子,apache2在:: 88上
tcp6 0 0 :::88 :::* LISTEN 3051/apache2

禁用在Apache中使用模块

由于HTTP请求现在由Nginx处理,我们可以在Apache中禁用KeepAlive。编辑/etc/apache2/apache2.conf和更改:
KeepAlive Off
另外,运行以下命令来禁用未使用的模块。
a2dismod deflate
a2dismod cgi
a2dismod autoindex
a2dismod negotiation
a2dismod ssl

#nginx配置

将任何PHP请求发送回Apache

server {
      root /usr/local/www/mydomain.com;
      server_name mydomain.com www.mydomain.com;
 
      access_log logs/mydomain_access.log;
      error_log logs/mydomain_error.log;
 
      location / { 
            # 主机文档根目录的位置块
      } 
 
      location ~ \.php {
            # 此块将捕获任何具有.php扩展名的请求 
            # 通常在这个块中将数据传递给FastCGI进程
 
            # 这两行告诉Apache客户端正在转发的实际IP
            #Apache需要mod_proxy(http://bit.ly/mod_proxy)才能工作
            # 大多数Apache 2.0+服务器已经配置了mod_proxy
 
            proxy_set_header X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
 
            #这个下一行添加了Host头,以便apache知道要提供哪个vHost
            # host变量自动设置为Nginx响应的主机名
 
            proxy_set_header Host $host;
 
            #现在我们转回apache
            # 如果您使用并行配置,则可以将IP更改为
            # apache绑定的IP端口80  http://192.170.2.1:80
 
            proxy_pass http://127.0.0.1:8080;
      }
 
       #你不喜欢看到所有错误,在根中丢失
       location = /favicon.ico { access_log off; log_not_found off; }
 
       #如果您不喜欢在root中看到遗失的robots.txt的错误
       location = /robots.txt { access_log off; log_not_found off; }
 
       #防止像.htaccess .htpassword .secret等文件一样被访问
       #如果你愿意,你可以删除日志指令
       #记录尝试访问隐藏文件的客户端的任何尝试
       location ~ /\. { deny all; access_log off; log_not_found off; }
}

使用nginx后面的apache进行重写(mod_rewrite)的情况下,您可以使用一个名为try_files的简单指令。这是与上述相同的代码,但是当进行更改时,使得nginx找不到的任何文件或文件夹将被传递到apache进行处理(对于诸如WordPress之类的情况或基于文件或文件夹未被Nginx捕获的基于.htaccess的重写)

server {
      root /usr/local/www/mydomain.com;
      server_name mydomain.com www.mydomain.com;
 
      access_log logs/mydomain_access.log;
      error_log logs/mydomain_error.log;
 
      location / { 
            # try_files attempts to serve a file or folder, until it reaches the fallback at the end
            try_files $uri $uri/ @backend;
      } 
 
      location @backend {
            # essentially the same as passing php requests back to apache
 
            proxy_set_header X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header Host $host;
            proxy_pass http://127.0.0.1:8080;
      }
      # ... 其余与上述配置相同... 
}

使用上述配置,Nginx将提供任何存在的文件或文件夹(php块将捕获以.php结尾的文件),否则将在后端传回给apache。 在某些情况下,除非另有说明,否则您可能希望将所有内容传递给Apache,在这种情况下,此类配置

server {
      root /usr/local/www/mydomain.com;
      server_name mydomain.com www.mydomain.com;
 
      access_log logs/mydomain_access.log;
      error_log logs/mydomain_error.log;
 
      location / {
            proxy_set_header X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
 
            proxy_set_header Host $host;
 
            proxy_pass http://127.0.0.1:8080;
      }
 
      # 示例1 - 指定文件夹及其内容
      # Nginx提供/ css下的所有内容
      location /css { }
 
      #示例2 - 指定RegEx模式,如文件扩展名
      # 从Nginx直接提供某些文件
      location ~* ^.+\.(jpg|jpeg|gif|png|css|zip|pdf|txt|js|flv|swf|html|htm)$
      {
            #这将基本匹配上述扩展名的任何文件
            # 当然如果文件不存在,你会看到一个Nginx错误页面
            #apache自己的错误页面
      }
}

查看服务器IP地址的简单方法

ip addr show eth0|grep inet|awk '{print $2}'|cut -d / -f1

还差个Mysql 5.6

sudo apt-get install software-properties-common
sudo add-apt-repository -y ppa:ondrej/mysql-5.6
sudo apt-get update
sudo apt-get install mysql-server-5.6