dong瓜

导航

ubuntu搭建web服务器(nginx和apache2共存)--总结2

一、方案采用:nginx启用80端口,apache2启用8081端口,nginx启用443端口用于https; 其中nginx1.4+php7.3,apache2.4+php.5,6,mysql为5.7
    1、nginx为web服务器,wordpress可在80端口运行。
    2、nginx通过80端口代理到apache2的8081端口。

二、nginx配置在/etc/nginx/sites-available目录下,可实现配置多个虚拟主机,在目录下创建以域名为文件名的配置文件,如:test.xxx.com, 创建test.xxx.com文件,编辑好test.xxx.com文件后,需创建软链接到/etc/nginx/sites-enabled目录下,配置才会启作用
    语法:ln -s /etc/nginx/sites-available/test.xxx.com /etc/nginx/sites-enabled/test.xxx.com

    1、nginx为web服务器:
        test.xxx.com文件内容:
            server {
                listen 80;
                root /var/www/html2/test.xxx.com;
                # Add index.php to the list if you are using PHP
                index index.html index.htm index.nginx-debian.html;
                server_name test.xxx.com;
                location / {
                    # First attempt to serve request as file, then
                    # as directory, then fall back to displaying a 404.
                    try_files $uri $uri/ =404;
                    index  index.html index.htm index.php;    #默认访问文件
                    try_files $uri $uri/ /index.php?$args;    #wordpress路由解析
                }
                # pass PHP scripts to FastCGI server
                location ~ \.php($|/)
                {
                    #响应请求处理入口,使用php-fpm进行管理
                    fastcgi_pass unix:/run/php/php7.3-fpm.sock;
                    fastcgi_index index.php;
                    fastcgi_split_path_info ^(.+\.php)(.*)$;
                    fastcgi_param PATH_INFO $fastcgi_path_info;
                    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
                    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                    include fastcgi_params;
                }
                access_log /var/log/nginx/test.xxx.com-access.log;
                error_log /var/log/nginx/test.xxx.com-error.log;
            }

    2、nginx通过80端口代理到apache2的8081端口:
        nginx的test.xxx.com文件内容
            server {
                listen 80;
                # Add index.php to the list if you are using PHP
                index index.html index.htm index.nginx-debian.html index.php;
                server_name test.xxx.com;
                location / {
                    proxy_pass http://127.0.0.1:8081;
                    proxy_redirect off;
                    proxy_set_header Host $host;
                    proxy_set_header X-Real-IP $remote_addr;
                    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                    #index  index.html index.htm index.php;    #默认访问文件
                    # First attempt to serve request as file, then
                    # as directory, then fall back to displaying a 404.
                    #try_files $uri $uri/ =404;
                }
                access_log /var/log/nginx/test.xxx.com-access.log;
                error_log /var/log/nginx/test.xxx.com-error.log;    
            }

        apache2配置虚拟主机信息在/etc/apache2/sites-available/000-default.conf内添加:
            <VirtualHost *:8081>
                DocumentRoot /var/www/html/test.xxx.com
                ServerName test.xxx.com
                ServerAlias xxx.com
                ServerAdmin webmaster@test.xxx.com
                DirectoryIndex index.html index.htm index.php default.php app.php u.php
                ErrorLog ${APACHE_LOG_DIR}/test.xxx.com-error.log
                CustomLog ${APACHE_LOG_DIR}/test.xxx.com-access.log combined
                <Directory /var/www/html/test.xxx.com>
                    Options FollowSymLinks
                    AllowOverride All
                    Require all granted
                </Directory>
            </VirtualHost>

        apache2里配置虚拟主机8081端口接收nginx代理过来的信息

注:都配置完毕后,需对nginx和appache2服务重新启动,配置才生效

  

posted on 2021-02-05 15:37  dong瓜(TreeSky)  阅读(995)  评论(0编辑  收藏  举报