Showing posts with label CentOS. Show all posts
Showing posts with label CentOS. Show all posts

Saturday, August 18, 2018

Hướng dẫn uninstall tận gốc DirectAdmin trên CentOS không cần reset VPS

Hôm nay mưa bão rảnh rỗi lướt qua diễn đàn V4U thấy có topic thắc mắc hỏi về cách gỡ DirectAdmin trên CentOS mà không cần phải reset VPS nên tiện viết bài hướng dẫn luôn ( đơn giản vì mình quá rảnh và cách làm cũng khá đơn giản, chỉ cần vài dòng lệnh là xong )


Related image

Bước 1: Kết nối tới VPS qua SSH và đăng nhập ( Bước này không cần nói chắc mọi người ai cũng biết )
Bước 2: Sau khi đăng nhập xong, bạn copy và paste các dòng lệnh dưới vào terminal:


Đầu tiên là gỡ DirectAdmin bằng cách thực hiện dòng lệnh:
  • rm -rf /usr/local/directadmin 
Tiếp đến là gỡ MySQL:

   + Kiểm tra các package MySQL bằng lệnh:
  • rpm -qa "MySQL*"
Các package MySQL sẽ được liệt kê như sau:
  • MySQL-devel-5.1.61-1.glibc23
    MySQL-shared-5.1.61-1.glibc23
    MySQL-client-5.1.61-1.glibc23
    MySQL-server-5.1.61-1.glibc23
Tiếp tục các bạn chạy lệnh sau để gỡ các package trên:
  • rpm -e MySQL-devel-5.1.61-1.glibc23 MySQL-shared-5.1.61-1.glibc23 MySQL-client-5.1.61-1.glibc23 MySQL-server-5.1.61-1.glibc23
Tiếp đến là xoá thư mục lưu trữ của MySQL và script còn sót lại:
  • rm -rf /var/lib/mysql
    rm -rf /etc/init.d/mysqld 
    rm -rf /etc/httpd
Cuối cùng là xoá thư mục PHP của phiên bản PHP bạn đang dùng, ví dụ như PHP 5:
  • rm -rf /usr/local/php5
Vậy là bạn đã gỡ hoàn toàn DirectAdmin mà không cần phải reset. Bạn có thể cài các Control Panel khác

Thursday, August 16, 2018

Hướng dẫn cài đặt LEMP (Linux, Nginx, MariaDB, PHP) trên CentOS

Trong bài viết này mình sẽ hướng dẫn các bạn cách cài đặt bộ webserver nổi tiếng LEMP bao gồm: Linux + Nginx + MariaDB + PHP (PHP-FPM) trên CentOS


– Nginx (đọc là Engine x) là một webserver nhỏ gọn, ít tốn RAM, mạnh mẽ và có hiệu suất hoạt động cao.
– PHP hỗ trợ các phiên bản 5.3, 5.4, 5.5, 5.6, 7.0

– MariaDB là phiên bản mã nguồn mở của MySQL, hoạt động giống hệt MySQL. Bắt đầu từ CentOS 7 mặc định đã bắt đầu hỗ trợ MariaDB, các bạn nên sử dụng thay cho MySQL ngay từ bây giờ.
Image result for linux nginx mysql php

I. Cài đặt Nginx và PHP trên CentOS

1. Thêm repo cần thiết

CentOS 7/6.5/5.10 EPEL repository
yum install epel-release
CentOS 7/6.5/5.10 Remi repository
## CentOS 7 ##
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

## CentOS 6 ##
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

## CentOS 5 ##
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm
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. Cài đặt 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

## PHP 7.0 ##
yum --enablerepo=remi,remi-php70 install -y nginx php-fpm php-common

## PHP 7.1 ##
yum --enablerepo=remi,remi-php71 install -y nginx php-fpm php-common

3. Cài đặt PHP module

Một số module PHP thông dụng:
  • 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
Để cài đặt bạn hãy sử dụng lệnh yum --enablerepo=remi,remi-php56 install ten_module. Ví dụ:
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 và 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. Tự động khởi động Nginx, PHP-FPM và tắt httpd

Tắt httpd (Apache) khi 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. Cấu hình Nginx và PHP-FPM

Cấu hình Nginx
– Thay đổi worker_processes
nano /etc/nginx/nginx.conf
Chỉnh worker_processes bằng với số processor VPS của bạn
– Cấu hình nginx virtual hosts
nano /etc/nginx/conf.d/default.conf
Bạn thay đổi thông tin như bên dưới:
#
# 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;
    }
}
Các đoạn bôi đỏ là cần phải thay đổi.
– Restart Nginx
## CentOS 7 ##
systemctl restart nginx.service
 
## CentOS 6.5/5.10 ##
service nginx restart
Cấu hình PHP-FPM
– Chỉnh user và group
nano /etc/php-fpm.d/www.conf
Thay user và group = apache sang 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 to be able to access some dir as httpd
 user = nginx
 ; RPM: Keep a group allowed to write in 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 cấu hình Nginx và PHP-FPM

nano /usr/share/nginx/html/info.php
Thêm đoạn sau vào
<?php
phpinfo();
?>
Test thử bằng link: http://<ip-address>/info.php. Nếu bạn thấy thông tin về PHP hiện ra thì đã cài đặt thành công.
Lưu ý: nếu bạn truy cập thẳng vào IP mà báo lỗi không kết nối được thì hãy open port http:
service iptables start
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
service iptables save
service iptables restart

II. Cài đặt MariaDB trên CentOS 7/6.5/5.10

 1. Thêm MariaDB repo

Lưu ý: Không cần thực hiện trên CentOS 7. Phiên bản MariaDB trên CentOS 7 hiện nay là 5.5.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. Cài đặt hoặc update MariaDB

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

## CentOS 6.5/5.10 ##
yum install -y MariaDB MariaDB-server

3. Khởi động MariaDB và tự động chạy khi 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. Cấu hình MariaDB

  • Set (Change) root password
  • Remove anonymous users
  • Disallow root login remotely
  • Remove test database and access to it
  • Reload privilege tables
– Bắt đầu cài đặt
/usr/bin/mysql_secure_installation
Ngay bước đầu tiên bạn sẽ bị hỏi root password, do mới cài đặt nên tất nhiên chưa có password, nhấn Enter để tiếp tục.
– Output tương tự như sau:
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!

Như vậy là bạn đã hoàn thành việc cài đặt LEMP stack trên CentOS rồi đó.

Wednesday, August 15, 2018

Hướng dẫn cài đặt LAMP (Linux, Apache, MySQL, PHP) trên CentOS 6

Do đã xuất hiện từ lâu nên LAMP (viết tắt của Linux, Apache, MySQL, và PHP) được dùng rất phổ biến trên thế giới. Ngoài LAMP còn có LEMP (viết tắt của Linux, Nginx, MySQL và PHP hoặc Perl hay Python cũng được). Nó tương tự như LAMP server ngoại trừ việc web server nền tảng được giám sát bằng Nginx thay vì Apache. Hôm nay, mình sẽ hướng dẫn các bạn cài đặt LAMP trên VPS chạy CentOS 6.
Related image

Các bước cài đặt LAMP trên CentOS

Đầu tiên bạn cần chuẩn bị một server CentOS mới tinh chưa cài gì cả, ở đây mình dùng CentOS 6.4 64bit. Trước khi thao tác bạn cần phải kiểm tr, đổi lại hostname và chỉnh sửa file host vì một số phần mềm yêu cầu server phải được thiết lập chính xác thông tin FQDN (Fully Qualified Domain Name) cho hostname. Đây cũng là vấn đề thường xuyên gặp phải trong khi làm việc với CentOS.

1. Cài đặt Apache

yum install httpd
Đường dẫn file cấu hình Apache:
/etc/httpd/conf/httpd.conf
Khởi động Apache
service httpd start
Test thử bằng cách truy cập vào http://<youraddress> bạn sẽ thấy thông báo “Apache 2 Test Page”
Cấu hình Apache Virtual Hosts
– Tạo một file cấu hình ở thư mục /etc/httpd/conf.d, đặt tên ví dụ example.com.conf (thay bằng domain của bạn)
Nội dung file /etc/httpd/conf.d/example.com.conf
NameVirtualHost *:80

<VirtualHost *:80> 
     ServerAdmin webmaster@example.com
     ServerName example.com
     ServerAlias www.example.com
     DocumentRoot /var/www/example.com/public_html/
     ErrorLog /var/www/example.com/logs/error.log 
     CustomLog /var/www/example.com/logs/access.log combined
</VirtualHost>
– Tạo thư mục cho website
mkdir -p /var/www/example.com/public_html
mkdir /var/www/example.com/logs
– Nếu muốn thêm website, bạn chỉ cần tạo thêm một file .conf khác và reload lại Apache với lệnh: service httpd reload

2. Cài đặt MySQL

yum install mysql-server
service mysqld start
Tiến hành cài đặt MySQL bằng lệnh
/usr/bin/mysql_secure_installation
Do mới cài đặt nên nếu bị hỏi password bạn nhấn Enter
Enter current password for root (enter for none): 
OK, successfully used password, moving on...
Sau đó cần đặt root password bằng cách chọn y
Tiếp theo bạn sẽ phải trả lời một loạt các câu hỏi, tốt nhất cứ chọn y
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL 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, MySQL 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 MySQL
installation should now be secure.

Thanks for using MySQL!

3. Cài đặt PHP

yum install php php-mysql
PHP Modules
PHP có rất nhiều thư viện module khác nhau, bạn có thể xem bằng cách gõ lệnh sau
yum search php-
php-bcmath.x86_64 : A module for PHP applications for using the bcmath library
php-cli.x86_64 : Command-line interface for PHP
php-common.x86_64 : Common files for PHP
php-dba.x86_64 : A database abstraction layer module for PHP applications
php-devel.x86_64 : Files needed for building PHP extensions
php-embedded.x86_64 : PHP library for embedding in applications
php-enchant.x86_64 : Human Language and Character Encoding Support
php-gd.x86_64 : A module for PHP applications for using the gd graphics library
php-imap.x86_64 : A module for PHP applications that use IMAP
Để cài module nào dùng lệnh sau
yum install name of the module

4. Tự động chạy service khi reboot

chkconfig httpd on
chkconfig mysqld on
Restart Apache
service httpd restart
OK vậy là quá trình cài đặt LAMP trên CentOS vậy là xong.
Chúc các bạn thành công !