Hướng dẫn cài đặt LAMP (APACHE-PHP-MYSQL) và tạo VirtualHost trên Ubuntu 14


Bước 1: Cài đặt Apache

Tiến hành cập nhật gói phần mềm của Ubuntu lên phiên bản mới nhất với lệnh:

# apt-get update -y

Tên phần mềm Apache trên Ubuntu sẽ là apache2 nên chúng ta sẽ cài đặt với lệnh sau:

# apt-get install apache2 -y

/var/www/html : Thư mục chứa dữ liệu website gốc ,mặc định của Apache trên Ubuntu

/etc/apache2 : Thư mục chứa các file cấu hình của Apache trên Ubuntu

/etc/apache2/apache2.conf  : File cấu hình Apache trên Ubuntu.

Ta truy cập vào http://ip-vps nếu có hình như sau là thành công

Bước 2.Cài đặt PHP

# apt-get install php5 libapache2-mod-php5 php5-mcrypt -y

Chỉnh sửa file dir.conf thêm ’index.php’ vào đầu để ưu tiên index file index.php

# nano /etc/apache2/mods-enabled/dir.conf

<IfModule mod_dir.c>
        DirectoryIndex index.php  index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>

Các gốc tài liệu của trang web mặc định là / var / www / html. Bây giờ chúng ta sẽ tạo ra một file PHP nhỏ <info.php> trong thư mục đó và gọi nó trong một trình duyệt. Các tập tin sẽ hiển thị nhiều chi tiết hữu ích về cài đặt PHP của chúng tôi, chẳng hạn như các phiên bản PHP được cài đặt

# nano /var/www/html/info.php

Trong file info.php ta có nội dung sau 

<?php
phpinfo();
?>

Khởi động lại Apache.

# service apache2 restart

Nếu bạn thấy có lỗi này thì sửa như sau

root@nguyenvietduc:~# service apache2 restart
 * Restarting web server apache2                                                                                                                                        AH00558: apache2: Could not reliably determine the server’s fully qualified doma                                                                                        in name, using 107.189.160.9. Set the ’ServerName’ directive globally to suppres                                                                                        s this message
                                                                         [ OK ]

 

Ta chỉnh sửa file cấu hình ’apache2.conf’

# nano /etc/apache2/apache2.conf

Thêm 1 dòng sau

ServerName localhost

Khởi động lại Apache.

# service apache2 restart

root@nguyenvietduc:~# service apache2 restart
 * Restarting web server apache2                                         [ OK ]

Nếu bạn muốn cài thêm các module khác cho PHP thì có thể tìm tên các module trong gói cài đặt bằng lệnh sau:

# apt-cache search php5-

Kiểm tra php truy cập http://ip-vps/info.php

Bước 3:Cài đặt MySQL Server

Gõ lệnh dưới đây để cài đặt MySQL Server và module MySQL cho PHP.

# apt-get install mysql-server libapache2-mod-auth-mysql php5-mysql -y

Trong khi cài đặt, MySQL sẽ hiển thị một giao diện để bạn thiết lập mật khẩu root cho MySQL, bạn có thể ấn enter cho qua cũng được

 

Một khi bạn đã cài đặt MySQL, chúng ta nên kích hoạt nó bằng lệnh này:

# mysql_install_db

Chạy lệnh sau để thiết lập cho Mysql

# mysql_secure_installation

Nó sẽ hiện ra danh sách sau

root@nguyenvietduc:~# /usr/bin/mysql_secure_installation


NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!


In order to log into MySQL to secure it, we’ll need the current
password for the root user.  If you’ve just installed MySQL, 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):      (Nếu bạn đã nhập mật khẩu ở bảng trên thì nhập lại còn không thì ấn enter)
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

Set root password? [Y/n] y             (Đặt mật khẩu MySQL)
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!


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                       (xóa người dùng vô danh)
 ... 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                   (Không cho phép đăng nhập root từ xa)
 ... 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         (xóa cơ sở dữ liệu kiểm tra và truy cập vào nó)
 - Dropping test database...
ERROR 1008 (HY000) at line 1: Can’t drop database ’test’; database doesn’t exist
 ... Failed!  Not critical, keep moving...
 - 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                   (khởi động lại)
 ... 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!
 

Đăng nhập vào MySQL

# mysql -u root -p

Bước 4: Tạo VirtualHost 

Tạo 1 thư mục để chứa các code web 

# mkdir -p /var/www/maxserver.net/public_htm

Tạo 1 thư mục chứa các log của web đó

# mkdir /var/www/maxserver.net/log/

Phân quyền cho thư mục 

# chmod -R 755 /var/www/

Cấu trúc thư mục của apache2 

/etc/apache2/sites-available/ :Thư mục chưa các file cấu hình VirtualHost 

/etc/apache2/sites-enabled/ : Nếu muốn cho domain đc cấu hình trong thư mục /etc/apache2/sites-available/ thì phải enabled file cấu hình đó lên bằng cách tạo liên kết đến thư mục này 

/etc/apache2/sites-available/000-default.conf : file cấu hình VirtualHost mặc định của hệ thống ,dựa vào file này bạn cấu hình nhiều domain khác 

/etc/apache2/sites-available/default-ssl.conf : Đây cũng là file cấu hình mặc định của hệ thống ,dựa vào file này để cấu hình ssl

Ví dụ ta tạo file cấu hình cho domain maxserver.net

# nano /etc/apache2/sites-available/maxserver.net.conf

với nội cung như sau :

<VirtualHost *:80>
        ServerName maxserver.net
        ServerAlias www.maxserver.net
        ServerAdmin contact@maxserver.net
        DocumentRoot /var/www/maxserver.net/public_html
        ErrorLog /var/www/maxserver.net/log/error.log
        CustomLog /var/www/maxserver.net/log/access.log combined
        <Directory /var/www/maxserver.net/public_html>
                Options FollowSymLinks
                AllowOverride All
                Order allow,deny`
                Allow from all
                Require all granted
        </Directory>

</VirtualHost>

Bây giờ ta sẽ enabled file maxserver.net.conf bằng lệnh sau 

# a2ensite maxserver.net

Sẽ cho kết quả nhu sau ;

root@maxserver  ~# a2ensite maxserver.net
Enabling site maxserver.net.
To activate the new configuration, you need to run:
  service apache2 reload

Khởi động lại dịch vụ apache2 

# service apache2 restart

Ta tạo thử 1 file để xem kết quả 

# nano /var/www/maxserver.net/public_html/index.html

Có nội dung như sau

<html>
  <head>
    <title>maxserver</title>
  </head>
  <body>
    <h1>Chuc mot ngay tot lanh!</h1>
  </body>
</html>

Ta truy cập domain để xem kết quả 



Article ID: 96
Created: Sun, Apr 14, 2019
Last Updated: Sun, Apr 14, 2019
Author: admin

Online URL: https://huongdan.maxserver.com/article-96.html