LAMP环境搭建(Ubuntu14.04 For OwnCloud)

本日志主要记录我配置OwnCloud环境的过程,注意,针对OwnCloud,至于某些模块开启和关闭问题,可以根据具体情况自行调整.另外我操作的系统是Ubuntu14.04LTS.
这里推荐一个很不错的文档位置:https://www.linode.com/docs/

感悟:

  1. 遇坑过坑,佛挡杀佛.
  2. 看文档很重要,当然,能去官方的GitHub看看更好.
  3. 最大坑解决过程: OwnCloud文档 -> MySQL文档 -> MariaDB文档 -> Google -> MySQL/MariaDB文档 -> Github/owncloud/core -> issue ->search for my ailling key-> get it!!

一. 更换源

参照:https://wiki.ubuntu.org.cn/源列表
我曾经用的(网易):http://mirrors.163.com/.help/ubuntu.html

$ sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
$ wget http://mirrors.163.com/.help/sources.list.trusty
$ sudo cp sources.list.trusty /etc/apt/sources.list
# 刷新列表
$ sudo apt-get update
# 升级系统,可选!
$ sudo apt-get upgrade 

我现在用的(香港中文大学):

deb http://ftp.cuhk.edu.hk/pub/Linux/ubuntu trusty main restricted universe multiverse
deb http://ftp.cuhk.edu.hk/pub/Linux/ubuntu trusty-security main restricted universe multiverse
deb http://ftp.cuhk.edu.hk/pub/Linux/ubuntu trusty-updates main restricted universe multiverse
deb http://ftp.cuhk.edu.hk/pub/Linux/ubuntu trusty-backports main restricted universe multiverse
deb http://ftp.cuhk.edu.hk/pub/Linux/ubuntu trusty-proposed main restricted universe multiverse
deb-src http://ftp.cuhk.edu.hk/pub/Linux/ubuntu trusty main restricted universe multiverse
deb-src http://ftp.cuhk.edu.hk/pub/Linux/ubuntu trusty-security main restricted universe multiverse
deb-src http://ftp.cuhk.edu.hk/pub/Linux/ubuntu trusty-updates main restricted universe multiverse
deb-src http://ftp.cuhk.edu.hk/pub/Linux/ubuntu trusty-backports main restricted universe multiverse
deb-src http://ftp.cuhk.edu.hk/pub/Linux/ubuntu trusty-proposed main restricted universe

二. 初始化操作

其他操作系统可以参考:https://www.linode.com/docs/getting-started

1. 设置Hostname

你可以之间编辑:

$ sudo nano /etc/hostname

When you’re finished editing, press Control-X, then Y to save the changes and Enter to confirm.

或者:

$ echo "hostname" > /etc/hostname
$ hostname -F /etc/hostname

2. 设置时区

这个还是很有必要的,否则你看不懂服务器日志的实际时间.

$ sudo dpkg-reconfigure tzdata

三. Apache安装和基本配置

参考:
http://tecadmin.net/install-php-7-0-apache-2-4-mysql-5-6-on-ubuntu/
https://segmentfault.com/a/1190000004687322
http://www.rolandolucio.com/howto/2016/02/05/php7-mysql5.7-apache2-ubuntu-lamp.html
https://launchpad.net/~ondrej/

安装 Apache 2.4

(1)通过apt安装Apache

$ sudo apt-get install apache2
  • Error:

AH00558: apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1. Set the ‘ServerName’ directive globally to suppress this message

  • Solution:

If you insert a

ServerName localhost   

in either httpd.conf or apache2.conf in /etc/apache2 and restart apache the notice will disappear.

(2)关闭 KeepAlive

编辑apache主配置文件/etc/apache2/apache2.conf ,修改 KeepAlive 设置 KeepAlive Off

(3)启用 prefork 模块

Apache默认的 multi-processing 模块( MPM ) 是一个 event 模块, 但是 php 默认是使用 prefork 模块,禁用 event 模块,启用 prefork 模块:

$ sudo a2dismod mpm_event
$ sudo a2enmod mpm_prefork

(4)重启 apache2 服务

$ sudo service apache2 restart

四. Install PHP 7

考虑到蛋疼的中华大城域网的原因,很可能安装会遇到某些模块找不到的情况,就需要选择的编译源码安装(方法一),如果一切环境良好,可以选择便捷的方法二.

编译源码安装(方法一):

请参考我的的博客:
http://huifeng.me/2016/05/18/LEMP-step-to-step/#3-__u5B89_u88C5PHP_7-0-6

快速源安装(方法二):

First install python-software-properties package on your system which provides add-apt-repository command then use the following set of commands to add PPA for PHP 7 in your Ubuntu system and install it.

sudo apt-cache search php7.0

搜索看是否有 php7.0,如果没有就添加源,然后更新,然后安装:

$ sudo apt-get install python-software-properties
$ sudo apt-get install software-properties-common
$ sudo add-apt-repository ppa:ondrej/php
$ sudo apt-get update
$ sudo apt-get install -y php7.0

或者:

$ sudo nano /etc/apt/sources.list

添加:

deb http://ppa.launchpad.net/ondrej/php/ubuntu trusty main
deb-src http://ppa.launchpad.net/ondrej/php/ubuntu trusty main

然后:

$ sudo apt-get update
$ sudo apt-get install -y php7.0

1. 查看安装情况:

查看版本:

$ php -v

查看安装的模块:

$ php -m | grep -i <module_name>

查看可以安装的模块:

$ sudo apt-cache search php7-*

2. 安装模块

$ sudo apt-get install libapache2-mod-php7.0 php7.0-mysql php7.0-curl php7.0-json php7.0-zip php7.0-xml php7.0-gd php7.0-mcrypt php7.0-mbstring php7.0-imagick

Tips: Ubuntu Error->
locale: Cannot set LC_CTYPE to default locale: No such file or directory
Wiki:http://wiki.ubuntu.org.cn/%E4%BF%AE%E6%94%B9locale
Solution:

  • locale 显示当前语言环境
  • sudo nano /etc/default/locale 来修改语言配置
  • 安装需要的语言环境,我这里设置的是 zh_CN.UTF-8,所以:sudo locale-gen zh_CN.UTF-8
  • Over!

OwnCloud 必须需要的模块

  • PHP module ctype (if>4.2.0 enabled by default)
  • PHP module dom (enabled by default)
  • PHP module GD
  • PHP module iconv (enabled by default)
  • PHP module JSON
  • PHP module libxml (Linux package libxml2 must be >=2.7.0)
  • PHP module mb multibyte
  • PHP module posix (enabled by default)
  • PHP module SimpleXML
  • PHP module XMLWriter (if>5.1.2 enabled by default)
  • PHP module zip
  • PHP module zlib

之前的命令已经全部满足了这些模块的安装需求.

OwnCloud 推荐安装的模块

  • PHP module curl (highly recommended, some functionality, e.g. HTTP user authentication, depends on this)
  • PHP module fileinfo (highly recommended, enhances file analysis performance)
    • sudo apt-get install php7.0-common
  • PHP module bz2 (recommended, required for extraction of apps)
    • sudo apt-get install php7.0-bz2
  • PHP module intl (increases language translation performance and fixes sorting of non-ASCII characters)
    • sudo apt-get install php7.0-intl
  • PHP module mcrypt (increases file encryption performance)
    • sudo apt-get install php7.0-mcrypt
  • PHP module openssl (required for accessing HTTPS resources)
    • sudo apt-get install openssl ,更多操作待续…

OwnCloud 提到的额外的一些模块

个别app或者会用到以防不时之需,就安装上吧:

Required for specific apps:

  • PHP module ldap (for LDAP integration)
    • sudo apt-get install php7.0-ldap
  • PHP module smbclient (SMB/CIFS integration, see SMB/CIFS)
    • sudo apt-get install php7.0-smbclient
  • PHP module ftp (for FTP storage / external user authentication)
    • be installed by php7.0-common
  • PHP module imap (for external user authentication)
    • sudo apt-get install php7.0-imap

Recommended for specific apps (optional):

  • PHP module exif (for image rotation in pictures app)
    • sudo apt-get install php7.0-exif
  • PHP module gmp (for SFTP storage)
    • sudo apt-get install php7.0-gmp

3. phpinfo

写测试页测试下 /var/www/html/phpinfo.php :

<?php
phpinfo();

浏览: http://YourServerIp/phpinfo.php ,应该没问题,如果出现php文件没有被解析,重新运行安装模块命令吧: sudo apt-get install libapache2-mod-php7.0

4. PHP日志设定

Create the /var/log/php/ directory for the PHP error log:

$ sudo mkdir -p /var/log/php

Change the owner of the /var/log/php/ directory to www-data, which the PHP user runs as:

$ sudo chown www-data /var/log/php

五. 安装MariaDB10

参考:
https://mariadb.org/learn/
https://mariadb.com/kb/en/mariadb/getting-installing-and-upgrading-mariadb/

Than(香港的这个靠谱些):

$ sudo apt-get install software-properties-common
$ sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db
$ sudo add-apt-repository 'deb [arch=amd64,i386] http://mariadb.nethub.com.hk/repo/10.1/ubuntu trusty main'

然后官方建议添加个新的 sourse.list 文件到源目录下,方便以后更新更快捷,我的文件如下:

/etc/apt/sources.list.d/MariaDB.list

# MariaDB 10.1 repository list - created 2016-05-23 02:34 UTC
# http://mariadb.org/mariadb/repositories/
deb [arch=amd64,i386] http://mariadb.nethub.com.hk/repo/10.1/ubuntu trusty main
deb-src http://mariadb.nethub.com.hk/repo/10.1/ubuntu trusty main

然后,Install MariaDB 10.1.14 with blow once your add the key and the repository successful:

$ sudo apt-get update
$ sudo apt-get install mariadb-server

这里解决下 update 时问题:

W: GPG 错误:http://ppa.launchpad.net trusty InRelease: 由于没有公钥,无法验证下列签名: NO_PUBKEY 4F4EA0AAE5267A6C

从 hkp 服务器 wwwkeys.pgp.net下载并导入缺失的公钥(采用root权限)

$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 4F4EA0AAE5267A6C

六. 配置Apache

文档进行到这一步了:
https://doc.owncloud.org/server/9.0/admin_manual/installation/source_installation.html#example-installation-on-ubuntu-14-04-lts-server

接下来将进行 Apache 和 PHP 的配置, OwnCloud 命令行的安装等等,按照 OwnCloud 文档一步一步来吧.

整个过程虽然比较慢,但是我想这样下来收货绝对不少,至少提高了过坑能力.

1. Get Owncloud

Now download the archive of the latest ownCloud version:

Go to the ownCloud Download Page.

My command for download,unpacking and copy unpacks to its final destination:

$ wget https://download.owncloud.org/community/owncloud-9.0.2.tar.bz2
$ tar jxvf owncloud-9.0.2.tar.bz2
$ sudo cp -r owncloud /var/www/

2. Apache Web Server Configuration

(1) Create a /etc/apache2/sites-available/owncloud.conf file with these lines in it,replacing the Directory and other filepaths with your own filepaths:

Alias /owncloud "/var/www/owncloud/"

<Directory /var/www/owncloud/>
  Options +FollowSymlinks
  AllowOverride All

 <IfModule mod_dav.c>
  Dav off
 </IfModule>

 SetEnv HOME /var/www/owncloud
 SetEnv HTTP_HOME /var/www/owncloud

</Directory>

(2) Then create a symlink to /etc/apache2/sites-enabled to enable your new website, replacing owncloud with your config file name:

$ sudo ln -s /etc/apache2/sites-available/owncloud.conf /etc/apache2/sites-enabled/owncloud.conf

Or:

$ sudo a2ensite owncloud.conf

Than:

$ sudo service apache2 restart

Additional Apache Configurations

  • For ownCloud to work correctly, we need the module mod_rewrite. Enable it by running:

    sudo a2enmod rewrite
  • Additional recommended modules are mod_headers, mod_env, mod_dir and mod_mime:

    $ sudo a2enmod headers
    $ sudo a2enmod env
    $ sudo a2enmod dir
    $ sudo a2enmod mime
  • 你必须为 Owncloud 禁止任何服务器配置认证,add the following line in the <Directory section:

    Satisfy Any

Enable Vhost

(1) Disable the default Apache virtual host:

$ sudo a2dissite *default

(2) Create log folders

$ mkdir -p /var/www/owncloud/log/

(3) change config file /etc/apache2/sites-available/owncloud.conf to blow:

<VirtualHost *:80>
  # Admin email, Server Name (domain name), and any aliases
  ServerAdmin wedojava@gmail.com
  ServerName  yun.app
  # ServerAlias owncloud.app

  # Index file and Document Root (where the public files are located)
  DirectoryIndex index.html index.php
  DocumentRoot /var/www/owncloud
  # Log file locations
  LogLevel warn
  ErrorLog  /var/www/owncloud/log/error.log
  CustomLog /var/www/owncloud/log/access.log combined
</VirtualHost>

至此,我已经可以通过 http://yun.app 访问到owncloud站点了,可以安装了.


七. 配置MySQL/MariaDB

OwnCloud 通过 TRANSACTION_READ_COMMITTED 来交换隔离,以避免在高调用率的情况下丢失数据(比如,你有很多客户,客户用客户端同时同步数据,频繁操作).解决这个问题,需要禁用或者正确的配置 MySQL/MariaDB 的 binary logging 配置.

默认安装好的数据库在通过浏览器访问并安装的时候会遇到错误信息:

An unhandled exception has been thrown: exception ‘PDOException’ with message
‘SQLSTATE[HY000]: General error: 1665 Cannot execute statement: impossible to
write to binary log since BINLOG_FORMAT = STATEMENT and at least one table
uses a storage engine limited to row-based logging. InnoDB is limited to
row-logging when transaction isolation level is READ COMMITTED or READ
UNCOMMITTED.’

There are two solutions. One is to disable binary logging. Binary logging records all changes to your database, and how long each change took. The purpose of binary logging is to enable replication and to support backup operations.

The other is to change the BINLOG_FORMAT = STATEMENT in your database configuration file, or possibly in your database startup script, to BINLOG_FORMAT = MIXED. See Overview of the Binary Log and The Binary Log for detailed information.

下面开始修改 BINGLOG:
我安装的是 10.1.14-MariaDB-1~trusty, 主配置文件在:/etc/mysql/my.cnf

版本号怎么查? mysql -u root -p 登录,然后输入 show variables like "version"; 即可显示版本号了.

1. 解决 BINLOG_FORMAT 问题.

真心查阅里很多地方,从MySQL和OwnCloud官方文档到GitHub:
http://dev.mysql.com/doc/refman/5.7/en/binary-log-setting.html
这里MySQL确实说了怎么设置,但是重启服务器又没了!!!Owncloud也是指向里类似的解决问题的办法.
可能很多解决问题的都是大神吧,他们说的并不是很清楚,只是说添加一行binlog_format = MIXED到my.cnf,但是添加错误真的不会起作用的!!最终找到一个好人,他说中了我的痛点,怎么处理?
https://github.com/owncloud/core/issues/16008 看@saamo的回答总算明白是哪里的问题了.

It was already said somewhere above but make sure to place the option under the [mysqld] header in /etc/mysql/my.cnf unless it won’t work.

[mysqld]
binlog_format = MIXED

然后重启服务器,问题解决!

然后我在安装的时候遇到的上面的出错信息,下面分布解决,你可以跳过第一步,之间进入第二步.

2. phpMyAdmin

先安装个phpMyAdmin吧,可以参考:
https://launchpad.net/~nijel/+archive/ubuntu/phpmyadmin
https://docs.phpmyadmin.net/en/latest/setup.html
我是用的 Composer 装的,推荐此法!!
当然直接下载也是可以用的,需要注意的是截止目前,4.6对中文的支持不是很好,所以会报错: Ignoring unsupported language code.

暂时可以这么解决:

在登录页修改语言为英文,或者修改配置文件 libraries/config.default.php 的这一行:

$cfg['Lang'] = 'en';

3. Configuring a MySQL or MariaDB Database

If you decide to use a MySQL or MariaDB database, ensure the following:

  • That you have installed and enabled the pdo_mysql extension in PHP
  • That the mysql.default_socket points to the correct socket (if the database runs on the same server as ownCloud).

Now you need to create a database user and the database itself by using the MySQL command line interface. The database tables will be created by ownCloud when you login for the first time.

(1) 生成密码
这里新开一个终端,用命令随机生成一个用户密码:openssl rand -base64 12,我测试生成的是6Vh44YmKoOcR2NWo
经常我们会忘记了数据库的用户名,所以,我推荐一个数据库就用和数据库名一样的用户名,密码随机生成.

(2) 创建用户和数据库
To start the MySQL command line mode use:

$ mysql -uroot -p

Then a mysql> or MariaDB [root]> prompt will appear. Now enter the following lines and confirm them with the enter key:

MariaDB [root]> CREATE USER 'owncloud'@'localhost' IDENTIFIED BY '刚才随机生成的密码';
MariaDB [root]> CREATE DATABASE IF NOT EXISTS owncloud;
MariaDB [root]> GRANT ALL PRIVILEGES ON owncloud.* TO 'owncloud'@'localhost' IDENTIFIED BY '刚才随机生成的密码';

You can quit the prompt by entering:

quit

注意,到这一步,已经可以完全安装好OwnCloud了,虽然下面是可选项,但是最好还是设置号ssl和pretty url. ↓


Pretty URLs

Pretty URLs are created automatically when .htaccess is writable by the HTTP user, mod_env and mod_rewrite are installed, and 'overwrite.cli.url' in your config.php is set to any non-null value.

Enabling SSL

官方强烈建议启用ssl.

Apache installed under Ubuntu comes already set-up with a simple self-signed certificate. All you have to do is to enable the ssl module and the default site. Open a terminal and run:

$ sudo a2enmod ssl
$ sudo a2ensite default-ssl
$ sudo service apache2 reload