127.0.0.1 localhost
127.0.0.1 test-site
127.0.1.1 my-hostname
# The following lines are desirable for IPv6 capable hosts. etc...
Đâu test-site
là "localhost" thứ hai. Và my-hostname
là "Tên máy chủ hệ thống" được định nghĩa trong /etc/hostname
.
2. Bạn nên xác định và kích hoạt Máy chủ ảo (VH):
Có một HTTP VH mặc định. Nó được đặt trong /etc/apache2/sites-available/
. Tên tệp là 000-default.conf
. Bạn phải chỉnh sửa nó (bạn có thể đổi tên nó, nếu bạn muốn hoặc tạo một số tệp .conf khác, dựa trên nó) và sau đó bạn phải kích hoạt nó.
Bạn có thể kích hoạt thủ công thông qua việc tạo "liên kết tượng trưng, mềm mại":
sudo ln -s /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-enabled/
Hoặc bạn có thể sử dụng công cụ Apache2 có tên a2ensite , công cụ này cũng tương tự:
sudo a2ensite 000-default.conf
Giả sử có 3 Máy chủ ảo , SSL được kích hoạt và tên miền riêng đã đăng ký (ví dụ: SOS.info):
/etc/apache2/sites-available/http.SOS.info.conf
/etc/apache2/sites-available/https.SOS.info.conf
Và một cái được tạo ra cho mục đích của chủ đề này:
/etc/apache2/sites-available/http.test-site.conf
Nội dung của 2 VH đầu tiên là:
$ cat /etc/apache2/sites-available/
http.SOS.info.conf
<VirtualHost *:80>
ServerName SOS.info
ServerAlias www.SOS.info
ServerAdmin admin@SOS.info
# Redirect Requests to SSL
Redirect permanent "/" "https://SOS.info/"
ErrorLog ${APACHE_LOG_DIR}/http.SOS.info.error.log
CustomLog ${APACHE_LOG_DIR}/http.SOS.info.access.log combined
</VirtualHost>
Điều này chuyển hướng tất cả các yêu cầu HTTP đến HTTPS.
$ cat /etc/apache2/sites-available/
https.SOS.info.conf
<IfModule mod_ssl.c>
<VirtualHost _default_:443>
ServerName SOS.info
ServerAlias www.SOS.info
ServerAdmin admin@SOS.info
DocumentRoot /var/www/html
SSLEngine on
SSLCertificateFile /etc/ssl/certs/SOS.info.crt
SSLCertificateKeyFile /etc/ssl/private/SOS.info.key
SSLCertificateChainFile /etc/ssl/certs/SOS.info.root-bundle.crt
#etc..
</VirtualHost>
</IfModule>
Đây là HTTPS VH.
Nội dung của hai tệp này có thể được đăng trong một tệp, nhưng trong trường hợp này, việc quản lý ( a2ensite
/ a2dissite
) của chúng sẽ khó khăn hơn.
Máy chủ ảo thứ ba là cái được tạo ra cho mục đích của chúng tôi :
$ cat /etc/apache2/sites-available/
http.test-site.conf
<VirtualHost *:80>
ServerName test-site
ServerAlias test-site.SOS.info
DocumentRoot /var/www/test-site
DirectoryIndex index.html
ErrorLog ${APACHE_LOG_DIR}/test-site.error.log
CustomLog ${APACHE_LOG_DIR}/test-site.access.log combined
<Directory /var/www/test-site>
# Allow .htaccess
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
3. Với cấu hình này, bạn nên truy cập:
http://localhost # pointed to the directory of the mine Domain
https://localhost # iin our case: /var/www/html (SOS.info), but you should get an error, because the SSL certificate
http://SOS.info # which redirects to https://SOS.info
https://SOS.info # you should have valid SSL certificate
http://www.SOS.info # which is allied to http://SOS.info and redirects to https://SOS.info
https://www.SOS.info # which is allied to https://SOS.info
Trên ví dụ chính bạn nên truy cập và :
http://test-site # pointed to the directory /var/www/test-site
http://test-site.SOS.info # which is allied to http://test-site
Cố gắng mở trang web trong trình duyệt web hoặc chỉ thử (trong thiết bị đầu cuối) bằng các lệnh tiếp theo:
$ curl -L http://test-site/index.html
$ curl -L http://test-site.SOS.info/index.html
Tất nhiên, bạn cần phải có một số index.html
trang trong DocumentRoot của họ :)
Tôi sẽ để lại ghi chú tiếp theo bởi lý do của sư phạm :)
4. Bạn cần cấu hình đúng `/ etc / apache2 / apache2.conf`.
Ii là ý tưởng tốt để dành thời gian để cải thiện bảo mật máy chủ của bạn. Các hướng dẫn này là về cấu hình bảo mật: 1 và 2 . Tại đây bạn có thể nhận được chứng chỉ SSL miễn phí. Các trang web này sẽ giúp bạn kiểm tra tiến trình của bạn: thứ 1 và thứ 2 .
Theo hướng dẫn bảo mật /etc/apache2/apache2.conf
tập tin ở trên phải trông giống như:
Mutex file:${APACHE_LOCK_DIR} default
PidFile ${APACHE_PID_FILE}
Timeout 60
#KeepAlive Off
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
HostnameLookups Off
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf
Include ports.conf
<Directory />
Options None FollowSymLinks
AllowOverride None
Require all denied
</Directory>
<Directory /var/www/>
Options None FollowSymLinks
AllowOverride None
Require all granted
</Directory>
AccessFileName .htaccess
<FilesMatch "^\.ht">
Require all denied
</FilesMatch>
LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
IncludeOptional conf-enabled/*.conf
IncludeOptional sites-enabled/*.conf
# Hide Server type in the http error-pages
ServerSignature Off
ServerTokens Prod
# Etag allows remote attackers to obtain sensitive information
FileETag None
# Disable Trace HTTP Request
TraceEnable off
# Set cookie with HttpOnly and Secure flag.
# a2enmod headers
Header edit Set-Cookie ^(.*)$ $1;HttpOnly;Secure
# Clickjacking Attack
Header always append X-Frame-Options SAMEORIGIN
# CX-XSS Protection
Header set X-XSS-Protection "1; mode=block"
# Disable HTTP 1.0 Protocol
RewriteEngine On
RewriteCond %{THE_REQUEST} !HTTP/1.1$
RewriteRule .* - [F]
# Change the server banner @ ModSecurity
# Send full server signature so ModSecurity can alter it
ServerTokens Full
# Alter the web server signature sent by Apache
<IfModule security2_module>
SecServerSignature "Apache 1.3.26"
</IfModule>
Header set Server "Apache 1.3.26"
Header unset X-Powered-By
# Hde TCP Timestamp
# gksu gedit /etc/sysctl.conf
# >> net.ipv4.tcp_timestamps = 0
# Test: sudo hping3 SOS.info -p 443 -S --tcp-timestamp -c 1
# Disable -SSLv2 -SSLv3 and weak Ciphers
SSLProtocol all -SSLv2 -SSLv3
SSLHonorCipherOrder on
SSLCipherSuite "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA EECDH EDH+aRSA !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS !RC4"
5. Thiết lập Tường lửa.
Để cho phép / từ chối truy cập bên ngoài vào máy chủ web của bạn, bạn có thể sử dụng UFW (Tường lửa không biến đổi):
sudo ufw allow http
sudo ufw allow https
Để chỉ cho phép tcp
sử dụng giao thức:
sudo ufw allow http/tcp
sudo ufw allow https/tcp
Bạn có thể sử dụng và số cổng trực tiếp:
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
Chỉ trong trường hợp bạn có thể tải lại "bảng quy tắc":
sudo ufw reload
Bạn có thể sử dụng và giao diện GUI của UFW, được gọi là gufw .
sudo apt update
sudo apt install gufw
gufw &
Chọn Office
hồ sơ. Nó sẽ thiết lập: Status:ON
, Incoming:Deny
và Outgoing:Allow
và thêm các quy tắc của bạn.
6. Nếu bạn có bộ định tuyến đừng quên chuyển tiếp một số cổng:
Nếu bạn có bộ định tuyến và bạn muốn máy chủ web của mình có thể truy cập được từ Internet , đừng quên thêm một số chuyển tiếp cổng. Một cái gì đó như thế này .
ServerName 192.168.0.2
dòng lệnh ServerName nên có tên như www.server.com chứ không phải số IP. Tôi nghĩ rằng điều này có thể giải quyết vấn đề. Đối với Tên máy chủ, bạn nên nhập tên của máy chủ nếu bạn có nó. ServerName cho phép lưu trữ ảo dựa trên tên, cho phép có nhiều trang web hơn trên cùng một IP.