一直以来我都强烈建议各位使用LAMP环境,因为这似乎是最合适的选择。但是Apache在内存占用和并发能力方面并不是那么尽人意,相比之下Nginx更加优秀。本文将介绍如何在CentOS7系统上部署LNMP环境并安装Nextcloud。
本教程以Vultr上的一台VPS为例。
CentOS默认的yum源中并不包含Nginx和php-fpm,首先要为CentOS添加epel源:
yum -y install epel-release
完成安装后,就可以用yum安装Nginx了:
yum -y install nginx
现在需要再添加一个yum源来安装php-fpm,我这儿使用的是webtatic(这个yum源对国内网络来说恐怕有些慢,当然你也可以选择其它的yum源)。
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
接下来,安装php7-fpm和一些其它的必要的组件:
yum -y install php70w-fpm php70w-cli php70w-gd php70w-mcrypt php70w-mysql php70w-pear php70w-xml php70w-mbstring php70w-pdo php70w-json php70w-pecl-apcu php70w-pecl-apcu-devel
完成后,检查一下php-fpm是否已正常安装:
php -v
php与Nginx并不像与Apache那样易于相处,需要让Nginx分离静态和动态请求,并将动态请求交给php-fpm来处理,接下来是一些必要的配置。
使用vim编辑php-fpm的配置文件:
vim /etc/php-fpm.d/www.conf
:set number
即可显示行号。
在配置文件的第8~10行,找到这两项配置:
user = apache group = apache
将用户和组都改为
nginx
:
user = nginx group = nginx
在第22行可以看到php-fpm所监听的端口为9000:
listen = 127.0.0.1:9000
在第366~370行找到有关环境变量的设置:
;env[HOSTNAME] = $HOSTNAME ;env[PATH] = /usr/local/bin:/usr/bin:/bin ;env[TMP] = /tmp ;env[TMPDIR] = /tmp ;env[TEMP] = /tmp
删掉每行前面的
;
,取消注释。
完毕后保存文件并退出。
接下来,在
/var/lib/
目录下为session路径创建一个新的文件夹,并将用户名和组设为
nginx
。
mkdir -p /var/lib/php/session
chown nginx:nginx -R /var/lib/php/session/
启动Nginx和php-fpm服务,并添加开机启动:
systemctl start php-fpm
systemctl start nginx
systemctl enable php-fpm
systemctl enable nginx
php-fpm的配置到此完成。
使用MaraiDB作为Nextcloud数据库。yum安装MaraiDB服务:
yum -y install mariadb mariadb-server
启动MariaDB服务并添加开机启动:
systemctl start mariadb systemctl enable mariadb
接下来设置MariaDB的root密码:
mysql_secure_installation
按照提示设置密码,首先会询问当前密码,密码默认为空,直接回车即可:
Enter current password for root (enter for none): #直接回车
Set root password? [Y/n] Y
New password: #输入新密码
Re-enter new password: #再次输入新密码
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y
设置完MariaDB的密码后,使用命令行登录MariaDB,并为Nextcloud创建相应的用户和数据库。例如将要创建的数据库为
nextcloud_db
,用户名为
nextclouduser
,密码为
nextcloudpasswd
(这只是个示例,为确保安全请尽量将密码设置得复杂些)。
mysql -u root -p
然后键入密码
使用SQL语句创建用户和数据库:
create database nextcloud_db; create user nextclouduser@localhost identified by 'nextcloudpasswd'; grant all privileges on nextcloud_db.* to nextclouduser@localhost identified by 'nextcloudpasswd'; flush privileges;
数据库
nextcloud_db
和用户
nextclouduser
,创建完成,密码是
nextcloudpasswd
。
在本教程中,我将使用客户端的https连接运行nextcloud。您可以使用免费的SSL证书,例如让加密或创建自签名的SSL证书。我将使用OpenSSL命令创键自签名SSL证书文件:
为SSL证书创建一个新的文件夹:
mkdir -p /etc/nginx/cert/
生成一个新的SSL证书文件:
openssl req -new -x509 -days 365 -nodes -out /etc/nginx/cert/nextcloud.crt -keyout /etc/nginx/cert/nextcloud.key
然后将证书文件的权限设置为660:
chmod 700 /etc/nginx/cert chmod 600 /etc/nginx/cert/*
我将使用wget命令直接将Nextcloud下载到服务器上,在此之前需要安装两样必要的工具:
yum -y install wget unzip
前往
/tmp
文件夹,使用wget命令下载Nextcloud:
cd /tmp
wget https://download.nextcloud.com/server/releases/nextcloud-12.0.0.zip
将下载完成的zip文件解压,然后移动到Nginx的目录
/usr/share/nginx/html/
中:
unzip nextcloud-12.0.0.zip
mv nextcloud/ /usr/share/nginx/html/
然后进入Nginx的root目录,并为Nextcloud创建data目录:
cd /usr/share/nginx/html/
mkdir -p nextcloud/data/
将Nextcloud的用户和组修改为
nginx
:
chown nginx:nginx -R nextcloud/
Nextcloud并不能在Nginx完美地运行,我们需要为它量身定做一个虚拟主机配置。
进入Nginx的虚拟主机配置文件所在目录并创建一个新的虚拟主机配置:
cd /etc/nginx/conf.d/ vim nextcloud.conf
然后将下面的配置信息粘贴进去(记得修改两个
server_name
为你自己的域名):
upstream php-handler { server 127.0.0.1:9000; #server unix:/var/run/php5-fpm.sock; } server { listen 80; server_name cloud.dreampacific.cn; # enforce https return 301 https://$server_name$request_uri; } server { listen 443 ssl; server_name cloud.dreampacific.cn; 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 /usr/share/nginx/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服务。
nginx -t systemctl restart nginx
安装SElinux管理工具policycoreutils-python:
yum -y install policycoreutils-python
执行以下命令来使Nextcloud在SELinux下正常运行:
semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/nextcloud/data(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/nextcloud/config(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/nextcloud/apps(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/nextcloud/assets(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/nextcloud/.htaccess'
semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/nextcloud/.user.ini'
restorecon -Rv '/usr/share/nginx/html/nextcloud/'
接下来设置防火墙,为Nextcloud开放http和https两个端口。
启动Firewalld服务并设置开机启动:
systemctl start firewalld systemctl enable firewalld
用firewalld-cmd命令开放http和https端口:
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
访问你的服务器,可以给服务器绑定一个域名,我的是cloud.dreampacific.cn。
访问域名,会自动跳转到https连接,按照提示安装即可。
本文由 橙叶博客 作者:FrankGreg 发表,转载请注明来源!
但是用宝塔的话就不一样了
非常感谢博主详细的分享,感恩,
另外,我参照配置了,然后访问域名,都提示 Your connection is not secure
请问该如何更改呢?firefox,safari都提示连接不安全,要添加信任才能访问
感谢解答
我是在虚拟机中搭建的nextcloud,然后在VPS上面用FRP连接进来的
按照您的教程一路设置下来,最后进入网站提示:
这是与Fedora 上的nginx一起分发的默认index.html页面。它位于 / usr / share / nginx / html中。
您现在应该将您的内容放在您选择的位置,并在nginx 配置文件 /etc/nginx/nginx.conf中编辑根配置指令。
请问是哪里我做错了吗?
安装后出错,提示Show warning if a PHP version below 5.6.0 is used, this has to happen here,是因为php没有安装成功吗?还是与其他组件有冲突?请教大佬如何解决?
按您的步骤配置完。打开http://域名/nextcloud 或是 <a href="http://IP/nextcloud" rel="nofollow ugc">http://IP/nextcloud</a> 后提示是保存(下载)index.php文件。请问这个是什么原因?谢谢!!
说明Nginx并没有被正确配置,把nginx的配置文件清空重新粘贴进去,然后重启
我虚拟机搭建的不能访问
可能是虚拟机网卡的问题
6411ff2f12f31966735cc599dd1abfe9我的网站总出现这个怎么办?
你好:我根据你上面一配置完了,输入域名打不开nextcloud,我尝试关闭selinux也不行,直接输入127.0.0.1能显示nginx的测试页。。。实在是新手,不知道如何解决了,忘求助,谢谢
你好,请问执行 semanage fcontext -a -t httpd_sys_rw_content_t ‘/usr/share/nginx/html/nextcloud/data(/.*)?’
出现报错ValueError: SELinux policy is not managed or store cannot be accessed.是什么情况
额,先把SELinux关掉再试试
你好:
我这边的php的源是用的remi的,但是安装好了后,管理员和数据库都配置好了,但是登陆不进去是什么原因???急急急!!!
您好,按照本文我已经成功安装并运行nextcloud,但是发现一个问题“应用”页面不显示内容。从“管理”“日志”页面看到刷新相同的报错,如下:
Error appstoreFetcher GuzzleHttp\Exception\ConnectException: cURL error 28: Operation timed out after 10000 milliseconds with 440192 out of 831152 bytes received
请教这是什么原因导致的?
十分感谢!
“应用”里不显示内容是由于网速的问题,已解决。但是“日志”报错还是没解决。
日志里的报错也与网速有关,这个真没什么好办法
你好,博主,我按照你的思路做出来了,但是安装完nextcloud后,登陆不进去这是怎么回事
配置好了 但是访问不了 错误码500 selinux跟firewall我是直接关掉的
这个情况比较复杂,可能与你的环境有关,我再研究下
你好,我在原来的主机上把apache给卸载了,环境改成lnmp,安装访问都没有任何问题,但是不能更改设置,提示输入授权密码,输入密码提交后还是弹出框框,没有任何其它提示,我用虚拟机全新安装centos7又不会,这个问题求解,两台主机之前都有部署过ow
你好:我根据你上面一配置完了,可是怎么都是打不开nextcloud?
怎么打不开呢,错误码是什么500还是502?可以试试将
add_header Strict-Transport-Security “max-age=15768000;
includeSubDomains; preload;”;
改为
includeSubDomains; preload;”;
add_header Strict-Transport-Security “max-age=15768000;
(颠倒一下顺序)
请问LNMP环境环境下如何将nextcloud安装在home目录下呢?看教程里面目录是在/usr/share/nginx/html/nextcloud/下
在home目录下创建用于安装nextcloud的目录,比如nextcloud,把nextcloud程序解压到里面,这样完整的文件目录为/home/nextcloud。然后将文章中的nginx配置中的<code>
root /usr/share/nginx/html/nextcloud/;
</code>
改为<code>
root /home/nextcloud/;
</code>
是否下面的两步骤也要更改代码呢
为Nextcloud创建data目录
#创建/data
#如果安装路径是/home的话则执行 cd /home/
#如果安装路径是/home的话则执行 mkdir -p nextcloud/data/
将Nextcloud的用户和组修改为 nginx
#设置用户和组
chown nginx:nginx -R nextcloud/
给予data750权限
chown 750 nextcloud
chown 750 nextcloud/data/
#安装policycoreutils-python
yum -y install policycoreutils-python
执行以下命令来使Nextcloud在SELinux下正常运行分别执行下面的7个命令
#如果是在home目录则执行 semanage fcontext -a -t httpd_sys_rw_content_t ‘/home/nextcloud/data(/.*)?’
#如果是在home目录则执行 semanage fcontext -a -t httpd_sys_rw_content_t ‘/home/nextcloud/config(/.*)?’
#如果是在home目录则执行 semanage fcontext -a -t httpd_sys_rw_content_t ‘/home/nextcloud/apps(/.*)?’
#如果是在home目录则执行 semanage fcontext -a -t httpd_sys_rw_content_t ‘/home/nextcloud/assets(/.*)?’
#如果是在home目录则执行 semanage fcontext -a -t httpd_sys_rw_content_t ‘/home/nextcloud/.htaccess’
#如果是在home目录则执行 semanage fcontext -a -t httpd_sys_rw_content_t ‘/home/nextcloud/.user.ini’
#如果是在home目录则执行 restorecon -Rv ‘/home/nextcloud/’
或者是直接关闭selinux呢
setenforce 0
sed -i ‘s/SELINUX=enforcing/SELINUX=disabled/g’ /etc/selinux/config
也该更改,可以直接关闭。二选一
请问我把路径改为home后在操作设置Firewalld更改的这段代码后再去执行安装的时候提示HTTP 403禁止访问,如果直接关闭了selinux是可以正常安装的,不知上面我更改的代码哪部分出错,还望版主指点一下谢谢
代码部分
执行以下命令来使Nextcloud在SELinux下正常运行分别执行下面的7个命令
#如果是在home目录则执行 semanage fcontext -a -t httpd_sys_rw_content_t ‘/home/nextcloud/data(/.*)?’
#如果是在home目录则执行 semanage fcontext -a -t httpd_sys_rw_content_t ‘/home/nextcloud/config(/.*)?’
#如果是在home目录则执行 semanage fcontext -a -t httpd_sys_rw_content_t ‘/home/nextcloud/apps(/.*)?’
#如果是在home目录则执行 semanage fcontext -a -t httpd_sys_rw_content_t ‘/home/nextcloud/assets(/.*)?’
#如果是在home目录则执行 semanage fcontext -a -t httpd_sys_rw_content_t ‘/home/nextcloud/.htaccess’
#如果是在home目录则执行 semanage fcontext -a -t httpd_sys_rw_content_t ‘/home/nextcloud/.user.ini’
#如果是在home目录则执行 restorecon -Rv ‘/home/nextcloud/’
看了下好像真没什么错。我一时半会也搞不明白。试一下这个命令吧:<code> chcon -R -u system_u /xxx/
chcon -R -t usr_t /xxx/</code>
感谢提供的技术文档,请问按照配置完成安装了,怎样自定义修改用户数据存储的目录呢
还有一个自检提示的配置安全及设置警告应该怎样解决呢
为了您服务的安全和性能, 请将所有设置配置正确. 我们将会进行一些自动化检查以帮助您完成这项工作. 详情请查看 “小提示” 部分及相关文档.
HTTP 请求头 “X-Frame-Options” 没有配置为 “SAMEORIGIN”. 这是一个潜在的安全或隐私风险, 我们建议您调整这项设置.
内存缓存未配置. 如果可用, 请配置 memcache 以增强性能. 更多信息请查看我们的文档.
PHP 的组件 OPcache 没有正确配置. 为了提供更好的性能, 我们建议在php.ini文件中使用下列设置:
opcache.enable=1
opcache.enable_cli=1
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=10000
opcache.memory_consumption=128
opcache.save_comments=1
opcache.revalidate_freq=1
请再次检查 安装指南 ↗, 并检查 日志 中的任何错误或警告.
用户存储目录要在安装阶段修改:在填写用户名和密码的下方设置安装目录,安装目录是绝对目录,注意要给予750权限。HTTP头的问题,将配置文件中响应字段删掉就可以隐藏掉信息。内存缓存可以查看本站文章。opache的问题要修改php.ini,在php.ini中找到警告中的相应内容,取消每行前面的;即可
感谢回复
请问安装完后:php.ini中没有这些配置怎么为“opcache.enable=1”
数据上传大小的话怎样修改该呢,改了下面的代码后发现提示超时了
client_max_body_size 512M;
感谢已经可以正常运行了
nginx -t 按教程到这里就出错了
nginx: [emerg] unknown directive ” server” in /etc/nginx/conf.d/nextcloud.conf:2
nginx: configuration file /etc/nginx/nginx.conf test failed
都是空格惹的祸
_(:3 」∠)_ 哈
为什么我重启了systemctl restart php-fpm服务 nextcloud就进不去了 打开网站一片空白 也没有报错
你好,请问如何修改上传文件大小!
修改:<code> client_max_body_size 512M;</code>