Thứ Hai, 29 tháng 2, 2016

Installation Guide LEMP (Linux, Nginx, MariaDB, PHP) on CentOS

In this post, I will show you how to install the popular webserver LEMP ( Linux, Nginx, MariaDB, PHP) on 7/6.5/6.4/6.3/6.2/6.1/6/5.10 CentOS.

Nginx (Engine x) is a compact webserver, less expensive RAM, powerful and high performance.

- PHP supports versions 5.3, 5.4, 5.5, 5.6.

MariaDB is the open source version of MySQL, it active as MySQL. Default at 7CentOS has started support MariaDB, you should use instead of MySQL now.

I.                   Installing Nginx and PHP on 7 / 6.5 / 5.10 CentOS

The first, you need to prepare a CentOS server not installed at anything . Verify that the hostname and host file are correct before starting.

The operation performed by the command line, use the tool ZOC Terminal and nano.

1. Add the necessary repo

CentOS  7 / 6.5 / 5.10 Remi repository

## CentOS 7 ##
rpm -Uvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

## CentOS 6 ##
rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

## CentOS 5 ##
rpm -Uvh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm

If you get an error message File Not Found, maybe RPM version has changed. You can grab the latest version of Fedora EPEL wiki page: https://fedoraproject.org/wiki/EPEL#How_can_I_use_these_extra_packages.3F

CentOS 7/6.5/5.10 Nginx repository

## CentOS 7 ##
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

## CentOS 6 ##
rpm -Uvh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm

## CentOS 5 ##
rpm -Uvh http://nginx.org/packages/centos/5/noarch/RPMS/nginx-release-centos-5-0.el5.ngx.noarch.rpm

2. Install Nginx, PHP

CentOS 7/6.5/5.10

## PHP 5.3 ##
yum install -y nginx php-fpm php-common

## PHP 5.4 ##
yum --enablerepo=remi install -y nginx php-fpm php-common

## PHP 5.5 ##
yum --enablerepo=remi,remi-php55 install -y nginx php-fpm php-common

## PHP 5.6 ##
yum --enablerepo=remi,remi-php56 install -y nginx php-fpm php-common

3. Install PHP module

Some commonly used PHP modules:

OPcache (php-opcache) – The Zend OPcache provides faster PHP execution through opcode caching and optimization.
APCu (php-pecl-apc) – APCu userland caching
CLI (php-cli) – Command-line interface for PHP
PEAR (php-pear) – PHP Extension and Application Repository framework
PDO (php-pdo) – A database access abstraction module for PHP applications
MySQL (php-mysqlnd) – A module for PHP applications that use MySQL databases
PostgreSQL (php-pgsql) – A PostgreSQL database module for PHP
MongoDB (php-pecl-mongo) – PHP MongoDB database driver
SQLite (php-pecl-sqlite) – Extension for the SQLite Embeddable SQL Database Engine
Memcache (php-pecl-memcache) – Extension to work with the Memcached caching daemon
Memcached (php-pecl-memcached) – Extension to work with the Memcached caching daemon
GD (php-gd) – A module for PHP applications for using the gd graphics library
XML (php-xml) – A module for PHP applications which use XML
MBString (php-mbstring) – A module for PHP applications which need multi-byte string handling
MCrypt (php-mcrypt) – Standard PHP module provides mcrypt library support

To install you user command line: yum --enablerepo=remi,remi-php56 install ten_module. For Example:

yum --enablerepo=remi,remi-php56 install -y php-opcache php-pecl-apcu php-cli php-pear php-pdo php-mysqlnd php-pgsql php-pecl-mongo php-pecl-sqlite php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml

4. Stop httpd (Apache) server, Start Nginx and PHP-FPM

Stop httpd (Apache)

## CentOS 7 ##
systemctl stop httpd.service

## CentOS 6.5/5.10 ##
service httpd stop

Start Nginx

## CentOS 7 ##
systemctl start nginx.service

## CentOS 6.5/5.10 ##
service nginx start

Start PHP-FPM

## CentOS 7 ##
systemctl start php-fpm.service

## CentOS 6.5/5.10 ##
service php-fpm start

5. Automatically restart Nginx, PHP-FPM and off httpd

Off httpd (Apache) to boot

## CentOS 7 ##
systemctl disable httpd.service

## CentOS 6.5/5.10 ##
chkconfig httpd off

Autostart Nginx

## CentOS 7 ##
systemctl enable nginx.service

## CentOS 6.5/5.10 ##
chkconfig --add nginx
chkconfig --levels 235 nginx on

Autostart PHP-FPM

## CentOS 7 ##
systemctl enable php-fpm.service

## CentOS 6.5/5.10 ##
chkconfig --add php-fpm
chkconfig --levels 235 php-fpm on

6. Configure PHP-FPM and Nginx

Nginx configuration

- Change worker_processes

nano /etc/nginx/nginx.conf

Adjusted worker_processes by the number of processor your VPS

- Configure virtual hosts nginx

nano /etc/nginx/conf.d/default.conf

You change the information as below:

#
# The default server
#
server {
    listen       80;
    server_name example.com;

    location / {
        root   /usr/share/nginx/html;
        index index.php index.html index.htm;
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }

    error_page  404              /404.html;
    location = /404.html {
        root   /usr/share/nginx/html;
    }

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

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           /usr/share/nginx/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}
The section highlighted in red is the need to change.
- Restart Nginx

## CentOS 7 ##
systemctl restart nginx.service

## CentOS 6.5/5.10 ##
service nginx restart

PHP-FPM configuration

- Panel users and groups

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

Replace user and group = apache to nginx

  [...]
  ; Unix user / group of processes
  ; Note: The user is Mandatory. If the group is not set, the default user's group
  ; Will Be used.
  ; RPM: apache choosed thể access to some dir as httpd
  user = nginx
  ; RPM: Keep a group allowed to write in the log dir.
  group = nginx
  [...]

– Restart PHP-FPM

## CentOS 7 ##
systemctl restart php-fpm.service

## CentOS 6.5/5.10 ##
service php-fpm restart

7. Test configure Nginx and PHP-FPM

nano /usr/share/nginx/html/info.php

Add the following to the

<? Php
phpinfo ();
?>

Tested by link: http: // <ip-address> /info.php. If you see information about PHP appear, it is installed successfully.

Note: if you go straight to the IP but you still see message connected error, please open port http:

service iptables start
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
service iptables save
service iptables restart

II. Installing MariaDB on CentOS 7 / 6.5 / 5.10

1.        Add the repo MariaDB

Note: No need to perform on CentOS 7. Now, the MariaDB versions on 7 CentOS is 05/05/37

## CentOS 6/5 MariaDB 5.5 ##
wget -O /etc/yum.repos.d/MariaDB.repo http://mariadb.if-not-true-then-false.com/centos/$(rpm -E %centos)/$(uname -i)/5

## CentOS 6/5 MariaDB 10.0 ##
wget -O /etc/yum.repos.d/MariaDB.repo http://mariadb.if-not-true-then-false.com/centos/$(rpm -E %centos)/$(uname -i)/10

2.        Install or update MariaDB

CentOS 7 ## ##
yum install -y MariaDB MariaDB-server

CentOS 6.5 ## / ## 5:10
yum install -y MariaDB MariaDB-server

3.        Run the MariaDB and automatically runs when the boot

## CentOS 7 ##
systemctl start mariadb.service
systemctl enable mariadb.service

## CentOS 6.5/5.10 ##
service mysql start
chkconfig --levels 235 mysql on

4. Configure MariaDB

Set (Change) root password
Remove anonymous users
Disallow root login remotely
Remove test database and access to it
Reload privilege tables

- Start the installation

/usr/bin/mysql_secure_installation

The first step you will be asked for the root password. Since you recently installed, so you will not have the password, press Enter to continue.

- Output similar to the following:

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!


Now that you have completed the installation on CentOS LEMP stack. 

Không có nhận xét nào:

Đăng nhận xét