LAMP V2 thực tế vẫn là Linux + Apache + MariaDB + PHP. Tuy nhiên trong bản hướng dẫn cài LAMP V2 này mình sẽ thay thế PHP bằng PHP-FPM kết hợp mod_fastcgi.
Tại sao lại có sự thay đổi này ?
Trước tiên các bạn có thể đọc 1 số vấn đề dưới đây
– Bạn đang có một web server CentOS 6 chạy Apache với mod_php – Web server của bạn đang có lượng truy cập lớn nên chạy rất chậm – Web server của bạn đang chạy những chương trình PHP khá nặng nề như WordPress, Drupal hay Joomla – Bạn đang sử dụng memcached, opcode, APC hay CDN nhưng vẫn chậm – Bạn nghe nói đến việc sử dụng nginx và PHP-FPM sẽ nhanh hơn nhiều – NHƯNG bạn chưa thể chuyển sang nginx được vì bạn gặp rắc rối với việc chuyển đổi .htaccess và các file virtual host.
Biện pháp
– Bạn có thể mua server với CPU mạnh hơn, nhiều RAM hơn, ổ cứng nhanh hơn, hoặc chuyển sang dùng nginx, và mỗi khi gặp vấn đề với cấu hình lại chạy đôn chạy đáo khắp các forum để tìm cách giải quyết. Cả 2 giải pháp này đều tốn nhiều thời gian và tiền bạc. – Vẫn còn một giải pháp thứ 3, đó là cài Apache và chạy PHP-FPM thay cho mod_php
Và trong tut này mình sẽ hướng dẫn các bạn thực hiện biện pháp thứ 3.
Hướng dẫn cài đặt LAMP V2 trên Centos 6
Bước 1. Cài đặt Apache, bật UserDir, tạo virtualhost
Ở bước này không có gì thay đổi. Các bạn cài theo các tut sau:
Bước 2. Cài đặt php-fpm
- Add repo
Các bạn chạy lần lượt các lệnh sau
rpm –Uvh //mirrors.digipower.vn/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
rpm –Uvh //rpms.famillecollet.com/enterprise/remi-release-6.rpm
- Cài đặt php-fpm
* Php-fpm 5.4
yum –y —enablerepo\=remi install php php–common php–fpm php–mysql php–gd php–xml php–mbstring php–mcrypt php–pdo php–soap php–curl php–snmp php–pspell php–gmp php–intl php–imap perl–LWP–Protocol–https php–pear–Net–SMTP php–enchant php–pear php–devel php–zlib php–xml php–xmlrpc php–tidy
* Php-fpm 5.5
yum –y —enablerepo\=remi,remi–php55 install php php–common php–fpm php–mysql php–gd php–xml php–mbstring php–mcrypt php–pdo php–soap php–curl php–snmp php–pspell php–gmp php–intl php–imap perl–LWP–Protocol–https php–pear–Net–SMTP php–enchant php–pear php–devel php–zlib php–xml php–xmlrpc php–tidy
* Php-fpm 5.6
yum –y —enablerepo\=remi,remi–php56 install php php–common php–fpm php–mysql php–gd php–xml php–mbstring php–mcrypt php–pdo php–soap php–curl php–snmp php–pspell php–gmp php–intl php–imap perl–LWP–Protocol–https php–pear–Net–SMTP php–enchant php–pear php–devel php–zlib php–xml php–xmlrpc php–tidy
- Cấu hình
* Mở file /etc/php-fpm.d/www.conf
– Tìm listen = 127.0.0.1:9000 sửa thành listen = /var/run/php-fpm/www.sock
– Tìm
;listen.owner
;listen.group
;listen.mode \= 0660
Sửa thành
listen.owner \= apache
listen.group \= apache
listen.mode \= 0660
* Mở file /etc/php.ini
– Tìm ;date.timezone = sửa thành date.timezone = “Asia/Saigon”
– Tìm expose_php = On sửa thành expose_php = Off
– Tìm allow_url_fopen = On sửa thành allow_url_fopen = Off
– Nếu muốn disable_function hạn chế run shell thì các bạn tìm dòng disable_functions = sửa thành
disable_functions \= apache_child_terminate,apache_get_modules,apache_get_version,apache_getenv,apache_note,apache_setenv,virtual,mb_send_mail,link,chgrp,leak,syslog,openlog,mysql_list_dbs,ini_restore,symlink,escapeshellarg,escapeshellcmd,ini_alter,dl,exec,passthru,parse_ini_file,pcntl_exec,pfsockopen,popen,posix_kill,posix_mkfifo,posix_setuid,proc_close,proc_open,proc_terminate,shell_exec,system,leak,posix_setpgid,posix_setsid,proc_get_status,proc_nice,show_source
Ngoài ra các bạn có thể dis thêm một số functions sau:
pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority
Tuy nhiên cần chú ý khi sử dụng tránh lỗi site
* Chạy tiếp lệnh sau
mv /etc/httpd/conf.d/php.conf /etc/httpd/conf.d/php.conf.bak
Bước 3. Cài đặt MariaDB
– Đối với php 5.4
yum –y —enablerepo\=remi install phpmyadmin
– Đối với php 5.5
yum –y —enablerepo\=remi,remi–php55 phpmyadmin
– Đối với php 5.6
yum –y —enablerepo\=remi,remi–php56 install phpmyadmin
– Mở file /etc/httpd/conf.d/phpMyAdmin.conf sửa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
AddDefaultCharset UTF–8
# Apache 2.4
Require ip 127.0.0.1
Require ip ::1
# Apache 2.2
Order Deny,Allow
Deny from All
Allow from 127.0.0.1
Allow from ::1
Thành
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
AddDefaultCharset UTF–8
# Apache 2.4
Require ip All
Require ip ::1
# Apache 2.2
Order Deny,Allow
Allow from All
Allow from ::1
Bước 5. Cài đặt mod_fastcgi
rpm –ivh //rpm.centoshelp.org/el6/mod_fastcgi/mod_fastcgi-2.4.6-2.el6.cosh.x86_64.rpm
* Cấu hình
* Mở file /etc/php-fpm.d/fastcgi.conf
– Tìm FastCgiIpcDir /var/run/mod_fastcgi sửa thành
FastCgiIpcDir /var/run/mod_fastcgi
– Tìm FastCgiWrapper On sửa thành
FastCgiWrapper On
– Tìm FastCgiConfig -idle-timeout 20 -maxClassProcesses 1 sửa thành
FastCgiConfig -idle-timeout 20 -maxClassProcesses 1
– Thêm xuống cuối file
DirectoryIndex index.php
AddHandler php–fcgi .php
Action php–fcgi /php–fcgi
Alias /php–fcgi /cgi–bin–php.fcgi
FastCgiExternalServer /cgi–bin–php.fcgi –socket /var/run/php–fpm/www.sock –pass–header Authorization
Bước 6: Khởi động lại webserver
Các bạn chạy lần lượt các lệnh sau
chkconfig —level 235 httpd on
chkconfig —level 235 mysql on
chkconfig —level 235 php–fpm on
service httpd start
service mysql restart
service php–fpm start
Ok. Done. Trong các tut sau mình sẽ hướng dẫn các bạn một số phương pháp tối ưu vps để đạt hiệu quả tốt nhất.
Như các bạn đã biết mặc định Apache sẽ để đường dẫn thư mục chứa code là /var/ww/html. Trong tut này mình sẽ hướng dẫn các bạn bật UserDir trên Apache và chuyển đường dẫn về /home/domain/public_html.
Bắt đầu thôi.
1. Bước 1: Mở file /ect/httpd/conf/httpd.conf
– Tìm [Dòng 366]
Sửa thành
– Tìm [Dòng 373]
Sửa thành
– Tìm [Từ dòng 381 – 392]
#
# AllowOverride FileInfo AuthConfig Limit
# Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
#
# Order allow,deny
# Allow from all
#
#
# Order deny,allow
# Deny from all
#
Sửa thành
1
2
3
4
5
6
7
8
9
10
11
12
AllowOverride FileInfo AuthConfig Limit
Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
Order allow,deny
Allow from all
Order deny,allow
Deny from all
– Tìm [Dòng 990]
Sửa thành
* Sau khi làm xong các bạn nhớ save lại
2. Bước 2: Tạo thư mục public_html và virtualhost
* Để tạo thư mục public_html [hay là thư mục chứa code site các bạn] và thư mục chứa file logs chạy 2 lênh sau
mkdir –p /home/example.com/public_html
mkdir /home/example.com/logs
* Tạo file virtualhost
Các bạn tạo file domain.conf trong thư mục /etc/httpd/conf.d với nội dung như sau
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ServerAdmin contact@example.com
DocumentRoot /home/example.com/public_html
ServerName example.com
ServerAlias www.example.com
ErrorLog “/home/example.com/logs/error.log”
CustomLog “/home/example.com/logs/access.log” common
Options –MultiViews +FollowSymlinks –Indexes
AllowOverride All
Order allow,deny
Allow from all
– Các bạn thay example.com bằng domain của các bạn
* Chú ý: Ở bước tạo thư mục thư mục chứa code và file logs [public_html, logs] không nhất thiết phải để là example.com, các bạn có thể đặt tên thư mục là gì cũng được
vd: /home/abcxyz/public_html, /home/abcxyz/logs
– Đường dẫn trong file virtualhost phải giống với đường dẫn các bạn đã tạo, cụ thể ở các dòng:
DocumentRoot /home/example.com/public_html
ErrorLog “/home/example.com/logs/error.log”
CustomLog “/home/example.com/logs/access.log” common
ví dụ: Mình tạo thư mục chứa code và file logs có path là: /home/vpstut/public_html, /home/vpstut/logs và domain của mình là vpstut.info thì file virtualhost của mình sẽ là
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ServerAdmin contact@vpstut.info
DocumentRoot /home/vpstut/public_html
ServerName vpstut.info
ServerAlias www.vpstut.info
ErrorLog “/home/vpstut/logs/error.log”
CustomLog “/home/vpstut/logs/access.log” common
Options –MultiViews +FollowSymlinks –Indexes
AllowOverride All
Order allow,deny
Allow from all
– Khởi động lại apache
Apache là gì ? Apache hay là chương trình máy chủ HTTP là một chương trình dành cho máy chủ đối thoại qua giao thức HTTP. Apache chạy trên các hệ điều hành tương tự như Unix, Microsoft Windows, Novell Netware và các hệ điều hành khác. Apache đóng một vai trò quan trọng trong quá trình phát triển của mạng web thế giới [tiếng Anh: World Wide Web].
Cài đặt và cấu hình Apache
1. Cài đặt
– Thực hiện trên VPS chạy Centos 6
– Chạy lệnh
2. Cấu hình * Mở file /etc/httpd/conf/httpd.conf – Tìm [Dòng 44]
Sửa thành
– Tìm [Dòng 76]
Sửa thành
-Tìm [Dòng 338]
Sửa thành
– Tìm [Dòng 276]
Sửa thành
ServerName www.example.com:80
– Tìm [Dòng 536]
Sửa thành
– Tìm [Dòng 759]
Sửa thành
– Tìm [Dòng 402]
DirectoryIndex index.html index.html.var
Sửa thành
DirectoryIndex index.php index.html index.htm
* Sau khi sửa xong các bạn nhớ save lại nhé
2. Khởi động Apache
– Chạy lần lượt 2 lệnh sau
chkconfig —level 235 httpd on
service httpd start
3. Test webserver – Tạo file index.html trong thư mục /var/www/html/ với nội dung bất kỳ – Ví dụ
1
2
3
4
5
6
7
8
9
10
11
12
Vpstut.info – Hướng dẫn cài đặt và cấu hình VPS
Vpstut.info
Hướng dẫn cài đặt và cấu hình VPS
Tương tự như LEMP server, khi cài đặt xong LAMP các bạn nên thực hiện tối ưu server bằng các thủ thuật sau.
Tận dụng browser caching
Mình sẽ hướng dẫn các bạn cách thêm Expires headers vào file .htaccess để tận dụng browser caching. Phương pháp này giúp giảm thiểu tối đa HTTP request đến server bằng cách sử dụng những file tĩnh như hình ảnh, css, javascript đã lưu trong cache của browser lúc bạn truy cập website lần đầu tiên.
Cách thêm Expires header
Trong ví dụ này mình sẽ thêm header cho một số loại file tĩnh như:
- images: jpg, gif, png
- favicon/ico
- javascript
- css
Thời gian lưu cache thì bạn có thể tùy chọn
- years
- months
- weeks
- days
- hours
- minutes
- seconds
Đây là đoạn code mình sử dụng, các bạn thêm vào cuối file .htaccess
Enable expirations
ExpiresActive On
Default directive
ExpiresDefault "access plus 1 month"
My favicon
ExpiresByType image/x-icon "access plus 1 year"
Images
ExpiresByType image/gif "access plus 1 month" ExpiresByType image/png "access plus 1 month" ExpiresByType image/jpg "access plus 1 month" ExpiresByType image/jpeg "access plus 1 month"
CSS
ExpiresByType text/css "access 1 month"
Javascript
ExpiresByType application/javascript "access plus 1 year"
Kích hoạt GZIP Compression cho Apache Server
GZIP giúp tối ưu và tăng tốc website bằng cách nén nội dung trả về từ server mỗi khi có request gửi đến.
Để thực hiện, các bạn chỉ cần thêm đoạn sau vào cuối file .htaccess. Apache 1.3 sử dụng mod_gzip trong khi Apache 2.x sử dụng mod_deflate.
Cấu hình mod_gzip cho Apache 1.3.x
mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file .[html?|txt|css|js|php]$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/. mod_gzip_item_include mime ^application/x-javascript. mod_gzip_item_exclude mime ^image/. mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.
Cấu hình mod_deflate cho Apache 2.x
SetOutputFilter DEFLATE
Don't compress
SetEnvIfNoCase Request_URI .[?:gif|jpe?g|png]$ no-gzip dont-vary SetEnvIfNoCase Request_URI .[?:exe|t?gz|zip|bz2|sit|rar]$ no-gzip dont-vary
Dealing with proxy servers
Header append Vary User-Agent
Để test lại xem đã thành công hay chưa các bạn sử dụng tool //redbot.org, điền link site bạn vào và tìm xem có dòng Content-Encoding: gzip trong kết quả trả về là được.
About LAMP
LAMP stack is a group of open source software used to get web servers up and running. The acronym stands for Linux, Apache, MySQL, and PHP. Since the server is already running CentOS, the linux part is taken care of. Here is how to install the rest.
Set Up
The steps in this tutorial require the user on the virtual private server to have root privileges. You can see how to set that up in the Initial Server Setup Tutorial in steps 3 and 4.
Step One—Install Apache
Apache is a free open source software which runs over 50% of the world’s web servers.
To install apache, open terminal and type in this command:
sudo yum install httpd
Once it installs, you can start apache running on your VPS:
sudo service httpd start
That’s it. To check if Apache is installed, direct your browser to your server’s IP address [eg. //12.34.56.789]. The page should display the words “It works!” like this.
How to find your Server’s IP address
You can run the following command to reveal your server’s IP address.
ifconfig eth0 | grep inet | awk '{ print $2 }'
Step Two—Install MySQL
MySQL is a powerful database management system used for organizing and retrieving data on a virtual server
To install MySQL, open terminal and type in these commands:
sudo yum install mysql-server sudo service mysqld start
During the installation, MySQL will ask you for your permission twice. After you say Yes to both, MySQL will install.
Once it is done installing, you can set a root MySQL password:
sudo /usr/bin/mysql_secure_installation
The prompt will ask you for your current root password.
Since you just installed MySQL, you most likely won’t have one, so leave it blank by pressing enter.
Enter current password for root [enter for none]: OK, successfully used password, moving on...
Then the prompt will ask you if you want to set a root password. Go ahead and choose Y and follow the instructions.
CentOS automates the process of setting up MySQL, asking you a series of yes or no questions.
It’s easiest just to say Yes to all the options. At the end, MySQL will reload and implement the new changes.
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!
Step Three—Install PHP
PHP is an open source web scripting language that is widely used to build dynamic webpages.
To install PHP on your virtual private server, open terminal and type in this command:
mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file .[html?|txt|css|js|php]$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/. mod_gzip_item_include mime ^application/x-javascript. mod_gzip_item_exclude mime ^image/. mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.
0
Once you answer yes to the PHP prompt, PHP will be installed.
PHP Modules
PHP also has a variety of useful libraries and modules that you can add onto your server. You can see the libraries that are available by typing:
mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file .[html?|txt|css|js|php]$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/. mod_gzip_item_include mime ^application/x-javascript. mod_gzip_item_exclude mime ^image/. mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.
1
Terminal then will display the list of possible modules. The beginning looks like this:
mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file .[html?|txt|css|js|php]$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/. mod_gzip_item_include mime ^application/x-javascript. mod_gzip_item_exclude mime ^image/. mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.
2
To see more details about what each module does, type the following command into terminal, replacing the name of the module with whatever library you want to learn about.
mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file .[html?|txt|css|js|php]$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/. mod_gzip_item_include mime ^application/x-javascript. mod_gzip_item_exclude mime ^image/. mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.
3
Once you decide to install the module, type:
mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file .[html?|txt|css|js|php]$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/. mod_gzip_item_include mime ^application/x-javascript. mod_gzip_item_exclude mime ^image/. mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.
4
You can install multiple libraries at once by separating the name of each module with a space.
Congratulations! You now have LAMP stack on your droplet!
We should also set the processes to run automatically when the server boots [php will run automatically once Apache starts]:
mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file .[html?|txt|css|js|php]$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/. mod_gzip_item_include mime ^application/x-javascript. mod_gzip_item_exclude mime ^image/. mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.
5
Step Four—RESULTS: See PHP on your Server
Although LAMP is installed on your virtual server, we can still take a look and see the components online by creating a quick php info page
To set this up, first create a new file:
mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file .[html?|txt|css|js|php]$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/. mod_gzip_item_include mime ^application/x-javascript. mod_gzip_item_exclude mime ^image/. mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.
6
Add in the following line:
mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file .[html?|txt|css|js|php]$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/. mod_gzip_item_include mime ^application/x-javascript. mod_gzip_item_exclude mime ^image/. mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.
7
Then Save and Exit.
Restart apache so that all of the changes take effect on your virtual server:
mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file .[html?|txt|css|js|php]$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/. mod_gzip_item_include mime ^application/x-javascript. mod_gzip_item_exclude mime ^image/. mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.
8
Finish up by visiting your php info page [make sure you replace the example ip address with your correct one]: //12.34.56.789/info.php
It should look similar to this.
Apache là một Webserver rất phổ biến hiện nay, bạn cứ tưởng tượng là các gói Shared Host có sử dụng cPanel X đa phần đều dùng Apache hết. Do nó phổ biến và dễ cấu hình hơn nên mình sẽ hướng dẫn cho bạn thực hành, chứ để tối ưu nhất thì sẽ sử dụng NGINX Webserver nhưng mình không nói tới ở các bài đầu tiên.
Mục lục nội dung
I. Webserver là gì?
Webserver là một dịch vụ [service] hỗ trợ bạn hiển thị dữ liệu trên VPS đến người dùng thông qua cổng mặc định là 80. Một webserver có thể tạo ra nhiều VirtualHost, và mỗi VirtualHost có thể mang 1 IP và 1 domain khác nhau.
Nói dễ hiểu hơn, VirtualHost chính là cách mà bạn thêm 1 domain khác vào VPS để chạy web trên đó.
II. Cách cài đặt Apache Webserver
Trước khi cài, mình xin nhắc lại là bạn nên cài trên CentOS 6.5 nếu bạn làm theo hướng dẫn này, CentOS 7.0 đã thay đổi rất nhiều nên mình sẽ có 1 tutorial riêng cho nó.
Hãy đăng nhập vào SSH và tiến hành tắt SELinux đi bằng cách mở file /etc/sysconfig/selinux và tìm
SetOutputFilter DEFLATE
Don't compress
SetEnvIfNoCase Request_URI .[?:gif|jpe?g|png]$ no-gzip dont-vary SetEnvIfNoCase Request_URI .[?:exe|t?gz|zip|bz2|sit|rar]$ no-gzip dont-vary
Dealing with proxy servers
Header append Vary User-Agent
0 rồi sửa thành như dưới đây:
Sau đó gõ lệnh
SetOutputFilter DEFLATE
Don't compress
SetEnvIfNoCase Request_URI .[?:gif|jpe?g|png]$ no-gzip dont-vary SetEnvIfNoCase Request_URI .[?:exe|t?gz|zip|bz2|sit|rar]$ no-gzip dont-vary
Dealing with proxy servers
Header append Vary User-Agent
1 để khởi động lại máy chủ.
Đăng nhập lại SSH và bắt đầu cài Apache với lệnh
SetOutputFilter DEFLATE
Don't compress
SetEnvIfNoCase Request_URI .[?:gif|jpe?g|png]$ no-gzip dont-vary SetEnvIfNoCase Request_URI .[?:exe|t?gz|zip|bz2|sit|rar]$ no-gzip dont-vary
Dealing with proxy servers
Header append Vary User-Agent
2 với hành động là
SetOutputFilter DEFLATE
Don't compress
SetEnvIfNoCase Request_URI .[?:gif|jpe?g|png]$ no-gzip dont-vary SetEnvIfNoCase Request_URI .[?:exe|t?gz|zip|bz2|sit|rar]$ no-gzip dont-vary
Dealing with proxy servers
Header append Vary User-Agent
3 và cài phần mềm tên là là
SetOutputFilter DEFLATE
Don't compress
SetEnvIfNoCase Request_URI .[?:gif|jpe?g|png]$ no-gzip dont-vary SetEnvIfNoCase Request_URI .[?:exe|t?gz|zip|bz2|sit|rar]$ no-gzip dont-vary
Dealing with proxy servers
Header append Vary User-Agent
4. Đây nghĩa là phần mềm webserver Apache.
Nếu nó hỏi như thế này thì ấn
SetOutputFilter DEFLATE
Don't compress
SetEnvIfNoCase Request_URI .[?:gif|jpe?g|png]$ no-gzip dont-vary SetEnvIfNoCase Request_URI .[?:exe|t?gz|zip|bz2|sit|rar]$ no-gzip dont-vary
Dealing with proxy servers
Header append Vary User-Agent
5 rồi enter nhé.
01
02
03
SetOutputFilter DEFLATE
Don't compress
SetEnvIfNoCase Request_URI .[?:gif|jpe?g|png]$ no-gzip dont-vary SetEnvIfNoCase Request_URI .[?:exe|t?gz|zip|bz2|sit|rar]$ no-gzip dont-vary
Dealing with proxy servers
Header append Vary User-Agent
6
SetOutputFilter DEFLATE
Don't compress
SetEnvIfNoCase Request_URI .[?:gif|jpe?g|png]$ no-gzip dont-vary SetEnvIfNoCase Request_URI .[?:exe|t?gz|zip|bz2|sit|rar]$ no-gzip dont-vary
Dealing with proxy servers
Header append Vary User-Agent
7
SetOutputFilter DEFLATE
Don't compress
SetEnvIfNoCase Request_URI .[?:gif|jpe?g|png]$ no-gzip dont-vary SetEnvIfNoCase Request_URI .[?:exe|t?gz|zip|bz2|sit|rar]$ no-gzip dont-vary
Dealing with proxy servers
Header append Vary User-Agent
8
Sau khi nó hiện ra chữ Complete! tức là đã cài thành công.
Tiêp theo là tiến hành khởi động service httpd mà mình vừa cài đặt.
nếu nó có hiện “httpd: Could not reliably determine the server’s fully qualified domain name, using 127.0.0.1 for ServerName” thì kệ nó, chẳng qua là do bạn chưa trỏ IP về hostname thôi. Ở cuối bài mình có chỉ bạn cách đổi ServerName để nó khỏi báo nữa.
Một vài nhà cung cấp có thể sẽ chặn cổng 80 ở mặc định, bạn có thể nhập thêm 3 lệnh sau để mở cổng 80 ra nếu bị khóa:
01
02
03
SetOutputFilter DEFLATE
Don't compress
SetEnvIfNoCase Request_URI .[?:gif|jpe?g|png]$ no-gzip dont-vary SetEnvIfNoCase Request_URI .[?:exe|t?gz|zip|bz2|sit|rar]$ no-gzip dont-vary
Dealing with proxy servers
Header append Vary User-Agent
9
sudo yum install httpd
0
sudo yum install httpd
1
Bây giờ bạn hãy thử truy cập vào website với địa chỉ là IP của VPS xem, nếu nó hiện ra trang Welcome của Apache là thành công.
Nhưng có một vấn đề là các service bạn cài vào sẽ không tự động khởi động nếu bạn reboot lại VPS, nên bạn sẽ cần gõ thêm lệnh này để đưa service httpd vào danh sách tự động khởi động khi reboot.
Mặc định các dữ liệu hiển thị ra website sẽ nằm ở thư mục /var/www. Bạn có thể truy cập vào đó để upload file bất kỳ lên rồi truy cập bằng đường dẫn là IP VPS của bạn.
Bật mod_rewrite
Để sử dụng tính năng Permalink trong WordPress cũng như các chức năng rewrite đường dẫn khác, hãy mở file /etc/httpd/conf/httpd.conf và tìm:
01
02
03
04
05
06
07
sudo yum install httpd
2
sudo yum install httpd
3
sudo yum install httpd
4
sudo yum install httpd
5
sudo yum install httpd
6
Thay đoạn
sudo yum install httpd
6 trong đó thành
sudo yum install httpd
8.
Cuối cùng là gõ
sudo yum install httpd
9 để khởi động lại Apache.
III. Cách tạo VirtualHost cho Apache
Bạn làm lại bước này nếu cần thêm một domain khác vào VPS.
Ở trên chỉ là bước cài Apache và mình không khuyến khích bạn sử dụng thư mục /var/www để upload website lên, mà hãy tạo ra một thư mục mới theo ý của bạn rồi thêm domain cần sử dụng cho thư mục đó, cái này chúng ta sẽ gọi là VirtualHost [Host ảo].
Để tạo VirtualHost, thì việc đầu tiên là bạn cần phải tạo một thư mục public_html [hoặc bạn cũng có thể ghi là www tùy bạn] trong một thư mục nào đó, ở đây mình khuyến khích các bạn đặt trong /home/webdata/tên-example.com. Chúng ta tiến hành tạo thư mục với lệnh sau:
01
sudo service httpd start
0
sudo service httpd start
1
sudo service httpd start
2
sudo service httpd start
3
sudo service httpd start
4
sudo service httpd start
5
Các bạn nhớ thay
sudo service httpd start
6 thành domain mà bạn muốn thêm vào VPS, tham số
sudo service httpd start
7 ở đây nghĩa là bạn ép nó tự tạo ra đoạn thư mục trên nếu nó chưa có. Chẳng hạn trong thư mục /home của mình chưa cówebdata/ nên chúng ta sẽ sử dụng tham số
sudo service httpd start
7 để ép nó tạo ra thư mục
sudo service httpd start
9.
Bạn có thể kiểm tra xem nó đã tạo thành công chưa bằng lệnh
ifconfig eth0 | grep inet | awk '{ print $2 }'
0.
Tiếp theo là chúng ta sẽ cho user apache và group apache sở hữu thư mục này bởi vì thư mục này đang thuộc quyền sở hữu của user mà bạn tiến hành tạo, thay vì CHMOD cho an toàn hơn.
01
sudo service httpd start
0
ifconfig eth0 | grep inet | awk '{ print $2 }'
2
ifconfig eth0 | grep inet | awk '{ print $2 }'
3
sudo service httpd start
3
sudo service httpd start
4
Nếu bạn không cấp quyền thư mục này cho user apache thì khi cài WordPress, nó không thể tự động sửa file [chẳng hạn như tự thêm nội dung vào file .htaccess khi cài plugin] và nó sẽ bắt bạn nhập tài khoản FTP khi cài Plugin/Theme.
Bạn có thể kiểm tra quyền sở hữu của thư mục này với lệnh:
01
ifconfig eth0 | grep inet | awk '{ print $2 }'
6
ifconfig eth0 | grep inet | awk '{ print $2 }'
7
sudo service httpd start
3
sudo service httpd start
4
Kế tiếp là hãy tạo một file index.html trong thư mục /home/webdata/thachpham.dev/public_html để chút nữa ta sẽ kiểm tra, với nội dung như sau:
01
sudo service httpd start
0
sudo yum install mysql-server sudo service mysqld start
1
sudo service httpd start
3
sudo service httpd start
4
sudo yum install mysql-server sudo service mysqld start
4
sudo yum install mysql-server sudo service mysqld start
5
01
02
03
04
05
06
07
08
sudo yum install mysql-server sudo service mysqld start
6
sudo yum install mysql-server sudo service mysqld start
7
sudo yum install mysql-server sudo service mysqld start
8
sudo yum install mysql-server sudo service mysqld start
9
sudo yum install mysql-server sudo service mysqld start
6
sudo /usr/bin/mysql_secure_installation
1
sudo yum install mysql-server sudo service mysqld start
8
sudo /usr/bin/mysql_secure_installation
3
sudo yum install mysql-server sudo service mysqld start
6
sudo /usr/bin/mysql_secure_installation
5
sudo /usr/bin/mysql_secure_installation
6
sudo /usr/bin/mysql_secure_installation
5
sudo yum install mysql-server sudo service mysqld start
8
sudo yum install mysql-server sudo service mysqld start
9
Enter current password for root [enter for none]: OK, successfully used password, moving on...
0
sudo /usr/bin/mysql_secure_installation
1
sudo yum install mysql-server sudo service mysqld start
8
sudo yum install mysql-server sudo service mysqld start
9
sudo yum install mysql-server sudo service mysqld start
6
Enter current password for root [enter for none]: OK, successfully used password, moving on...
5
sudo yum install mysql-server sudo service mysqld start
8
sudo /usr/bin/mysql_secure_installation
3
sudo yum install mysql-server sudo service mysqld start
6
Enter current password for root [enter for none]: OK, successfully used password, moving on...
9
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!
0
Enter current password for root [enter for none]: OK, successfully used password, moving on...
9
sudo yum install mysql-server sudo service mysqld start
8
sudo yum install mysql-server sudo service mysqld start
9
Enter current password for root [enter for none]: OK, successfully used password, moving on...
0
Enter current password for root [enter for none]: OK, successfully used password, moving on...
5
sudo yum install mysql-server sudo service mysqld start
8
Enter current password for root [enter for none]: OK, successfully used password, moving on...
0
sudo yum install mysql-server sudo service mysqld start
7
sudo yum install mysql-server sudo service mysqld start
8
Kế tiếp, hãy mở file /etc/httpd/conf/httpd.conf và tìm đoạn dưới đây rồi xóa dấu # đi [bước này chỉ làm 1 lần, lần thêm domain sau khỏi cần làm].
Đoạn này nằm gần cuối file.
Sau đó bạn vào thư mục /etc/httpd/conf.d và tạo thêm một file .conf tên bất kỳ. Ví dụ mình muốn thêm domain thachpham.dev vào VPS thì sẽ tạo file tên là thachpham.dev.conf cho dễ phân biệt. Trong nội dung, mình chèn đoạn sau vào:
mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file .[html?|txt|css|js|php]$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/. mod_gzip_item_include mime ^application/x-javascript. mod_gzip_item_exclude mime ^image/. mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.
9
Mình xin giải thích các thông số ở đoạn trên như sau:
- mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file .[html?|txt|css|js|php]$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/. mod_gzip_item_include mime ^application/x-javascript. mod_gzip_item_exclude mime ^image/. mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip. 00 – Nghĩa là cái domain này sẽ sử dụng port 80, bạn thiết lập giống với port của Apache nhé.
- mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file .[html?|txt|css|js|php]$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/. mod_gzip_item_include mime ^application/x-javascript. mod_gzip_item_exclude mime ^image/. mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip. 01 – Email của người quản trị.
- mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file .[html?|txt|css|js|php]$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/. mod_gzip_item_include mime ^application/x-javascript. mod_gzip_item_exclude mime ^image/. mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip. 02 – Thư mục gốc của domain này, chúng ta đã tạo ra nó ở đầu bài.
- mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file .[html?|txt|css|js|php]$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/. mod_gzip_item_include mime ^application/x-javascript. mod_gzip_item_exclude mime ^image/. mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip. 03 – domain của VirtualHost.
- mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file .[html?|txt|css|js|php]$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/. mod_gzip_item_include mime ^application/x-javascript. mod_gzip_item_exclude mime ^image/. mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip. 04 – Thêm tùy chọn để truy cập vào domain không cần www.
- mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file .[html?|txt|css|js|php]$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/. mod_gzip_item_include mime ^application/x-javascript. mod_gzip_item_exclude mime ^image/. mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip. 05 – Đường dẫn file log báo lỗi webserver của domain này. Bạn có thể bỏ dấu # để dùng chức năng này nhằm theo dõi lỗi, và nhớ tạo 1 file riêng để nó lưu lại lỗi rồi nhập đường dẫn của file log đó vào.
Trường hợp VPS bạn có nhiều IP và bạn muốn VirtualHost này sử dụng 1 IP riêng thì thêm đoạn
mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file .[html?|txt|css|js|php]$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/. mod_gzip_item_include mime ^application/x-javascript. mod_gzip_item_exclude mime ^image/. mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.
06 ở ngay trên
mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file .[html?|txt|css|js|php]$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/. mod_gzip_item_include mime ^application/x-javascript. mod_gzip_item_exclude mime ^image/. mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.
01.
Sau này bạn muốn thêm nhiều VirtualHost thì cứ copy cái đoạn trên rồi sửa lại thôi là được. Bây giờ chúng ta Save file và thoát ra.
Ok, hãy khởi động lại Apache để nó cập nhật thay đổi nhé.
01
sudo service httpd start
0
sudo yum install httpd
9
01
02
mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file .[html?|txt|css|js|php]$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/. mod_gzip_item_include mime ^application/x-javascript. mod_gzip_item_exclude mime ^image/. mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.
10
mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file .[html?|txt|css|js|php]$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/. mod_gzip_item_include mime ^application/x-javascript. mod_gzip_item_exclude mime ^image/. mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.
11
Phần stopping nó bị FAILED thì kệ nó, chẳng qua là do chúng ta restart bất chợt thôi, gõ restart lại lần nữa là hết.
Cuối cùng là hãy trỏ domain về IP của VPS hoặc IP mà bạn đã thiết lập cho VirtualHost nhé, nếu truy cập vào domain đó mà nó ra file Welcome mà chúng ta đã tạo ở trên thì thành công.
Hỏi/Đáp khi tạo VirtualHost
Tại sao tôi thêm 2 domain vào nhưng cả 2 lại trỏ về 1 domain?
Lý do là bạn chưa tiến hành gõ lệnh chown -R apache:apache /home/webdata/example.com/ cho cả 2 hoặc chỉ gõ cho 1 domain. Tốt nhất mỗi khi thêm 1 domain vào, hãy gõ lệnh
mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file .[html?|txt|css|js|php]$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/. mod_gzip_item_include mime ^application/x-javascript. mod_gzip_item_exclude mime ^image/. mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.
12.
Khi thêm domain vào, tôi trỏ domain về VPS thế nào?
Hãy trỏ bằng cách sử dụng các dịch vụ quản lý DNS trung gian như CloudFlare hoặc trình quản lý các record DNS của nhà cung cấp domain rồi trỏ record A về IP của VPS.
Tối ưu Apache để tiết kiệm CPU
Mặc định có thể bạn sẽ thấy Apache nó “ăn” hơi bị nhiều CPU do sử dụng quá nhiều process. Để thiết lập lại số lượng processes mà Apache được phép tạo ra, hãy mở file /etc/httpd/conf/httpd.conf lên và tìm đoạn:
01
02
03
04
05
06
07
08
mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file .[html?|txt|css|js|php]$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/. mod_gzip_item_include mime ^application/x-javascript. mod_gzip_item_exclude mime ^image/. mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.
13
mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file .[html?|txt|css|js|php]$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/. mod_gzip_item_include mime ^application/x-javascript. mod_gzip_item_exclude mime ^image/. mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.
14
mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file .[html?|txt|css|js|php]$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/. mod_gzip_item_include mime ^application/x-javascript. mod_gzip_item_exclude mime ^image/. mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.
15
mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file .[html?|txt|css|js|php]$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/. mod_gzip_item_include mime ^application/x-javascript. mod_gzip_item_exclude mime ^image/. mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.
16
mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file .[html?|txt|css|js|php]$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/. mod_gzip_item_include mime ^application/x-javascript. mod_gzip_item_exclude mime ^image/. mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.
17
mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file .[html?|txt|css|js|php]$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/. mod_gzip_item_include mime ^application/x-javascript. mod_gzip_item_exclude mime ^image/. mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.
18
Ở đoạn trên, bạn sửa lại các phần thành những giá trị sau:
- mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file .[html?|txt|css|js|php]$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/. mod_gzip_item_include mime ^application/x-javascript. mod_gzip_item_exclude mime ^image/. mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip. 19 : 1
- mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file .[html?|txt|css|js|php]$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/. mod_gzip_item_include mime ^application/x-javascript. mod_gzip_item_exclude mime ^image/. mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip. 20 : 1
- mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file .[html?|txt|css|js|php]$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/. mod_gzip_item_include mime ^application/x-javascript. mod_gzip_item_exclude mime ^image/. mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip. 21 : 5
- mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file .[html?|txt|css|js|php]$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/. mod_gzip_item_include mime ^application/x-javascript. mod_gzip_item_exclude mime ^image/. mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip. 22 : 10000
Sau này nếu cần thêm VirtualHost, bạn chỉ cần copy file thachpham.dev.conf kia, đổi tên thành domain cần thêm cũng như sửa file để thay thachpham.dev thành domain mới của bạn.
Lời kết
Trong bài này chúng ta đã tìm hiểu qua cách cài Apache Webserver và thiết lập VirtualHost cho nó. Đây là bước quan trọng mà bạn có thể cần làm đi làm lại nhiều lần cho nhuần nhuyễn để có thể chủ động hơn về sau.
Tuy nhiên hiện tại bạn chưa chạy được WordPress đâu bởi vì chúng ta còn thiếu ứng dụng PHP và MySQL, chúng ta sẽ cài ở các bài sau.