การติดตั้ง Nextcloud 22 บน CentOS7 ด้วย PHP 7, Apache และ MariaDB
1. Disable SELinux
sudo setenforce 0
sudo sed -i 's/^SELINUX=.*/SELINUX=permissive/g' /etc/selinux/config
2. Install PHP and httpd
การติดตั้ง Nextcloud จำเป็นต้องติดตั้ง PHP และ Apache ในที่นี้เราจะใช้ PHP 7.4
yum -y install epel-release yum-utils
yum -y install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
Disable default PHP 5.4 แล้ว Enable PHP 7.4
yum-config-manager --disable remi-php54
yum-config-manager --enable remi-php74
แล้ว Install Apache และ PHP Packages
yum -y install vim httpd php php-cli php-mysqlnd php-zip php-devel php-gd php-mcrypt php-mbstring php-curl php-xml php-pear php-bcmath php-json php-pdo php-pecl-apcu php-pecl-apcu-devel
ตรวจสอบ PHP Version
php -v
3. Install and Configure MariaDB
เพิ่ม MariaDB YUM Repository
cat <<EOF | sudo tee /etc/yum.repos.d/MariaDB.repo
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.5/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
EOF
*** แทนที่ 10.5 ด้วย MariaDB Version ที่ต้องการติดตั้ง
Clean yum cache index
yum makecache fast
Install MariaDB 10.5
yum -y install MariaDB-server MariaDB-client
ตรวจสอบรายละเอียดและ MariaDB Package ที่ติดตั้ง
rpm -qi MariaDB-server
Start และ enable MariaDB service
systemctl enable --now mariadb
Secure MariaDB โดยการรัน
$ mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y
... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
Login และตรวจสอบ
# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 16
Server version: 10.4.6-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
สร้าง User, Database สำหรับ Nextcloud
$ mysql -u root -p
#CREATE USER 'nextclouduser'@'localhost' IDENTIFIED BY "[Password]";
#CREATE DATABASE nextclouddb;
#GRANT ALL PRIVILEGES ON nextclouddb.* TO 'nextclouduser'@'localhost';
#FLUSH PRIVILEGES;
#QUIT
4. Download and Install Nextcloud on CentOS 7
yum -y install wget unzip
wget https://download.nextcloud.com/server/releases/nextcloud-22.1.1.zip
unzip nextcloud-22.1.1.zip
rm -f nextcloud-22.1.1.zip
mv nextcloud/ /var/www/html/
สร้าง Directory สำหรับเก็บไฟล์ (สามารถ Mount มาจาก NFS หรือ SAN หรืออื่นๆได้)
mkdir /var/www/html/nextcloud/data
chown apache:apache -R /var/www/html/nextcloud/data
chown apache:apache -R /var/www/html/nextcloud
5. Configure Apache VirtualHost
5.1 ไม่ติดตั้ง SSL
Create apache configuration:
nano -w /etc/httpd/conf.d/nextcloud.conf
วาง Config ตามด้านล่างในไฟล์ /etc/httpd/conf.d/nextcloud.conf
<VirtualHost *:80>
ServerName servername.example.com
ServerAdmin admin@example.com
DocumentRoot /var/www/html/nextcloud
<directory /var/www/html/nextcloud>
Require all granted
AllowOverride All
Options FollowSymLinks MultiViews
SetEnv HOME /var/www/html/nextcloud
SetEnv HTTP_HOME /var/www/html/nextcloud
</directory>
</VirtualHost>
*** แก้ไข ServerName และ ServerAdmin เป็นชื่อของเราเอง
Start httpd service:
systemctl enable --now httpd
5.2 ติดตั้ง Let's Encrypt SSL
เพื่อติดตั้ง Let's Encrypt SSL Certificate ต้องติดตั้ง certbot
wget https://dl.eff.org/certbot-auto
chmod a+x certbot-auto
sudo mv certbot-auto /usr/local/bin
Request SSL:
#export DOMAIN="servername.example.com"
#export EMAIL="admin@example.com"
#certbot-auto certonly --standalone -d $DOMAIN --preferred-challenges http --agree-tos -n -m $EMAIL --keep-until-expiring
*** แก้ไข DOMAIN และ EMAIL ของเราเอง
แก้ไข Virtualhost Configuration
sudo nano -w /etc/httpd/conf.d/nextcloud.conf
วาง Config ด้านล่างที่ /etc/httpd/conf.d/nextcloud.conf
<VirtualHost *:80>
ServerName servername.example.com
ServerAdmin admin@example.com
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]
</VirtualHost>
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName servername.example.com
ServerAdmin admin@example.com
DocumentRoot /var/www/html/nextcloud
<directory /var/www/html/nextcloud>
Require all granted
AllowOverride All
Options FollowSymLinks MultiViews
SetEnv HOME /var/www/html/nextcloud
SetEnv HTTP_HOME /var/www/html/nextcloud
</directory>
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/servername.example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/servername.example.com/privkey.pem
</VirtualHost>
</IfModule>
*** แก้ไข ServerName และ ServerAdmin เป็นชื่อของเราเอง
6. ตั้งค่า Nextcloud Configuration
เรียก URL ที่ติดตั้ง Nextcloud
https://servername.example.com
กำหนดค่าตามรูป
รอจนดำเนินการเสร็จสิ้น