Hosting your own websites on a *NIX server can be an empowering and cost-effective way to manage your web presence. Whether you are hosting a personal blog, an e-commerce site, or a portfolio, having control over your server can provide significant benefits in terms of performance, security, and customization. This guide will walk you through the process of setting up a fully functional hosting environment on a *NIX server using a simple Bash script, this is mainly designed for hosting woocommerce or wordpress sites with high or low traffic, assuming you have 8GB ram with 4 vcpu server.
Introduction
Running your own hosting server may seem daunting at first, but with the right guidance, it can be a straightforward process. This guide is designed to help you set up and manage your own hosting server using a Bash script. The script will handle the installation and configuration of essential services such as Nginx, MariaDB, and PHP, allowing you to focus on your websites rather than server administration.
Prerequisites
Before you begin, ensure that you have the following:
- A *NIX server: This guide is tailored for servers running Linux distributions like Ubuntu or Debian.
- Root or sudo access: You need administrative privileges to install and configure software.
- Basic knowledge of the command line: Familiarity with terminal commands will help you follow this guide.
Step-by-Step Guide
Step 1: Initial Setup
The first step is to prepare your server by updating the package lists and upgrading installed packages. This ensures that you have the latest security updates and software versions.
innodb_buffer_pool_size=4G innodb_log_file_size=1G innodb_flush_log_at_trx_commit=2 innodb_thread_concurrency=8 query_cache_size=64M query_cache_limit=2M max_connections=200 tmp_table_size=64M max_heap_table_size=64M EOL # Restart services to apply changes sudo systemctl restart nginx if [ $? -ne 0 ]; then echo "Failed to restart Nginx. Checking configuration..." sudo nginx -t echo "Please check the error logs and fix the issues before retrying." exit 1 fi sudo systemctl restart mariadb echo "Initial setup and optimizations completed." } initial_setup
Step 2: Adding a Domain
Adding a new domain involves creating a directory structure, generating SSL certificates, configuring Nginx, and setting up PHP-FPM. The script below simplifies this process:
Step 3: Removing a Domain
Removing a domain involves deleting the Nginx and PHP-FPM configurations, the domain's directory, and the associated database. Here's how you can automate this process:
Step 4: Managing Databases
You can also automate the import and export of databases. This can be useful for backups or migrations.
Import Database
# Function to import a database
import_database() {
if [ "$#" -ne 2 ]; then
echo "Usage: $0 import_database domain_name sql_gz_file_path"
exit 1
fi
DOMAIN=$1
SQL_GZ_FILE=$2
# Ensure the SQL file exists
if [ ! -f "$SQL_GZ_FILE" ]; then
echo "The file $SQL_GZ_FILE does not exist."
exit 1
fi
# Prompt for MySQL root password
read -sp "Enter MySQL root password: " MYSQL_ROOT_PASSWORD
echo
# Create a temporary MySQL credentials file
TEMP_MY_CNF=$(mktemp)
chmod 600 $TEMP_MY_CNF
cat <<EOF > $TEMP_MY_CNF
user=root password=$MYSQL_ROOT_PASSWORD EOF # Decompress the SQL file SQL_FILE="${SQL_GZ_FILE%.gz}" gzip -dc "$SQL_GZ_FILE" > "$SQL_FILE" # Replace collation in the SQL file if needed sed -i 's/utf8mb4_unicode_ci/utf8mb4_general_ci/g' "$SQL_FILE" # Import the SQL file into the database DB_NAME=$(echo $DOMAIN | tr -d '.') sudo mysql --defaults-extra-file=$TEMP_MY_CNF $DB_NAME < "$SQL_FILE" # Clean up by removing the decompressed SQL file rm "$SQL_FILE" rm $TEMP_MY_CNF echo "Database imported for $DOMAIN successfully." # Set permissions for the WordPress directory setperm $DOMAIN }
Export Database
# Function to export a database
export_database() {
if [ "$#" -ne 2 ]; then
echo "Usage: $0 export_database domain_name output_sql_gz_file_path"
exit 1
fi
DOMAIN=$1
OUTPUT_SQL_GZ_FILE=$2
# Prompt for MySQL root password
read -sp "Enter MySQL root password: " MYSQL_ROOT_PASSWORD
echo
# Create a temporary MySQL credentials file
TEMP_MY_CNF=$(mktemp)
chmod 600 $TEMP_MY_CNF
cat <<EOF > $TEMP_MY_CNF
user=root password=$MYSQL_ROOT_PASSWORD EOF # Export the database to a SQL file DB_NAME=$(echo $DOMAIN | tr -d '.') SQL_FILE="${OUTPUT_SQL_GZ_FILE%.gz}" sudo mysqldump --defaults-extra-file=$TEMP_MY_CNF $DB_NAME > "$SQL_FILE" # Compress the SQL file gzip "$SQL_FILE" # Clean up by removing the temporary MySQL credentials file rm $TEMP_MY_CNF echo "Database exported for $DOMAIN successfully to $OUTPUT_SQL_GZ_FILE." }
Step 5: Setting Permissions
Setting the correct permissions for your WordPress directories is crucial for security and functionality.
Main Script
Here's the main script that ties all the functions together:
Let's break down the usage of the script and explain each command and its arguments:
echo "Usage: $0 {setup|add_domain|remove_domain|auto_add_domains|import_database|export_database|setperm} "
Script Usage
The script supports several commands to manage your hosting environment. Here’s a brief explanation of each command and how to use it:
1. setup
Usage: ./script.sh setup
- Purpose: Perform the initial setup of your server. This includes updating the system, installing necessary packages (Nginx, MariaDB, PHP), securing MariaDB, and configuring Nginx and MariaDB for optimal performance.
- Arguments: None.
2. add_domain
Usage:
./script.sh add_domain <domain_name> <site_type> <php_version>
- Purpose: Add a new domain to your server. This involves creating a directory structure, generating a self-signed SSL certificate, configuring Nginx, setting up PHP-FPM, and creating a new database.
- Arguments:
<domain_name>: The domain name you want to add (e.g.,example.com).<site_type>: The type of site, which determines the PHP-FPM worker configuration. Useheavyfor high-traffic sites andlightfor low-traffic sites.<php_version>: The PHP version to use. Supported versions are7.4and8.0.
3. remove_domain
Usage: ./script.sh remove_domain <domain_name>
- Purpose: Remove an existing domain from your server. This includes deleting the Nginx and PHP-FPM configurations, the domain's directory, and the associated database.
- Arguments:
<domain_name>: The domain name you want to remove (e.g.,example.com).
4. auto_add_domains
Usage: ./script.sh auto_add_domains
- Purpose: Automatically add a predefined list of domains to your server. This is useful for quickly setting up multiple domains with specific configurations.
- Arguments: None.
5. import_database
Usage:
./script.sh import_database <domain_name> <sql_gz_file_path>
- Purpose: Import a database for a specified domain from a compressed SQL file. This is useful for restoring backups or migrating databases.
- Arguments:
<domain_name>: The domain name associated with the database (e.g.,example.com).<sql_gz_file_path>: The path to the compressed SQL file (e.g.,/path/to/database.sql.gz).
6. export_database
Usage:
./script.sh export_database <domain_name> <output_sql_gz_file_path>
- Purpose: Export the database of a specified domain to a compressed SQL file. This is useful for creating backups or migrating databases.
- Arguments:
<domain_name>: The domain name associated with the database (e.g.,example.com).<output_sql_gz_file_path>: The path to save the compressed SQL file (e.g.,/path/to/backup.sql.gz).
7. setperm
Usage: ./script.sh setperm <domain_name>
- Purpose: Set the correct permissions for a WordPress directory associated with a specified domain. This ensures that the web server can read and write files as needed while maintaining security.
- Arguments:
<domain_name>: The domain name whose directory permissions you want to set (e.g.,example.com).
Example Commands
Here are a few examples of how to use the script:
- Initial Setup:
./script.sh setup
- Add a Domain:
./script.sh add_domain example.com light 7.4
- Remove a Domain:
./script.sh remove_domain example.com
- Automatically Add Predefined Domains:
./script.sh auto_add_domains
- Import a Database:
./script.sh import_database example.com /path/to/database.sql.gz
- Export a Database:
./script.sh export_database example.com /path/to/backup.sql.gz
- Set Permissions for a Domain:
./script.sh setperm example.com
With these commands, you can efficiently manage your hosting environment on any *NIX server. This script simplifies the process of setting up, configuring, and maintaining your web hosting services.
Conclusion
Setting up your own hosting server can be a rewarding experience. With this comprehensive guide and the provided Bash script, you can easily manage your server, add or remove domains, and handle database operations with ease. The power of automation allows you to focus more on developing your websites and less on the intricacies of server management. Happy hosting!
Was this worth your time?


