LNMP环境搭建nextCloud

概述

LNMP环境是指,linux+nginx+mysql+php,本次搭建的linux系统版本为CentOS7,如果已经具备这些环境可直接从第四步开始。下面是详细搭建步骤。

1.先删除系统可能自带的PHP,nginx

rpm -qa|grep php
rpm -qa|grep nginx

如果没有任何信息,表示系统是干净的。

2.安装yum的epel源,安装nginx,php

安装yum的epel源

yum -y install epel-release

 

安装nginx

yum install -y nginx

安装php的源

rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

安装php已经会用到扩展

yum install -y php70w-devel php70w-pear php70w-pecl php70w-gd php70w-opcache php70w-cli php70w-pdo php70w-process php70w-pecl-apcu php70w-mcrypt php70w-mysql php70w-fpm php70w-pecl-memcached php70w-common php70w-xml php70w-mbstring php70w-pecl-igbinary php70w-json php70w-pecl-apcu-devel  php70w-intl

检查是否安装成功

nginx -v   
 php -v

到此,这两个重要组件就安装成功了,然后我们来安装mysql。

3.MySql数据库安装

首先去mysql官网下载一个mysql80-community-release-el*-*.noarch.rpm 的链接,然后移动到自己的安装路径,执行下面命令

rpm -ivh mysql80-community-release-el*-*.noarch.rpm

修改mysql安装配置:将自己要安装的版本enable 设置为1,不安装的版本设置为0。

vim /etc/yum.repos.d/mysql-community.repo

开始安装mysql

yum clean all
 yum makecache fast
 yum install mysql-community-server mysql-community-devel mysql-community-client –y
 service mysqld start
 chkconfig mysqld on
 mysql_secure_installation

4.在MySql里面建库

上传的文件存储在该库中,首先登陆数据库,再创建数据库(数据库名自定义)。

mysql -u root -p
create database nextcloud_db;

授权给自定义用户,用户名:nextclouduser,密码:nextcloudpassword(用户名密码可以自定义),然后刷新权限。

grant all privileges on nextcloud_db.* to nextclouduser@localhost identified by'nextcloudpassword';
flush privileges;

5.为nextcloud 生成自签名ssl证书

cd /etc/nginx/cert/                      # 没有则创建此文件夹
openssl req -new -x509 -days 365 -nodes -out /etc/nginx/cert/nextcloud.crt -keyout /etc/nginx/cert/nextcloud.key

这里默认是365天,会出现一些选项需要填写,可以随便填。eg:

Country Name (2 letter code) [XX]:cn 
State or Province Name (full name) []:shaanxi 
Locality Name (eg, city) [Default City]: Xi 'an 
……

修改证书和文件夹权限

chmod 600 /etc/nginx/cert/*
chmod 700 /etc/nginx/cert

6.下载nextcloud,并配置php和nginx

下载nextcloud,官网地址为:https://nextcloud.com/install/#instructions-server

cd /usr/local/src
yum install -y wget unzip
wget https://download.nextcloud.com/server/releases/nextcloud-13.0.2.zip

下载(这里虽然是13.0.2,后续会自动更新版本)

unzip nextcloud-13.0.2.zip
mv nextcloud /usr/share/nginx/html/    # 移动到指定文件夹内
cd /usr/share/nginx/html/nextcloud     # 进行nextcloud 目录中
mkdir data                             # 创建数据文件夹
chown nginx:nginx -R nextcloud/        # 将nextcloud文件授权给nginx

配置php-fpm

vim /etc/php-fpm.d/www.conf

将用户和组都改为nginx

user = nginx  
group = nginx
listen = 127.0.0.1:9000

将以下几行,去掉注释

env[HOSTNAME] = $HOSTNAME                
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp

为php创建session文件夹

mkdir -p /var/lib/php/session
chown nginx:nginx -R /var/lib/php/session/

配置nginx

cd /etc/nginx/conf.d/
vim nextcloud.conf

nextcloud.conf配置文件如下:(直接复制即可,修改前先备份一份以防修改错误)

upstream php-handler {

    server 127.0.0.1:9000;

    #server unix:/var/run/php5-fpm.sock;

}

server {

    listen 80;

    server_name cloud.nextcloud.co;

    # enforce https

    return 301 https://$server_name$request_uri;

}
server {
    listen 443 ssl;
    server_name cloud.nextcloud.co;

    ssl_certificate /etc/nginx/cert/nextcloud.crt;
    ssl_certificate_key /etc/nginx/cert/nextcloud.key;

    # Add headers to serve security related headers
    # Before enabling Strict-Transport-Security headers please read into this
    # topic first.
    add_header Strict-Transport-Security "max-age=15768000;
    includeSubDomains; preload;";
    add_header X-Content-Type-Options nosniff;
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Robots-Tag none;
    add_header X-Download-Options noopen;
    add_header X-Permitted-Cross-Domain-Policies none;
# Path to the root of your installation
    root /var/www/html/nextcloud/;    # 自定义路径

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }
 # The following 2 rules are only needed for the user_webfinger app.
    # Uncomment it if you're planning to use this app.
    #rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
    #rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json
    # last;

    location = /.well-known/carddav {
      return 301 $scheme://$host/remote.php/dav;
    }
    location = /.well-known/caldav {
      return 301 $scheme://$host/remote.php/dav;
    }
 # set max upload size
    client_max_body_size 512M;
    fastcgi_buffers 64 4K;

    # Disable gzip to avoid the removal of the ETag header
    gzip off;

    # Uncomment if your server is build with the ngx_pagespeed module
    # This module is currently not supported.
    #pagespeed off;

    error_page 403 /core/templates/403.php;
    error_page 404 /core/templates/404.php;

    location / {
        rewrite ^ /index.php$uri;
    }
 location ~ ^/(?:build|tests|config|lib|3rdparty|templates|data)/ {
        deny all;
    }
    location ~ ^/(?:\.|autotest|occ|issue|indie|db_|console) {
        deny all;
    }

    location ~ ^/(?:index|remote|public|cron|core/ajax/update|status|ocs/v[12]|updater/.+|ocs-provider/.+|core/templates/40[34])\.php(?:$|/) {
        include fastcgi_params;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
 fastcgi_param HTTPS on;
        #Avoid sending the security headers twice
        fastcgi_param modHeadersAvailable true;
        fastcgi_param front_controller_active true;
        fastcgi_pass php-handler;
        fastcgi_intercept_errors on;
        fastcgi_request_buffering off;
    }
 location ~ ^/(?:updater|ocs-provider)(?:$|/) {
        try_files $uri/ =404;
        index index.php;
    }
# Adding the cache control header for js and css files
    # Make sure it is BELOW the PHP block
    location ~* \.(?:css|js)$ {
        try_files $uri /index.php$uri$is_args$args;
        add_header Cache-Control "public, max-age=7200";
        # Add headers to serve security related headers (It is intended to
        # have those duplicated to the ones above)
        # Before enabling Strict-Transport-Security headers please read into
 # this topic first.
        add_header Strict-Transport-Security "max-age=15768000;
        includeSubDomains; preload;";
        add_header X-Content-Type-Options nosniff;
        add_header X-Frame-Options "SAMEORIGIN";
        add_header X-XSS-Protection "1; mode=block";
        add_header X-Robots-Tag none;
        add_header X-Download-Options noopen;
        add_header X-Permitted-Cross-Domain-Policies none;
        # Optional: Don't log access to assets
        access_log off;
    }
 location ~* \.(?:svg|gif|png|html|ttf|woff|ico|jpg|jpeg)$ {
        try_files $uri /index.php$uri$is_args$args;
        # Optional: Don't log access to other assets
        access_log off;
    }
}

配置nginx.conf文件

nginx.conf文件先进行备份,修改nginx.conf文件,将其中80端口部分的配置全部删除。将80端口的server整个删除,我们在上面nextcloud.conf中已经配置了,这里不删除的话会导致冲突不生效。

server {          
  listen 80;
  }                             (删除此部分)

具体操作命令如下:

cp nginx.conf nginx.conf.bak            #备份nginx.conf文件
vim /etc/nginx/nginx.conf               #修改配置

7.启动nginx和php-fpm

nginx -t                 # 检查nginx配置是否正确,出现下面输入则正确。
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

启动并设为开机启动 nginx,php-fpm

systemctl start nginx
systemctl enable nginx
systemctl start php-fpm
systemctl enable php-fpm

 配置防火墙,开放http和https的端口。

firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --add-port=443/tcp --permanent
firewall-cmd –reload
setenforce 0                                  # 关闭selinux
vim /etc/selinux/config                       # 修改配置,永久关闭
SELINUX=disabled                              #添加这句话写在中间

8.访问搭建nextcloud进行安装

访问搭建nextcloud服务器,使用本机浏览器输入:https://(搭建环境的ip)/nextcloud(我的访问地址为:https://192.168.*.*/nextcloud)即可进入首页,根据提示完成对应信息填写,
填写完成后点击安装完成。如下图所示:

next 部署nginx_php