wordpress,  🛠️ 运维

Nginx + Apache 实现域名默认打开 wordpress 首页

我主机上使用的 Nginx 作为反向代理服务器,而 web 服务器使用的是 Apache2,并且设置了 Apache2 的监听端口是 8080(避免和 Nginx 冲突)。

wordpress 的安装目录是 /var/www/html/wordpress

当以上安装完成后,如果要访问我的 wordpress 主页,我就必须输入 trantor.ink:8080/wordpress

为了让 Apache2 将 web 根目录指向 /var/www/html/wordpress,需要做如下配置:

#修改/etc/apache2/sites-enabled/000-default.conf下的 DocumentRoot
DocumentRoot /var/www/html/wordpress

然后重启 Apache2 服务器

sudo systemctl restart apache2

此时就能通过 trantor.ink:8080/ 访问我的 wordpress 主页了,但仅仅只能访问主页,且很多 css 样式丢失了,还需要进行如下设置:

只有进行了上图中的配置,在 wordpress 进行页面跳转才能正常执行。

下一步是将我们的域名 trantor.ink 默认指向 8080 端口,需要对 Nginx 进行如下配置:

location / {
		# First attempt to serve request as file, then
		# as directory, then fall back to displaying a 404.
		proxy_pass http://127.0.0.1:8080;  # 将请求转发到 Apache 服务器
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
	        #  try_files $uri $uri/ =404;
	}

这一步中将流量重定向到 http://127.0.0.1:8080,即 Apache2 监听的端口,经过这个配置后,浏览器输入 trantor.ink 就能直接打开我的 wordpress 主页了。


2024/12/25

这段时间研究如何隐藏掉 8080 端口,但没有找到很好的解决方案,且在给 apache2 配置 https 的时候也遇到了一些问题,最后决定不用 apache2,直接通过 nginx 来运行 wordpress。

首先是 nginx 的配置,将原先的 /etc/nginx/sites-available/default 修改为如下内容:

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or WordPress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
        server_name trantor.ink www.trantor.ink;
	# SSL configuration
	#
	# listen 443 ssl default_server;
	# listen [::]:443 ssl default_server;
	#
	# Note: You should disable gzip for SSL traffic.
	# See: https://bugs.debian.org/773332
	#
	# Read up on ssl_ciphers to ensure a secure configuration.
	# See: https://bugs.debian.org/765782
	#
	# Self signed certs generated by the ssl-cert package
	# Don't use them in a production server!
	#
	# include snippets/snakeoil.conf;

	root /var/www/html/wordpress;

	# Add index.php to the list if you are using PHP
	index index.php index.html index.htm;

	server_name _;


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

	# PHP 处理部分
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;  # 根据你的 PHP 版本调整
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }

        # 禁止访问隐藏文件(如 .htaccess)
        location ~ /\. {
            deny all;
        }

	location /videos/ {
	    alias /var/www/html/videos/;
            autoindex on;
	}


    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/trantor.ink/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/trantor.ink/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#	listen 80;
#	listen [::]:80;
#
#	server_name example.com;
#
#	root /var/www/example.com;
#	index index.html;
#
#	location / {
#		try_files $uri $uri/ =404;
#	}
#}

server {
    if ($host = www.trantor.ink) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = trantor.ink) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


	listen 80;
        server_name trantor.ink www.trantor.ink
	# SSL configuration
	#
	# listen 443 ssl default_server;

	server_name _;
    return 404; # managed by Certbot




}

在我实际操作中遇到了一个比较坑的问题,就是如下这行配置,一开始没有在尾部加 ; 号,导致 nginx 把后面的 root /var/www/html/wordpress; 也解析成了 service_name 的配置,导致配置一直有问题。

server_name trantor.ink www.trantor.ink;

配置完以后打开 trantor.ink 还是会转到 trantor.ink:8080 ,这里的问题并不是 nginx 导致,因为 nginx 里面的转发已经修改了,导致这个问题的是 wordpress 设置里的站点主页设置的还是 http://trantor.ink:8080。但由于此时 wordpress 无法打开,所以修改这两个配置需要对 wordpress 的数据库进行直接修改:

UPDATE wp_options SET option_value = 'https://trantor.ink' WHERE option_name = 'siteurl';
UPDATE wp_options SET option_value = 'https://trantor.ink' WHERE option_name = 'home';

修改完以后站点就能正常打开了,同时由于之前给 trantor.ink 配置过 https,所以站点也就直接支持了 https。

另外还有一个问题,就是原有文章中如果使用的图片是 wordpress 媒体库里的,那么他们的链接需要进行更新,否则还是会使用原来的 trantor.ink:8080 作为 host。

一条评论

留言

您的邮箱地址不会被公开。 必填项已用 * 标注