Blog 10-30-22
1 Install Nginx and related stacks on CentOS 8
1.1 Automated Script
I use HocVPS Script to simplify the installation process, allowing for simple management in the future.
HocVPS Script will support:
- Centos 7
- Centos 8
- Rocky Linux 8
- Ubuntu 18.04
- Ubuntu 20.04
HocVPS Script will automatically install:
- Webserver nginx latest version.
- The latest version of database MariaDB (MySQL is optimized).
- Option to install and update New Mariadb versions during setup
- PHP latest version options: PHP 8.0, PHP7.4, PHP 7.3, PHP 7.2, PHP 7.1, PHP 7.0, PHP 5.6; comes Zend OPcache which makes - PHP work fastest
- latest phpMyAdmin
- the latest eXtplorer to manage File Manager, can create user, separate permissions
- Option to install firewall using CSF
- Support to install let’s Encrypt SSL certificate for website
- Supports installation of commercial SSL certificates (Sectigo, Geotrust, IDE)
Run the following command to install:
|
|
When the prompt to enter your email appears, just input your working email.
THen enter PHP version that you want to use. I choose the latest PHP 8.0
Enter your domain, for example: pquan.info
Then enter your secret desired port admin, which is from the range of (2000 – 9999), can be change later. It’s used for these following stuff:
- Access the admin link, in the form of:
http://domain.com:port/
- Using phpMyAdmin, link form:
http://domain.com:port/phpmyadmin/
- File Manager, link form:
http://domain.com:port/filemanager/
- Monitor System Status, link form:
http://domain.com:port/serverinfo/
- Track Zend Opcache Status, link form:
http://domain.com:port/op.php
You’ll be ask to specify MariaDB version:
After that, you let the script automatically perform the installation process, which can take 3-5 minutes depending on the configuration and network of the VPS / Server.
Finally, if no problem occurs, you will receive successful installation notifications and VPS management information as below. At the same time, this information will also be saved in the text file with the path /root/hocvps-script.txt for you to review later.
1.2 How to use the script
After installing hocvps Script, you can use SFTP to manage files, upload code to /home/domain.com/public_html/
and point the domain name to IP of the VPS and start using.
Run hocvps
to access the script menu.
|
|
Note: after uploading the source to the web folder, you use Hocvps menu 14 Webserver permissions
for NGINX to read the website content.
If you want to connect to SSH, use port 2222
.
During use, being in any function you can press Ctrl + C
will exit the Script immediately.
1.3 Installing the latest Nginx version
Create the following file:
|
|
After that add the following content which specifies the Nginx repository which we will use to install the latest Nginx version:
|
|
Save the file and exit. Then run hocvps
script.
Choose menu 15, then select option 2 to update Nginx to latest stable version.
1.4 Configure SSL Certificate for your domain
1.4.1 Install Let’s Encrypt
Install Certbot:
|
|
Issue Let’s Encrypt for domain:
|
|
Next you enter the domain name that will use the SSL certificate, and then press Enter. This step you only enter the non-www And www versions of 1 domain or subdomain.
1.4.2 Nginx Configuration
Create a DH Parameters 2048 bit file:
|
|
Note: When the number of bits, in this case 4096, comes after the option where to put the file (-out), openssl seems to output meaningful data. That is the correct way. The script may fail to create a param file, if you put the option after the numbits, be it 512, 2048 or 4096.
Modify the domain cofiguration by editing yourdomain.com.conf
:
|
|
Configuring SSL processing requests
In the block server { … } adjustment as follows:
Convert listen 80 default_server
to listen 443 ssl default_server
;
After the server_name yourdomain.com
added SSL configuration snippet:
|
|
The Nginx configuration file will be similar to the following:
|
|
2 Installing Iptables on CentOS
HocVPS Script will also install iptables
. So you can skip installing steps and go straight to iptables Commands and Options.
In CentOS, iptables
was replaced by firewalld
.
To install iptables
, first you need to stop firewalld
. Enter the following commands:
|
|
The commands stop and prevent firewalld from starting at boot, and do not let other services start firewalld.
Next, install and enable iptables. First, install the iptables services package with the following command:
|
|
Enter the following commands to enable and start iptables in CentOS:
|
|
2.1 Basic Syntax for iptables Commands and Options
In general, an iptables command looks as follows:
|
|
Here is a list of some common iptables options:
-A –append – Add a rule to a chain (at the end). -C –check – Look for a rule that matches the chain’s requirements. -D –delete – Remove specified rules from a chain. -F –flush – Remove all rules. -I –insert – Add a rule to a chain at a given position. -L –list – Show all rules in a chain. -N -new-chain – Create a new chain. -v –verbose – Show more information when using a list option. -X –delete-chain – Delete the provided chain.
2.2 Check Current iptables Status:
To view the current set of rules on your server, enter the following in the terminal window:
|
|
2.3 Enable Loopback Traffic:
It’s safe to allow traffic from your own system (the localhost). Append the Input chain by entering the following:
|
|
2.4 Allow Traffic on Specific Ports:
|
|
The options work as follows:
-p – Check for the specified protocol (tcp). –dport – Specify the destination port. -j jump – Take the specified action.
2.5 Delete a Rule:
You can use the -F option to clear all iptables
firewall rules. A more precise method is to delete the line number of a rule.
First, list all rules by entering the following:
|
|
Locate the line of the firewall rule you want to delete and run this command:
|
|
2.6 Save Your Changes:
Iptables does not keep the rules you created when the system reboots. Whenever you configure iptables in Linux, all the changes you make apply only until the first restart.
To save the rules in Debian-based systems, enter:
|
|
To save the rules in Red-Hat based systems, enter:
|
|
The next time your system starts, iptables will automatically reload the firewall rules.