CentOS 7 下使用 MySQL 5.7 + PHP 7 + Apache 部署 Nextcloud 安装环境:CentOS7 64位 最小安装,勾选最小安装里的所有组件 一、脚本YUM源安装: 1.yum install wget #安装下载工具wget 2.wget http://www.atomicorp.com/installers/atomic #下载atomic yum源,配置 第三方yum源 3. sh ./atomic #脚本执行 4. yum check-update #更新yum软件包 二、Mysql 安装 1、配置YUM源 在MySQL官网中下载YUM源rpm安装包:http://dev.mysql.com/downloads/repo/yum/
shell> wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
shell> yum localinstall mysql57-community-release-el7-8.noarch.rpm 检查mysql源是否安装成功 shell> yum repolist enabled | grep “mysql.-community.”
看到上图所示表示安装成功 2、安装MySQL shell> yum install mysql-community-server 3、启动MySQL服务 shell> systemctl start mysqld 查看MySQL的启动状态 shell> systemctl status mysqld ● mysqld.service - MySQL Server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled) Active: active (running) since 五 2016-06-24 04:37:37 CST; 35min ago Main PID: 2888 (mysqld) CGroup: /system.slice/mysqld.service └─2888 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
6月 24 04:37:36 localhost.localdomain systemd[1]: Starting MySQL Server… 6月 24 04:37:37 localhost.localdomain systemd[1]: Started MySQL Server. 4、开机启动 shell> systemctl enable mysqld shell> systemctl daemon-reload 5、修改root默认密码 mysql安装完成之后,在/var/log/mysqld.log文件中给root生成了一个默认密码。通过下面的方式找到root默认密码,然后登录mysql进行修改: shell> grep ‘temporary password’ /var/log/mysqld.log
shell> mysql -uroot -p mysql> ALTER USER ‘root’@‘localhost’ IDENTIFIED BY ‘MyNewPass4!’; 或者 mysql> set password for ‘root’@‘localhost’=password(‘MyNewPass4!’);
6、为NextCloud创建数据库及用户 mysql -u root -p MariaDB [(none)]> CREATE DATABASE nextcloud_db; MariaDB [(none)]> GRANT ALL ON nextcloud_db.* TO ‘nextcloud’@‘localhost’ IDENTIFIED BY ‘test1234’; MariaDB [(none)]> FLUSH PRIVILEGES; MariaDB [(none)]> exit 上面的SQL语句创建了一个叫nextcloud_db的数据库和一个叫nextcloud的用户(密码test1234)。
三、安装Apache和PHP
安装 PHP
$ yum -y install RPM
$ rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm $ rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
$ yum -y install php71w php71w-devel php71w-mysqlnd php71w-xml php71w-mbstring php71w-gd
安装 Apache
$ yum -y install httpd
$ systemctl enable httpd
$ systemctl daemon-reload
配置HSTS HSTS(HTTP Strict Transport Security)国际互联网工程组织IETE正在推行一种新的Web安全协议 HSTS的作用是强制客户端(如浏览器)使用HTTPS与服务器创建连接。
到/etc/httpd/conf/目录下
cd /etc/httpd/conf
vim httpd.conf
添加下面的文字
systemctl restart httpd
四、设置缓存
安装APCu yum install -y php-pecl-apcu systemctl restart httpd
安装Memcached和php70w-pecl-memcached yum install memcached php70w-pecl-memcached
systemctl start memcached yum install memcached php-pecl-memcached systemctl start memcached
验证Memcached守护程序是否正在使用ps ax运行:
ps ax | grep memcached 19563 ? Sl 0:02 /usr/bin/memcached -m 64 -p 11211 -u memcache -l
Apcu配置
在owncoud/nextcloud安装目录下找到config.php(config/config.php)加入
‘memcache.local’ => ‘\OC\Memcache\APCu’,
重启apache服务
五、开启opcache
1、打开/etc/php.d/opcache.ini文件
[opcache] zend_extension = “G:/PHP/php-5.5.6-Win32-VC11-x64/ext/php_opcache.dll”
; Zend Optimizer + 的开关, 关闭时代码不再优化. opcache.enable=1
; Determines if Zend OPCache is enabled for the CLI version of PHP opcache.enable_cli=1
; Zend Optimizer + 共享内存的大小, 总共能够存储多少预编译的 PHP 代码(单位:MB) ; 推荐 128 opcache.memory_consumption=64
; Zend Optimizer + 暂存池中字符串的占内存总量.(单位:MB) ; 推荐 8 opcache.interned_strings_buffer=4
; 最大缓存的文件数目 200 到 100000 之间 ; 推荐 4000 opcache.max_accelerated_files=2000
; 内存“浪费”达到此值对应的百分比,就会发起一个重启调度. opcache.max_wasted_percentage=5
; 开启这条指令, Zend Optimizer + 会自动将当前工作目录的名字追加到脚本键上, ; 以此消除同名文件间的键值命名冲突.关闭这条指令会提升性能, ; 但是会对已存在的应用造成破坏. opcache.use_cwd=0
; 开启文件时间戳验证 opcache.validate_timestamps=1
; 2s检查一次文件更新 注意:0是一直检查不是关闭 ; 推荐 60 opcache.revalidate_freq=2
; 允许或禁止在 include_path 中进行文件搜索的优化 ;opcache.revalidate_path=0
; 是否保存文件/函数的注释 如果apigen、Doctrine、 ZF2、 PHPUnit需要文件注释 ; 推荐 0 opcache.save_comments=1
; 是否加载文件/函数的注释 ;opcache.load_comments=1
; 打开快速关闭, 打开这个在PHP Request Shutdown的时候会收内存的速度会提高 ; 推荐 1 opcache.fast_shutdown=1
;允许覆盖文件存在(file_exists等)的优化特性。 ;opcache.enable_file_override=0
; 定义启动多少个优化过程 ;opcache.optimization_level=0xffffffff
; 启用此Hack可以暂时性的解决”can’t redeclare class”错误. ;opcache.inherited_hack=1
; 启用此Hack可以暂时性的解决”can’t redeclare class”错误. ;opcache.dups_fix=0
; 设置不缓存的黑名单 ; 不缓存指定目录下cache_开头的PHP文件. /png/www/example.com/public_html/cache/cache_ ;opcache.blacklist_filename=
; 通过文件大小屏除大文件的缓存.默认情况下所有的文件都会被缓存. ;opcache.max_file_size=0
; 每 N 次请求检查一次缓存校验.默认值0表示检查被禁用了. ; 由于计算校验值有损性能,这个指令应当紧紧在开发调试的时候开启. ;opcache.consistency_checks=0
; 从缓存不被访问后,等待多久后(单位为秒)调度重启 ;opcache.force_restart_timeout=180
; 错误日志文件名.留空表示使用标准错误输出(stderr). ;opcache.error_log=
; 将错误信息写入到服务器(Apache等)日志 ;opcache.log_verbosity_level=1
; 内存共享的首选后台.留空则是让系统选择. ;opcache.preferred_memory_model=
; 防止共享内存在脚本执行期间被意外写入, 仅用于内部调试. ;opcache.protect_memory=0
六、开启Rewrite 网站是根目录的话:找到 代码如下 Options FollowSymLinks AllowOverride None 将上面的None改为All 如果站点不在根目录,设置如下: 代码如下 <Directory “/var/www/html/my_directory”> Order allow,deny Allow from all AllowOverride All OK,然后重启服务器 方法一
RewriteEngine On RewriteBase / RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ https://yusky.me/$1 [R=301,L]
#这样跳转的好处是独立IP主机也支持,访问ip能自动跳转到https,针对虚拟主机设置,放在站点的部分,同时写入到网站.htaccess的
方法二
RewriteEngine on RewriteCond %{SERVER_PORT} !^443$ RewriteRule ^(.*)?$ https://%{SERVER_NAME}/$1 [R=301,L]
#整站跳转,放在站点的部分,同时写入到网站.htaccess的
方法三
RewriteEngine on RewriteBase /yourfolder RewriteCond %{SERVER_PORT} !^443$ #RewriteRule ^(.*)?$ https://%{SERVER_NAME}/ 1 [ R = 301 , L ] R e w r i t e R u l e . ∗ 1 [R=301,L] RewriteRule ^.* 1[R=301,L]RewriteRule.∗ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
#以上至针对某个目录跳转, yourfolder就是目录名
方法四
redirect 301 /您的网页 https://你的主机+网页 #至针对某个网页跳转
方法五
RewriteEngine on RewriteCond %{SERVER_PORT} !^443$ RewriteCond %{REQUEST_URI} !^/tz.php RewriteRule (.*) https://%{SERVER_NAME}/$1 [R]
解释:
%{SERVER_PORT} —— 访问端口 %{REQUEST_URI} —— 比如如果url是 http://localhost/tz.php,则是指 /tz.php %{SERVER_NAME} —— 比如如果url是 http://localhost/tz.php,则是指 localhost
以上规则的意思是,如果访问的url的端口不是443,且访问页面不是tz.php,则应用RewriteRule这条规则。 这样便实现了:访问了 https://www.trustauth.cn/或者http://www.trustauth.cn/ssl 等页面的时候会自动跳转到https://www.trustauth.cn/或者 https://www.trustauth.cn/ssl。 但在此之前,站长们必须先确保HTTP升级到HTTPS的SSL证书是合法安全的,并且已经正确无误部署在服务器上。
SSL证书是HTTP明文协议升级HTTPS加密协议的重要渠道,是网络安全传输的加密通道。关于更多SSL证书的资讯,请关注数安时代(GDCA)。GDCA致力于网络信息安全,已通过WebTrust 的国际认证,是全球可信任的证书签发机构。GDCA专业技术团队将根据用户具体情况为其提供最优的产品选择建议,并针对不同的应用或服务器要求提供专业对应的HTTPS解决方案。 七、部署 Nextcloud
1、下载NextCloud 下载地址:https://download.nextcloud.com/server/releases/,找到最新版本下载: yum install wget bzip2 -Y wget https://download.nextcloud.com/server/releases/nextcloud-9.0.53.tar.bz2 解压tar包到var/www/html: tar -jxpvf nextcloud-9.0.53.tar.bz2 -C /var/www/html/ 更改目录权限: chown -R apache:apache /var/www/html/nextcloud
创建并设置 /var/lib/php/session 目录权限 777
2、为 Nextcloud 配置 SELinux 和 FirewallD 规则
我们将以强制模式运行 SELinux,因此需要一个 SELinux 管理工具来为 Nextcloud 配置 SELinux。
使用以下命令安装 SELinux 管理工具
yum -y install policycoreutils-python
然后以 root 用户来运行以下命令,以便让 Nextcloud 运行于 SELinux 环境之下。如果你是用的其他名称的目录,记得将 nextcloud 替换掉。
semanage fcontext -a -t httpd_sys_rw_content_t ‘/var/www/html/nextcloud/data(/.)?’ semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/html/nextcloud/config(/.)?’ semanage fcontext -a -t httpd_sys_rw_content_t ‘/var/www/html/nextcloud/apps(/.)?’ semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/html/nextcloud/assets(/.)?’ semanage fcontext -a -t httpd_sys_rw_content_t ‘/var/www/html/nextcloud/.htaccess’ semanage fcontext -a -t httpd_sys_rw_content_t ‘/var/www/html/nextcloud/.user.ini’ semanage fcontext -a -t httpd_sys_rw_content_t ‘/var/www/html/3rdparty/aws/aws-sdk-php/src/data/logs(/.*)?’
restorecon -Rv ‘/var/www/html/3rdparty/aws/aws-sdk-php/src/data/logs/’ restorecon -Rv ‘/var/www/html/nextcloud/’
3、Nextcloud 安装
打开你的 Web 浏览器,输入你为 Nextcloud 设置的域名,然后会重定向到安全性更好的 https 连接。 设置你的管理员用户名和密码,然后输入数据验证信息,点击 ‘完成安装 (Finish Setup)’。
Nextcloud 安装
CentOS 7 服务器上通过使用 Apache、PHP7、Mysql 完成了 Nextcloud 的安装。