孙华斌 發表於 2020-4-18 21:07:00

centos下nginx搭建drupal 8

<p>环境:centos 7.6,nginx 1.5.18,php 7.3,drupal 8.8.5</p>
<p>drupal 8版本系统要求:</p>
<p>php 7.2以上</p>
<p>数据库要求:MySQL 5.5.3/MariaDB 5.5.20/Percona Server 5.5.8 以上版本并且使用InnoDB数据库引擎, 和 PDO 数据库扩展.</p>
<p>web服务器:apache、nginx、IIS等支持php的服务都可以。</p>
<p>一、nginx</p>
<p>1.nginx安装和配置</p>
<div class="cnblogs_code">
<pre># yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm  //安装最新版epel软件源<br># yum install -y yum-utils  //安装yum工具<br># <span style="color: rgba(0, 0, 255, 1)">yum</span> <span style="color: rgba(0, 0, 255, 1)">install</span> -y nginx<br># setenforce 0   //设置selinux模式,最好再修改配置文件/etc/selinux/config,把enforcing改为permissive</pre>
</div>
<div class="cnblogs_code">
<pre># firewall-cmd --add-service=http --<span style="color: rgba(0, 0, 0, 1)">permanent  //开放http服务
# firewall</span>-cmd --reload  //更新防火墙策略</pre>
</div>
<p>&nbsp;启动nginx:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)"># systemctl enable nginx
# systemctl start nginx</span></pre>
</div>
<p>2.验证nginx</p>
<p>浏览器访问服务器地址</p>
<p>二、php</p>
<p>1.php7.3安装</p>
<p>由于centos软件仓库的php版本太低,所以需要第三方仓库安装高版本的php,这里使用remirepo.net提供的仓库。</p>
<div class="cnblogs_code">
<pre># <span style="color: rgba(0, 0, 255, 1)">yum</span> <span style="color: rgba(0, 0, 255, 1)">install -y</span> https:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi-release-7.rpm    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">使用清华大学的镜像源安装</span></pre>
</div>
<p>由于remi网站是外国站点,访问不稳定,建议更改仓库的地址使用清华镜像。</p>
<p>修改<code class="highlighter-rouge">/etc/yum.repos.d/remi-safe.repo和remi-php73.repo</code>,将<code class="highlighter-rouge">mirrorlist</code>的行注释掉。</p>
<p>接下来,取消注释文件里<code class="highlighter-rouge">baseurl</code>开头的行,并将其中的<code class="highlighter-rouge">http://rpms.remirepo.net</code>替换成<code class="highlighter-rouge">https://mirrors.tuna.tsinghua.edu.cn/remi</code></p>
<p>也可以用如下命令自动替换:</p>
<div class="cnblogs_code">
<pre># <span style="color: rgba(0, 0, 255, 1)">sed</span> -e <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">s!^mirrorlist=!#mirrorlist=!g</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)"> \
   </span>-e <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">s!^#baseurl=!baseurl=!g</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)"> \
   </span>-e <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">s!http://rpms\.remirepo\.net!https://mirrors.tuna.tsinghua.edu.cn/remi!g</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)"> \
   </span>-i /etc/<span style="color: rgba(0, 0, 255, 1)">yum</span>.repos.d/remi-safe.repo /etc/<span style="color: rgba(0, 0, 255, 1)">yum</span>.repos.d/remi-php73.repo</pre>
</div>
<p>启用php 7.3版本的remi源</p>
<div class="cnblogs_code">
<pre># <span style="color: rgba(0, 0, 255, 1)">yum</span>-config-manager --enable remi-php73</pre>
</div>
<p>安装php 7.3和相关扩展</p>
<div class="cnblogs_code">
<pre># <span style="color: rgba(0, 0, 255, 1)">yum</span> <span style="color: rgba(0, 0, 255, 1)">install</span> php php-pdo php-opcache php-mbstring php-fpm php-gd php-xml php-pdo php-pecl-mcrypt php-mysqlnd</pre>
</div>
<p>修改fpm的配置文件/etc/php-fpm.d/www.conf</p>
<div class="cnblogs_code">
<pre>;listen = <span style="color: rgba(128, 0, 128, 1)">127.0</span>.<span style="color: rgba(128, 0, 128, 1)">0.1</span>:<span style="color: rgba(128, 0, 128, 1)">9000</span>    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">注释掉该行</span>
listen = /var/run/php-fpm/php-fpm.sock    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置socket</span>
<span style="color: rgba(0, 0, 0, 1)">
;listen.owner </span>= nobody    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">原设置默认就是注释掉的</span>
;listen.group = nobody    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">原设置默认就是注释掉的</span>
;listen.mode = <span style="color: rgba(128, 0, 128, 1)">0660</span>    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">原设置默认就是注释掉的</span>
listen.owner = nginx    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">增加该行</span>
listen.group = nginx    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">增加该行</span></pre>
</div>
<p>启动php-fpm</p>
<div class="cnblogs_code">
<pre># systemctl enable php-<span style="color: rgba(0, 0, 0, 1)">fpm
# systemctl start php</span>-fpm</pre>
</div>
<p>配置nginx,修改/etc/nginx/nginx.conf文件:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">user nginx;
worker_processes auto;
error_log </span>/var/log/nginx/<span style="color: rgba(0, 0, 0, 1)">error.log;
pid </span>/run/<span style="color: rgba(0, 0, 0, 1)">nginx.pid;

include </span>/usr/share/nginx/modules<span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">.conf;

events {
    worker_connections 1024;
}

http {
    access_log/var/log/nginx/access.log;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type      application/octet-stream;

    include /etc/nginx/conf.d/*.conf;

    server {
      listen       80;
      root         /var/www/html;

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

      location ~ '\.php$|^/update.php' {
      fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
      # Ensure the php file exists. Mitigates CVE-2019-11043
      try_files $fastcgi_script_name =404;
      include fastcgi_params;
      # Block httpoxy attacks. See </span><span style="color: rgba(0, 128, 0, 1); text-decoration: underline">https://httpoxy.org/.</span><span style="color: rgba(0, 128, 0, 1)">
      fastcgi_param HTTP_PROXY "";
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      fastcgi_param PATH_INFO $fastcgi_path_info;
      fastcgi_param QUERY_STRING $query_string;
      fastcgi_intercept_errors on;
      # PHP 7 socket location.
      fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
    }

    }
}</span></pre>
</div>
<p>增加php测试文件:</p>
<div class="cnblogs_code">
<pre># <span style="color: rgba(0, 0, 255, 1)">vi</span> /var/www/html/index.php</pre>
</div>
<p>在里面写入:</p>
<div class="cnblogs_code">
<pre>&lt;?<span style="color: rgba(0, 0, 0, 1)">php
</span><span style="color: rgba(0, 0, 255, 1)">echo</span> <span style="color: rgba(0, 128, 128, 1)">phpinfo</span><span style="color: rgba(0, 0, 0, 1)">();
</span>?&gt;</pre>
</div>
<p>重启nginx服务:</p>
<div class="cnblogs_code">
<pre># systemctl restart nginx</pre>
</div>
<p>2.验证php</p>
<p>&nbsp;浏览器访问服务器地址,显示出php的基础信息</p>
<p><img src="https://img2020.cnblogs.com/blog/1955452/202004/1955452-20200418171452417-1732182954.png"></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>三、mariadb数据库</p>
<p>1.安装数据库</p>
<div class="cnblogs_code">
<pre># <span style="color: rgba(0, 0, 255, 1)">yum</span> <span style="color: rgba(0, 0, 255, 1)">install -y</span> mariadb-<span style="color: rgba(0, 0, 0, 1)">server
# systemctl start mariadb
# systemctl enable mariadb</span></pre>
</div>
<p>&nbsp;2.配置数据库</p>
<p>数据库初始安全配置(请牢记设置的root密码):</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> mysql_secure_installation</span></pre>
</div>
<p><img src="https://img2020.cnblogs.com/blog/1955452/202004/1955452-20200418172950296-2115502018.png"></p>
<p>&nbsp;</p>
<p><img src="https://img2020.cnblogs.com/blog/1955452/202004/1955452-20200418173151428-348072698.png"></p>
<p>&nbsp;</p>
<p>&nbsp;配置drupal网站的数据库:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> mysql -u root -p    //登录mysql,会要求输入root密码</span></pre>
</div>
<p>&nbsp;接着操作:</p>
<div class="cnblogs_code">
<pre>MariaDB [(none)]&gt; create database drupal;   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">新建数据库drupal</span>
MariaDB [(none)]&gt; GRANT ALL PRIVILEGES ON drupal.* TO db_user@localhost IDENTIFIED BY 'pass123';   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置用户和权限,本地用户为db_user,密码为pass123</span>
MariaDB [(none)]&gt; <span style="color: rgba(0, 128, 128, 1)">FLUSH</span> PRIVILEGES;   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">更新权限</span>
MariaDB [(none)]&gt; <span style="color: rgba(0, 0, 255, 1)">exit</span>   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">退出mysql命令行</span></pre>
</div>
<p>&nbsp;</p>
<p>四、部署drupal网站</p>
<p>使用ssh工具把网站压缩包drupal-8.8.5.zip上传到服务器/var/www/中</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)"># cd </span>/var/<span style="color: rgba(0, 0, 0, 1)">www/  //切换到www目录中
# </span><span style="color: rgba(0, 0, 255, 1)">yum</span> <span style="color: rgba(0, 0, 255, 1)">install</span> -<span style="color: rgba(0, 0, 0, 1)">y unzip  //安装unzip软件包
# </span><span style="color: rgba(0, 0, 255, 1)">unzip</span> drupal-<span style="color: rgba(128, 0, 128, 1)">8.8</span>.<span style="color: rgba(128, 0, 128, 1)">5</span>.<span style="color: rgba(0, 0, 255, 1)">zip  //把压缩文件解压到当前目录</span></pre>
</div>
<p>修改nginx配置文件/etc/nginx/nginx.conf,以:</p>
<div class="cnblogs_code">
<pre># For more<span> information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/
<span>
user nginx;
worker_processes auto;
error_log /var/log/nginx/<span>error.log;
pid /run/<span>nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/<span>README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log/var/log/nginx/access.logmain;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    client_max_body_size 20m;
    include             /etc/nginx/mime.types;
    default_type      application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;


server {
   # server_name example.com;
    listen 80;
    root /var/www/drupal-8.8.5; ## &lt;-- Your only path reference.
    location = /favicon.ico {
      log_not_found off;
      access_log off;
    }

    location = /robots.txt {
      allow all;
      log_not_found off;
      access_log off;
    }

    # Very rarely should these ever be accessed outside of your lan
    location ~* \.(txt|log)$ {
      allow 192.168.0.0/16;
      deny all;
    }

    location ~ \..*/.*<span>\.php$ {
      return 403<span>;
    }

    location ~ ^/sites/.*/private/<span> {
      return 403<span>;
    }

    # Block access to scripts in<span> site files directory
    location ~ ^/sites/[^/]+/files/.*<span>\.php$ {
      deny all;
    }

    # Allow "Well-Known URIs" as per RFC 5785<span>
    location ~* ^/.well-known/<span> {
      allow all;
    }

    # Block access to "hidden"<span> files and directories whose names begin with a
    # period. This includes directories used by version control systems such
    # as Subversion or Git to store control files.
    location ~ (^|/<span>)\. {
      return 403<span>;
    }

    location /<span> {
      # try_files $uri @rewrite; # For Drupal &lt;= 6<span>
      try_files $uri /index.php?$query_string; # For Drupal &gt;= 7<span>
    }

    location @rewrite {
      rewrite ^/(.*)$ /index.php?q=$1<span>;
    }

    # Don't allow direct access to PHP files in the vendor directory.
    location ~ /vendor/.*<span>\.php$ {
      deny all;
      return 404<span>;
    }

    # Protect files and directories from prying eyes.
    location ~* \.(engine|inc|install|make|module|profile|po|sh|.*sql|theme|twig|tpl(\.php)?|xtmpl|yml)(~|\.sw|\.bak|\.orig|\.save)?$|^(\.(?!well-known).*|Entries.*|Repository|Root|Tag|Template|composer\.(json|lock)|web\.config)$|^#.*#$|\.php(~|\.sw|\.bak|\.orig|<span>\.save)$ {
      deny all;
      return 404<span>;
    }

    # In Drupal 8, we must also match new paths where the '.php' appears in<span>
    # the middle, such as update.php/<span>selection. The rule we use is strict,
    # and only allows this pattern with the update.php front controller.
    # This allows legacy path aliases in<span> the form of
    # blog/index.php/legacy-<span>path to continue to route to Drupal nodes. If
    # you do not have any paths like that, then<span> you might prefer to use a
    # laxer rule, such as:
    #   location ~ \.php(/|<span>$) {
    # The laxer rule will continue to work if<span> Drupal uses this new URL
    # pattern with front controllers other than update.php in<span> a future
    # release.
    location ~ '\.php$|^/update.php'<span> {
      fastcgi_split_path_info ^(.+?\.php)(|/.*<span>)$;
      # Ensure the php file exists. Mitigates CVE-2019-11043<span>
      try_files $fastcgi_script_name =404<span>;
      # Security note: If you're running a version of PHP older than the
      # latest 5.3, you should have "cgi.fix_pathinfo = 0;" in<span> php.ini.
      # See http://serverfault.com/q/627903/94922 for details.
<span>      include fastcgi_params;
      # Block httpoxy attacks. See https://httpoxy.org/.
      fastcgi_param HTTP_PROXY ""<span>;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      fastcgi_param PATH_INFO $fastcgi_path_info;
      fastcgi_param QUERY_STRING $query_string;
      fastcgi_intercept_errors on;
      # PHP 5<span> socket location.
      #fastcgi_pass unix:/var/run/php5-<span>fpm.sock;
      # PHP 7<span> socket location.
      fastcgi_pass unix:/var/run/php-fpm/php-<span>fpm.sock;
    }

    # Fighting with Styles?<span> This little gem is amazing.
    # location ~ ^/sites/.*/files/imagecache/ { # For Drupal &lt;= 6<span>
    location ~ ^/sites/.*/files/styles/ { # For Drupal &gt;= 7<span>
      try_files $uri @rewrite;
    }

    # Handle private files through Drupal. Private file's path can come
<span>    # with a language prefix.
    location ~ ^(/+)?/system/files/ { # For Drupal &gt;= 7<span>
      try_files $uri /index.php?<span>$query_string;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico|<span>svg)$ {
      try_files $uri @rewrite;
      expires max;
      log_not_found off;
    }
    # Enforce clean URLs
    # Removes index.php from urls like www.example.com/index.php/my-page --&gt; www.example.com/my-<span>page
    # Could be done with 301 for<span> permanent or other redirect codes.
    if ($request_uri ~* "^(.*/)index\.php(.*)"<span>) {
      return 307 $1$2<span>;
    }
}
}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
</div>
<p>重启nginx服务:</p>
<div class="cnblogs_code">
<pre># systemctl restart nginx</pre>
</div>
<p>网站部署过程中,会自动写入一些文件,需要先开启写入权限(部署完成后关闭写入权限):</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> chmod o+w /var/www/drupal-8.8.5/sites/default/<br># cp /var/www/drupal-8.8.5/sites/default/default.settings.php /var/www/drupal-8.8.5/sites/default/settings.php    //复制settings.php文件<br># chmod o+w /var/www/drupal-8.8.5/sites/default/settings.php   //设置配置文件权限</span></pre>
</div>
<p>浏览器访问服务器地址,自动开始网站的初始化部署</p>
<p><img src="https://img2020.cnblogs.com/blog/1955452/202004/1955452-20200418180515237-1424591883.png"></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;<img src="https://img2020.cnblogs.com/blog/1955452/202004/1955452-20200418181203180-916449680.png"></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;配置数据库连接信息,使用之前创建的数据库、用户和密码:</p>
<p><img src="https://img2020.cnblogs.com/blog/1955452/202004/1955452-20200418183209171-752458637.png"></p>
<p>&nbsp;</p>
<p>&nbsp;<img src="https://img2020.cnblogs.com/blog/1955452/202004/1955452-20200418183246208-964332081.png"></p>
<p>&nbsp;</p>
<p>&nbsp;配置网站基本信息,包括创建管理员帐号</p>
<p><img src="https://img2020.cnblogs.com/blog/1955452/202004/1955452-20200418183833687-792163353.png"></p>
<p>&nbsp;</p>
<p>&nbsp;<img src="https://img2020.cnblogs.com/blog/1955452/202004/1955452-20200418183852037-1212489115.png"></p>
<p>&nbsp;</p>
<p>&nbsp;安装完毕后,自动访问网站:<img src="https://img2020.cnblogs.com/blog/1955452/202004/1955452-20200418184014622-1134769039.png"></p>
<p>&nbsp;</p>
<p>安全考虑,移除之前设置的写入权限:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> chmod o-w /var/www/drupal-8.8.5/sites/default/settings.php</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> chmod o-w /var/www/drupal-8.8.5/sites/default</span></pre>
</div>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/learn2teach/p/12721315.html
頁: [1]
查看完整版本: centos下nginx搭建drupal 8