INSTALL LAMP OR LEMP ON CENTOS 7 FOR MOODLE

Prerequisites

yum is located in /etc/yum.repos.d/
files have .repo extension to be recognized by YUM

$ sudo yum update
$ sudo systemctl status firewalld
$ sudo systemctl start firewalld
$ sudo systemctl enable firewalld
$ sudo systemctl stop firewalld

add service or port to to exclude from firewall

$ sudo firewall-cmd –permanent –add-service=http
$ sudo firewall-cmd –permanent –add-service=https
$ sudo firewall-cmd –permanent –add-service=mysql
$ sudo firewall-cmd –permanent –add-service=ntp
$ sudo firewall-cmd –permanent –add-port=8080/tcp
$ sudo firewall-cmd –permanent –add-port=4444/tcp
$ sudo firewall-cmd –permanent –add-port=4567/tcp
$ sudo firewall-cmd –permanent –add-port=4568/tcp
$ sudo firewall-cmd –permanent –add-port=139/tcp
$ sudo firewall-cmd –permanent –add-port=445/tcp
$ sudo firewall-cmd –permanent –add-port=9200/tcp
$ sudo firewall-cmd –reload
$ sudo systemctl restart firewalld.service
$ sudo iptables -L

4444, 45467, 4568 is for porcona, 139, 445 is for samba, 8080 is for https

i.e. test mysql access from remote machine (do this after mysql installation)

$ sudo mysql -h 10.0.0.71 -u msen -p

 

IP

to learn hostname
$ hostname
to get the local ip
$ ip addr show
to get public ip
$ ip addr show eth0
to get public ip in details
$ ip addr show eth0 | grep inet | awk ‘{ print $2; }’ | sed ‘s/\/.*$//‘

OTHER

print kernel release
$ uname -r
3.10.0-123.el7.x86_64
print kernel name
$ uname -s
Linux

 

SETUP MARIADB REPO

select your OS to get the latest MariaDB repo

https://downloads.mariadb.org/mariadb/repositories

you can rename a similar name to MariaDB

$ sudo vi /etc/yum.repos.d/MariaDB.repo

copy paste this:

# MariaDB 10.0 CentOS repository list - created 2015-07-09 14:56 UTC
# http://mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.0/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

update yum

$ sudo yum update

 

CONFIGURE NTP (In case you need for later purpose)

install ntp (network time protocol) server to synchronize time

$ sudo yum install ntp

first go to official http://www.pool.ntp.org/en/
choose your Continent area where the server physically is located, then search for your Country location

For United Sates:

server 0.us.pool.ntp.org
server 1.us.pool.ntp.org
server 2.us.pool.ntp.org
server 3.us.pool.ntp.org

go to ntp config file

$ sudo vi /etc/ntp.conf

Uncomment initials:

#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst

copy new United States to config and start

$ sudo systemctl start ntpdate
$ sudo systemctl enable ntpdate
$ sudo systemctl status ntpdate

run this

$ sudo ntpdate us.pool.ntp.org

check the date-time and match it with your cell phones!

$ timedatectl

Local time: Wed 2015-09-09 09:31:09 CDT
Universal time: Wed 2015-09-09 14:31:09 UTC
Timezone: America/Chicago (CDT, -0500)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: no
DST active: yes
Last DST change: DST began at
Sun 2015-03-08 01:59:59 CST
Sun 2015-03-08 03:00:00 CDT
Next DST change: DST ends (the clock jumps one hour backwards) at
Sun 2015-11-01 01:59:59 CDT
Sun 2015-11-01 01:00:00 CST

Since NTP enabled is no above, you can enable it like this (it will ask for your root password)

$ timedatectl set-ntp yes

then check out date-time again

$ timedatectl

Local time: Wed 2015-09-09 09:40:37 CDT
 Universal time: Wed 2015-09-09 14:40:37 UTC
 Timezone: America/Chicago (CDT, -0500)
 NTP enabled: yes
 NTP synchronized: yes
 RTC in local TZ: no
 DST active: yes
 Last DST change: DST began at
 Sun 2015-03-08 01:59:59 CST
 Sun 2015-03-08 03:00:00 CDT
 Next DST change: DST ends (the clock jumps one hour backwards) at
 Sun 2015-11-01 01:59:59 CDT
 Sun 2015-11-01 01:00:00 CST

if you still see a different timezone

$ sudo timedatectl set-timezone America/Chicago

if you want to restart ntp, then you need to first unrelate ntp from timedatectl

$ timedatectl set-ntp no
$ sudo systemctl restart ntpdate
$ sudo systemctl enable ntpdate
$ sudo ntpdate us.pool.ntp.org
$ timedatectl set-ntp yes

make sure your timezone is also correct
(please follow http://www.putorius.net/2015/04/setting-time-and-date-in-red-hat-7.html to play with timedatectl command)
list all the timezones just for curiosity

$ timedatectl list-timezones

you can also check the date simply like this

$ date
Wed Sep 9 09:47:35 CDT 2015

 

SERVICES (For later need)

shows all the services

$ sudo systemctl list-units –type=service

or you can search a specific service

$ sudo systemctl list-units –type=service | grep at\..*service

 

1- Install Apache

commands of apache

$ sudo yum install httpd
$ sudo systemctl start httpd.service
$ sudo systemctl enable httpd.service
$ sudo systemctl status httpd.service
$ sudo apachectl graceful
$ sudo systemctl restart httpd.service
$ httpd -v

setup config

$ sudo vi /etc/httpd/conf/httpd.conf
ServerName 10.10.0.71:80

to test apache configuration before restarting

$ sudo apachectl configtest

check out apache web

http://10.10.0.71/

NOTE: if nginx is going to be used instead of Apache follow these steps

NGINX

$ sudo systemctl stop httpd
$ sudo systemctl disable httpd
$ sudo yum install epel-release
$ sudo yum install nginx
$ sudo systemctl start nginx
$ sudo systemctl enable nginx

http://10.10.1.21 should show nginx default page

The default path root for nginx /usr/share/nginx/html

$ sudo vi /etc/php.ini
cgi.fix_pathinfo=0

make sure php-fpm is installed

$ sudo yum install php-fpm

configure php-fpm

$ sudo vi /etc/php-fpm.d/www.conf
listen = /var/run/php-fpm/php-fpm.sock
listen.owner = nobody
listen.group = nobody
user = nginx
group = nginx
security.limit_extensions = .php .php3 .php4 .php5
$ sudo systemctl start php-fpm
$ sudo systemctl enable php-fpm
$ sudo vi /etc/nginx/conf.d/default.conf

add this

server {
listen 80;
server_name 10.10.1.21;

# note that these lines are originally from the "location /" block
root /usr/share/nginx/html;
index index.php index.html index.htm;

location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

#location ~ \.php$ {
# try_files $uri =404;
# fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# include fastcgi_params;
#}

location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

location /dataroot/ {
internal;
alias /var/moodledata/; # ensure the path ends with /
}

location /cachedir/ {
internal;
alias /var/moodledata/cache/; # ensure the path ends with /
}

location /localcachedir/ {
internal;
alias /var/moodledata/localcache/; # ensure the path ends with /
}

location /tempdir/ {
internal;
alias /var/moodledata/temp/; # ensure the path ends with /
}

location /filedir/ {
internal;
alias /var/moodledata/filedir/; # ensure the path ends with /
}

backup first nginx files in temp

$ sudo mkdir /tmp/nginxdefaults
$ sudo cp /usr/share/nginx/html/404.html /tmp/nginxdefaults/
$ sudo cp /usr/share/nginx/html/50x.html /tmp/nginxdefaults/
$ sudo cp /usr/share/nginx/html/index.html /tmp/nginxdefaults/
$ sudo cp /usr/share/nginx/html/nginx-logo.png /tmp/nginxdefaults/
$ sudo cp /usr/share/nginx/html/poweredby.png /tmp/nginxdefaults/

if moodle is still in apache server copy it under nginx

$ sudo cp -R /var/www/html/moodle/* /usr/share/nginx/html/

if updated moodle copied then make sure you have nginx default files

$ sudo cp -R /tmp/nginxdefaults/* /usr/share/nginx/html/

enable nginx settings in moodle config file

$ sudo vi /usr/share/nginx/html/config.php

//enabled by Mehmet Sen
 $CFG->xsendfile = 'X-Accel-Redirect'; // Nginx {@see http://wiki.nginx.org/XSendfile}
 // If your X-Sendfile implementation (usually Nginx) uses directory aliases specify them
 // in the following array setting:
 $CFG->xsendfilealiases = array(
 '/dataroot/' => $CFG->dataroot,
 '/cachedir/' => '/var/moodledata/cache', // for custom $CFG->cachedir locations
 '/localcachedir/' => '/var/moodledata/localcache', // for custom $CFG->localcachedir locations
 '/tempdir/' => '/var/moodledata/temp', // for custom $CFG->tempdir locations
 '/filedir' => '/var/moodledata/filedir', // for custom $CFG->filedir locations
 );

restart nginx

$ sudo systemctl restart nginx

TUNE UP NGINX

https://www.digitalocean.com/community/tutorials/how-to-optimize-nginx-configuration
First tune up Kernel
get the total number of CPU cores of the machine

$ grep processor /proc/cpuinfo | wc -l
2

let’s become root to change the kernel

$ sudo -s

see how much open files the kernel can retreive

$ ulimit -a

assume 1024 for 0.5 Gig (512 MB) RAM so for 8 Gig change it to 16384

$ ulimit -n 16384

check the limit

$ ulimit -Hn

worker_connections tells our worker processes how many people can simultaneously be served by Nginx
The default value is 768; however, considering that every browser usually opens up at least 2 connections/server, this number can half.

$ sudo vi /etc/nginx/nginx.conf
events {
worker_connections 8192;
}

#we have 2 cpu cores
worker_processes 2;

Note:

max_clients = worker_processes * worker_connections ==> 2 * 8192 = 16384
for moodle https://docs.moodle.org/29/en/Performance_recommendations
MaxClients = Total available memory * 80% / Max memory usage of apache process
Memory usage of apache process is usually 10MB but Moodle can easily use up to 100MB per process
max_clients = 12 x 1024 * 80% /100 = 98.304
So for moodle the concurrent connections is decreased from 8192 to 98

$ ps -ylC nginx –sort:rss

adjust buffers in nginx.conf

$ sudo vi /etc/nginx/nginx.conf

#added by Mehmet Sen
 client_body_buffer_size 10K;
 client_header_buffer_size 1k;
 client_max_body_size 512m;
 large_client_header_buffers 2 1k;
#added by Mehmet Sen
 client_body_timeout 1200;
 client_header_timeout 1200;
 send_timeout 100;

$ sudo vi /etc/php-fpm.d/www.conf

pm = ondemand
pm.process_idle_timeout =10s
pm.max_children = 300
; Default Value: min_spare_servers + (max_spare_servers - min_spare_servers) / 2
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 1000

restart nginx server

$ sudo systemctl restart nginx
$ sudo systemctl restart php-fpm

 

2- Install MariaDB (MySQL)

Note: for MariaDB Cluster follow —> MARIADB CLUSTER ON CENTOS 7

remove if existing 5.5 MariaDB

$ sudo yum remove mariadb-server mariadb-libs

clean up the repository cache

$ yum clean all

to install mariadb

$ sudo yum install MariaDB-server MariaDB-client

to start mariadb

$ sudo systemctl start mysql
($ sudo /etc/init.d/mysql start)

to enable at startup

$ sudo systemctl enable mysql

run security script

$ sudo mysql_secure_installation

to check status of mariadb

$ sudo systemctl status mysql

upgrade

$ sudo mysql_upgrade -u root -p

login database

$ sudo mysql -u root -p
mysql>show databases;
mysql>exit;

main config file

$ sudo vi /etc/my.cnf

if it’s not there ask mysqld where it looks for its config

$ /usr/libexec/mysqld –help –verbose

 

3- Install PHP

to install

$ sudo yum install php php-mysqlnd

search optional php packages

$ sudo yum search php-

to install additional php packages

$ sudo yum install php-gd php-pear php-mbstring php-pgsql

or run almost all packages

$ sudo yum install php-gd php-pear php-mbstring php-pgsql php-fmp php-ldap php-devel php-pspell php-intl php-xml php-xmlrpc php-soap php-bcmath php-snmp php-embedded php-pecl-memcache php-odbc php-curl php-fpm php-memcached

get info of a certain package

$ sudo yum info php-mysql

test php

$ sudo vi /var/www/html/info.php

write this

<?php phpinfo() ?>

restart apache
$ sudo systemctl restart httpd.service
remove this later in prod
$ sudo rm /var/www/html/info.php

to change max file upload size
$ sudo vi /etc/php.ini
upload_max_filesize = 100M

test database from php
$ sudo vi /var/www/html/dbtest.php
copy paste this:
<?php //enable this in dev mode
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 'On');

$conn = mysqli_connect(“hostname”,”username”,”password”,”dbname”);

/* check connection */
if (mysqli_connect_errno()) {
printf(“Connect failed: %s\n”, mysqli_connect_error());
exit();
}

/* check if server is alive */
if (mysqli_ping($conn)) {
printf (“Connection is ok!\n”);
} else {
printf (“Error: %s\n”, mysqli_error($conn));
} /* close connection */ mysqli_close($conn);
?>

now check out dbtest

http://10.10.0.71/dbtest.php

 

4- Install PhpMyAdmin

To install phpMyAdmin easily on CentOS 7, first install extra packages for enterprise linux (epel)

$ sudo yum install epel-release

to install phpMyAdmin

$ sudo yum install phpMyAdmin

FOR APACHE

conf file -> /etc/httpd/conf.d/phpMyAdmin.conf

$ sudo vi /etc/httpd/conf.d/phpMyAdmin.conf

to access only from certain ip
put Require ip 10.10.0.71 under each Require ip 127.0.0.1

or disable first block and add this

<Directory /usr/share/phpMyAdmin/>
 Options none
 AllowOverride Limit
 Require all granted
</Directory>

restart apache

$ sudo systemctl restart httpd.service

FOR NGINX

$ ln -s /usr/share/phpmyadmin /usr/share/nginx/html
$ sudo systemctl restart php-fpm
$ sudo systemctl restart nginx

Advertisements

One thought on “INSTALL LAMP OR LEMP ON CENTOS 7 FOR MOODLE

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s