php+nginx部署wordpress,如何设置nginx配置文件

文章目录

  • 摘要
  • wordpress文章发布后,nginx报404解决方法
  • 处理 413 Request Entity Too Large
  • 最终的配置文件

摘要

本文是关于在CentOS上使用Nginx和PHP部署WordPress的指南。文章提供了一个Nginx配置文件示例,该示例包含了监听端口、网站域名、网站根目录、WordPress主页面配置、PHP解释器配置等多个方面的内容。并解决了wordpress文章发布后,nginx报404,413 Request Entity Too Large两个问题。需要根据实际情况进行相应的修改。

wordpress文章发布后,nginx报404解决方法
修改nginx.conf文件,在location /节点下添加如下代码:

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

处理 413 Request Entity Too Large
这个是nginx上传文件的大小限制,可以在nginx配置文件里面改一下。

client_max_body_size    100m; # # 默认才1m,很多插件都装不了,所以调大一点

最终的配置文件

server {
    listen       80;
    server_name  你的域名;
     location / {

        root /usr/share/nginx/html/wordpress;
        index index.php index.html index.htm;
        try_files $uri $uri/ /index.php?q=$uri&$args;  # 没有他,无法访问文章页会出现404
        client_max_body_size    100m; # # 默认才1m,很多插件都装不了,所以调大一点
   }

   location ~ \.php$ {
        root    /usr/share/nginx/html/wordpress;
        client_max_body_size    100m; # 默认才1m,很多插件都装不了,所以调大一点
        include /etc/nginx/fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
   }
}

该配置文件的解释如下:

  • listen 80:监听80端口。
  • server_name:指定网站的域名。
  • root:指定网站根目录。
  • index:指定默认文件,这里是index.php。
  • access_log和error_log:指定日志文件的路径。
  • location /:配置WordPress的主页面。
  • try_files:尝试查找文件,并根据结果进行相应的操作。
  • location ~ \.php$:配置PHP解释器,以便Nginx能够解析PHP脚本。
  • fastcgi_pass:指定FastCGI服务器的位置。
  • fastcgi_param SCRIPT_FILENAME:指定FastCGI解释器使用的文件名。

注意:这个配置文件假定你已经安装了PHP和Nginx,并将WordPress安装在了/usr/share/nginx/html/wordpress目录下。如果你的实际情况不同,需要相应地修改配置文件中的路径。

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是部署 MySQL、Nginx + PHP、并配置 WordPress 的项目步骤: 1. 部署 MySQL (1)安装 MySQL:在 Linux 系统上使用以下命令安装 MySQL: ``` sudo apt update sudo apt install mysql-server ``` (2)启动 MySQL:安装完成后,使用以下命令启动 MySQL: ``` sudo systemctl start mysql ``` (3)设置 MySQL 用户和密码:默认情况下,MySQL 服务器安装并未设置 root 密码,可以使用以下命令设置密码: ``` sudo mysql_secure_installation ``` 在该命令中,根据提示输入密码并进行其他设置。 2. 部署 Nginx + PHP (1)安装 Nginx:使用以下命令在 Linux 系统上安装 Nginx: ``` sudo apt-get update sudo apt-get install nginx ``` (2)安装 PHP:在 Linux 系统上使用以下命令安装 PHP: ``` sudo apt-get install php-fpm php-mysql ``` (3)配置 Nginx:使用以下命令打开默认 Nginx 配置文件: ``` sudo nano /etc/nginx/sites-available/default ``` 在该文件中,找到以下行: ``` index index.html index.htm index.nginx-debian.html; ``` 修为: ``` index index.php index.html index.htm index.nginx-debian.html; ``` 然后,找到以下行: ``` # pass PHP scripts to FastCGI server # #location ~ \.php$ { # include snippets/fastcgi-php.conf; # # # With php-fpm (or other unix sockets): # fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # # With php-cgi (or other tcp sockets): # # fastcgi_pass 127.0.0.1:9000; #} ``` 将其修为: ``` # pass PHP scripts to FastCGI server # location ~ \.php$ { include snippets/fastcgi-php.conf; # With php-fpm (or other unix sockets): fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # With php-cgi (or other tcp sockets): # fastcgi_pass 127.0.0.1:9000; } ``` (4)重启 Nginx:使用以下命令重启 Nginx: ``` sudo systemctl restart nginx ``` 3. 配置 WordPress (1)下载 WordPress:从 WordPress 官方网站下载最新版本的 WordPress 并解压缩到 /var/www/html 目录下。 (2)修权限:使用以下命令修 WordPress 文件夹的权限: ``` sudo chown -R www-data:www-data /var/www/html/wordpress sudo chmod -R 755 /var/www/html/wordpress ``` (3)创建 MySQL 数据库和用户:使用 MySQL 命令行创建新的数据库和用户: ``` sudo mysql -u root -p CREATE DATABASE wordpress; CREATE USER 'wordpressuser'@'localhost' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpressuser'@'localhost'; FLUSH PRIVILEGES; EXIT; ``` 在上面的命令中,将 password 替换为实际的密码。 (4)配置 WordPress:在浏览器中访问 http://your-server-ip-address/wordpress 并按照提示进行 WordPress配置。在配置数据库时,使用步骤 3 中创建的数据库和用户信息。 希望这些步骤对您有所帮助,如果您有任何疑问,请随时提出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值