🦄 2024 独立开发者训练营,一起创业!(早鸟优惠在6天后结束)查看介绍 / 立即报名 →

路径(二十一):在 nginx 上运行 Drupal 8

到现在,我们已经在本地搭建好了一个可以运行 PHP 应用的环境,这个环境可能会根据要运行的 PHP 应用来调整一下,比如设置地址重写,为应用的不同的位置去配置 nginx 的 location 等等。下面,我们在这个开发环境下去运行 Drupal 8 。

准备

先去修改一下本地电脑上的 hosts 文件,添加一条新的记录,让一个主机名指向虚拟机的 IP 地址,比如我要让 dp8.ninghao.dev 指向 192.168.33.10 这个 IP 地址,可以这样设置:

192.168.33.10 dp8.ninghao.dev

在 nginx 配置的目录(/etc/nginx/conf.d)下面创建一个新的配置文件,这个配置文件可以用服务器的主机名去命名一下,这里就是 dp8.ninghao.dev.conf 。下面是配置文件里的内容:

server {
  listen        80;
  server_name   dp8.ninghao.dev;
  root          /vagrant/drupal8/public;
  index         index.php index.html index.htm;

  location ~ \.php$ {
    fastcgi_pass 127.0.0.1:9000;
    include fastcgi.conf;
  }

}

上面的配置文件跟之前看到的差不多,listen 监听 80 端口,server_name 是 dp8.ninghao.dev ,root 根目录是 /vagrant/drupal8/public ,在虚拟机上需要去创建这个目录:

mkdir -p /vagrant/drupal8/public

重新加载 nginx 让新创建的配置文件生效:service nginx reload 。

安装 Drupal

进入到前面为 Drupal 配置的服务器的根目录(/vagrant/drupal8/public)。然后用 drush 去下载 Drupal :

drush dl drupal-8

返回:

Project drupal (8.0.0-beta6) downloaded to                           [success]
/vagrant/drupal8/public/drupal-8.0.0-beta6.
Project drupal contains:                                             [success]
 - 2 profiles: standard, minimal
 - 10 themes: stark, seven, twig, phptemplate, classy, bartik,
testing_multilingual, drupal_system_listing_compatible_test, testing,
migrate
 - 59 modules: views_ui, views, user, update, tracker, tour, toolbar,
text, telephone, taxonomy, system, syslog, statistics, simpletest,
shortcut, serialization, search, rest, responsive_image, rdf,
quickedit, path, options, node, migrate_drupal, menu_ui,
menu_link_content, locale, link, language, image, history, help, hal,
forum, filter, file, field_ui, field, entity_reference, editor,
dblog, datetime, contextual, content_translation, contact,
config_translation, config, comment, color, ckeditor, breakpoint,
book, block_content, block, basic_auth, ban, aggregator, action

完成以后,会显示都下载了什么,Drupal 的安装配置档案,主题还有模块。 Drupal 8 在写这篇文章的时候还在测试阶段。 输入 ls ,查看一下当前目录下面的东西,我这里会有一个 drupal-8.0.0-beta6 目录, 这就是用 drush 下载下来的 Drupal 8 ,把这个目录下面的所有的文件移动到当前的目录的下面:

mv drupal-8.0.0-beta6/.[!.]* .

因为 Drupal 的目录下面会包含一些 . 开头的文件,这些是被隐藏的文件,默认用 * 号表示的所有东西里面,不包含这些 . 开头的文件。所以,上面用了一个 .[!.]* ,它会包含 . 开头的文件。完成以后  ls -la 查看一下,确定 Drupal 的所有文件都转移到了当前目录的下面, 就可以把 drupal-8.0.0-beta6 这个目录删除掉了。

rm -rf drupal-8.0.0-beta6

中文翻译

安装 Drupal 之前,先中文翻译文件下载到安装的配置档案的目录下面。这个翻译文件的格式是 .po ,安装 Drupal 的时候,会把这个文件里的内容导入到数据库里。这个文件要放到你安装的时候选择的安装配置档案那个目录的下面的 translations 目录里,默认 Drupal 带了两个安装配置,standard, minimal,它们的位置是在 core/profiles 目录下面。在下面安装 Drupal 的时候我要选择 standard ,所以,把下载的翻译文件放到 core/profiles/standard/translations/ 这个目录下。

简单中文与繁体中文翻译的下载地址:

wget http://ftp.drupal.org/files/translations/8.x/drupal/drupal-8.0.0-beta4.zh-hans.po -P core/profiles/standard/translations/

创建数据库

为即将要安装的 Drupal 创建一个数据库,并且为这个数据库单独指定一个用户,也就是这个用户只能管理这个数据库,不能干涉其它的数据库,这样做会更安全一些。创建这个数据库可以使用 phpMyAdmin,或者直接登录到 MySQL / MariaDB 的控制台,执行:

mysql -u root -p

意思是想用 root 登录,回车之后,会提示你输入 root 这个用户的密码:

Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.40-MariaDB MariaDB Server

Copyright (c) 2000, 2014, Oracle, Monty Program Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

登录成功以后,可以使用 create database 去创建数据库:

create database drupal_8;

查看一下数据库系统里的数据库,输入:

show databases;

下面去创建一个用户,让他可以去管理 drupal_8 这个数据库:

grant all privileges on drupal_8.* to 'ninghao'@'localhost' identified by 'GcPpQJe0LE9h2Dlhtu69KoOP54RAVz1pN9H5a21fpKU=';

ninghao 是管理 drupal_8 这个数据库的用户,identified by 后面的 GcPpQJe0LE9h2Dlhtu69KoOP54RAVz1pN9H5a21fpKU= 是为这个用户设置的密码。

开始安装

创建了数据库,还有管理它的用户以后,就可以退出 MySQL / MariaDB 的控制台了,输入 exit 。确定你当前的位置是在 Drupal 所在的目录,然后用 drush 的 site-install 命令去安装 Drupal,用这个命令之前可以查看一下命令的使用说明,用 drush help site-install 。

drush site-install --db-url=mysql://ninghao:GcPpQJe0LE9h2Dlhtu69KoOP54RAVz1pN9H5a21fpKU=@localhost/drupal_8 --site-name=ninghao --locale=zh-hans -v

提示:

You are about to create a /vagrant/drupal8/public/sites/default/settings.php file and DROP all tables in your 'drupal_8' database. Do you want to continue? (y/n)

意思是,你确定要创建 Drupal 的配置文件并删除 drupal_8 数据库里的所有的数据表吗,确定,输入 y ,然后回车执行。完成以后,会提示:

Installation complete.  User name: admin  User password: tLC4JdVdmd     [ok]
恭喜,你安装了 Drupal!                                                  [status]
Command dispatch complete                                               [notice]

安装成功,User name 后面是你的 Drupal 网站的管理员的用户名,这里是 admin ,User password 后面是这个用户登录用的密码,复制一下这个密码。然后在浏览器打开安装好的 Drupal,用 admin 这个用户, 还有他的密码就可以登录到 Drupal 了。不过,你可能会遇到问题,这需要一些额外的 nginx 的配置,请看下面。

QQ20150130-1

nginx 配置

下面的配置里用了 try_files 还有 rewrite 指令。

server {
  listen        80;
  server_name   dp8.ninghao.dev;
  root          /vagrant/drupal8/public;
  index         index.php index.html index.htm;

  location / {
    index  index.php index.html index.htm;
    try_files $uri @rewrite;
  }

  location @rewrite {
    rewrite ^ /index.php;
  }

  location ~ \.php$ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi.conf;
  }

}
路径 Linux CentOS Drupal8 nginx

评论

皓哥您漏了一步,最好把设置文件夹权限也加上,chown -R nginx public。然后在进行安装,不然会报错,无论是用drush还是手动安装,至少我阿里云上是少补了这一步:)

嗯 ~~~ 不应该这样,只需要读取权限,然后只把网站用来保存上传文件的地方的拥有者改成 nginx 。

老师,根据你的方法是可以访问简洁网址了
但是如果我设置drupal的息定义URL 如链接为localhost/abc.html,这样就无法使用。

哈哈,按这个来配置:http://ninghao.net/blog/3630

微信好友

用微信扫描二维码,
加我好友。

微信公众号

用微信扫描二维码,
订阅宁皓网公众号。

240746680

用 QQ 扫描二维码,
加入宁皓网 QQ 群。

统计

14696
分钟
0
你学会了
0%
完成

社会化网络

关于

微信订阅号

扫描微信二维码关注宁皓网,每天进步一点