Created
July 26, 2023 03:24
-
-
Save md-riaz/1f200299cc9d90086c1ec8293bf86f33 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Function to create the website directory and files | |
function create_website() { | |
local domain_name=$1 | |
# Check if the domain name is provided | |
if [ -z "$domain_name" ]; then | |
echo "Please provide a domain name." | |
exit 1 | |
fi | |
# Create the website directory | |
if [ ! -d "$domain_name" ]; then | |
mkdir "$domain_name" | |
else | |
echo "The directory $domain_name already exists." | |
exit 1 | |
fi | |
# Create the index.php file | |
cat << EOF > "$domain_name/index.php" | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Welcome to $domain_name</title> | |
</head> | |
<body> | |
<h1>Welcome to $domain_name</h1> | |
<p>This is the default page for $domain_name.</p> | |
</body> | |
</html> | |
EOF | |
echo "Website for $domain_name has been created successfully." | |
} | |
# Function to enable PHP-FPM for the website | |
function enable_php() { | |
local domain_name=$1 | |
local php_version=$(php -v | head -n 1 | cut -d " " -f 2 | cut -d "." -f 1,2) | |
sudo apt update | |
sudo apt install -y php$php_version-fpm php$php_version-mysql | |
# Enable PHP-FPM for the website | |
sudo cp /etc/php/$php_version/fpm/pool.d/www.conf /etc/php/$php_version/fpm/pool.d/$domain_name.conf | |
sudo sed -i "s/www/$domain_name/g" /etc/php/$php_version/fpm/pool.d/$domain_name.conf | |
sudo service php$php_version-fpm restart | |
} | |
# Function to create Nginx virtual host configuration for the website | |
function create_nginx_virtual_host() { | |
local domain_name=$1 | |
local website_directory=$(readlink -f "$domain_name") | |
# Create Nginx virtual host configuration | |
cat << EOF | sudo tee "/etc/nginx/sites-available/$domain_name" > /dev/null | |
server { | |
listen 80; | |
server_name $domain_name; | |
root $website_directory; | |
index index.php index.html; | |
location / { | |
try_files \$uri \$uri/ /index.php?\$args; | |
} | |
location ~ \.php\$ { | |
include snippets/fastcgi-php.conf; | |
fastcgi_pass unix:/run/php/php$(php -v | head -n 1 | cut -d " " -f 2 | cut -d "." -f 1,2)-fpm.sock; | |
} | |
location ~ /\.ht { | |
deny all; | |
} | |
} | |
EOF | |
} | |
# Function to enable the Nginx virtual host | |
function enable_nginx_virtual_host() { | |
local domain_name=$1 | |
# Enable Nginx virtual host | |
sudo ln -s "/etc/nginx/sites-available/$domain_name" "/etc/nginx/sites-enabled/" | |
sudo service nginx restart | |
} | |
# Prompt the user for the domain name | |
read -p "Enter the domain name for the website: " domain_name | |
# Create the website | |
create_website "$domain_name" | |
# Enable PHP-FPM for the website | |
enable_php "$domain_name" | |
# Create Nginx virtual host configuration | |
create_nginx_virtual_host "$domain_name" | |
# Enable the Nginx virtual host | |
enable_nginx_virtual_host "$domain_name" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment