-
Notifications
You must be signed in to change notification settings - Fork 0
/
install_wordpress.sh
169 lines (138 loc) · 6.67 KB
/
install_wordpress.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/bin/bash
# Function to display message
display_message() {
echo "=========================================="
echo "$1"
echo "=========================================="
}
# Function to generate a random string for passwords and db usernames
generate_random_string() {
length=$1
openssl rand -base64 $length | tr -dc 'a-zA-Z0-9' | fold -w $length | head -n 1
}
# Prompt for domain and convert domain name for DB name (replace '.' with '_')
read -p "Enter your domain (e.g., www.koderstory.com or subdomain.example.com): " domain
# Check if domain starts with "www." and determine root domain
if [[ $domain == www.* ]]; then
domain_without_www=$(echo "$domain" | sed 's/^www\.//')
server_names="$domain_without_www www.$domain_without_www"
else
# For other subdomains or domains without 'www', only register as it is
domain_without_www=$domain
server_names=$domain_without_www
fi
# Convert the domain to a valid DB name by replacing dots with underscores
db_name=$(echo "$domain_without_www" | tr '.' '_')
db_user="${db_name}_$(generate_random_string 8)"
db_password=$(generate_random_string 16)
# Prompt for PHP version, with default as PHP 7.4
read -p "Enter PHP version to install (e.g., 7.4, 8.0, 8.1, 8.2) [default is 7.4]: " php_version
php_version=${php_version:-7.4}
# Check if running as root
if [ "$EUID" -ne 0 ]; then
echo "Please run this script as root."
exit
fi
# Step 1: Install necessary libraries
display_message "Installing necessary libraries and dependencies..."
sudo apt update -y
sudo apt install -y nginx mariadb-server mariadb-client curl git zip unzip software-properties-common
# Add PHP PPA and install selected PHP version with extensions
display_message "Adding PHP PPA repository and installing PHP $php_version..."
sudo LC_ALL=C.UTF-8 add-apt-repository -y ppa:ondrej/php
sudo apt update -y
# Install PHP and required extensions, skipping php-json for PHP 8.x+
if [[ "$php_version" =~ ^8 ]]; then
sudo apt install -y php${php_version} php${php_version}-fpm php${php_version}-mysql php${php_version}-cli php${php_version}-curl php${php_version}-zip php${php_version}-xml php${php_version}-mbstring php${php_version}-gd php${php_version}-soap php${php_version}-intl php${php_version}-bcmath php${php_version}-xmlrpc
else
sudo apt install -y php${php_version} php${php_version}-fpm php${php_version}-mysql php${php_version}-cli php${php_version}-curl php${php_version}-zip php${php_version}-xml php${php_version}-mbstring php${php_version}-gd php${php_version}-soap php${php_version}-intl php${php_version}-bcmath php${php_version}-xmlrpc php${php_version}-json
fi
# Configure PHP settings for WordPress
display_message "Configuring PHP settings..."
sudo sed -i "s/upload_max_filesize = .*/upload_max_filesize = 64M/" /etc/php/${php_version}/fpm/php.ini
sudo sed -i "s/post_max_size = .*/post_max_size = 64M/" /etc/php/${php_version}/fpm/php.ini
sudo sed -i "s/memory_limit = .*/memory_limit = 256M/" /etc/php/${php_version}/fpm/php.ini
sudo sed -i "s/max_execution_time = .*/max_execution_time = 300/" /etc/php/${php_version}/fpm/php.ini
# Step 2: Set up MariaDB database for WordPress
display_message "Setting up MariaDB database and user..."
sudo systemctl start mariadb
sudo systemctl enable mariadb
# Secure MariaDB installation (optional)
display_message "Securing MariaDB installation..."
sudo mysql_secure_installation
# Check if the database already exists
db_exists=$(sudo mysql -e "SHOW DATABASES LIKE '${db_name}';" | grep "${db_name}")
if [ "$db_exists" ]; then
display_message "Database ${db_name} already exists."
read -p "Do you want to delete the existing database and create a new one? (y/n): " delete_db
if [ "$delete_db" == "y" ]; then
sudo mysql -e "DROP DATABASE ${db_name};"
sudo mysql -e "CREATE DATABASE ${db_name};"
else
display_message "Skipping database creation."
fi
else
sudo mysql -e "CREATE DATABASE ${db_name};"
fi
# Create or update the database user
sudo mysql -e "CREATE USER IF NOT EXISTS '${db_user}'@'localhost' IDENTIFIED BY '${db_password}';"
sudo mysql -e "GRANT ALL PRIVILEGES ON ${db_name}.* TO '${db_user}'@'localhost';"
sudo mysql -e "FLUSH PRIVILEGES;"
# Step 3: Download and configure WordPress
display_message "Downloading and configuring WordPress..."
sudo curl -O https://wordpress.org/latest.tar.gz
sudo tar -xzf latest.tar.gz
sudo mkdir -p /srv/$domain_without_www
sudo mv wordpress/* /srv/$domain_without_www
sudo chown -R www-data:www-data /srv/$domain_without_www
sudo chmod -R 755 /srv/$domain_without_www
# Create WordPress wp-config.php
sudo mv /srv/$domain_without_www/wp-config-sample.php /srv/$domain_without_www/wp-config.php
# Update wp-config.php with database information
sudo sed -i "s/database_name_here/${db_name}/" /srv/$domain_without_www/wp-config.php
sudo sed -i "s/username_here/${db_user}/" /srv/$domain_without_www/wp-config.php
sudo sed -i "s/password_here/${db_password}/" /srv/$domain_without_www/wp-config.php
# Step 4: Configure Nginx server block
display_message "Configuring Nginx for domain $server_names..."
sudo tee /etc/nginx/sites-available/$domain_without_www > /dev/null <<EOL
server {
listen 80;
server_name $server_names;
root /srv/$domain_without_www;
index index.php index.html index.htm;
location / {
try_files \$uri \$uri/ /index.php?\$args;
}
location ~ \.php\$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php${php_version}-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
EOL
# Enable Nginx site and reload configuration
sudo ln -s /etc/nginx/sites-available/$domain_without_www /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
# Step 5: Set up UFW Firewall
display_message "Configuring UFW firewall to allow Nginx traffic..."
sudo ufw allow 'OpenSSH'
sudo ufw allow 'Nginx Full'
sudo ufw enable
# Step 6: Install Certbot and obtain SSL certificate for the domain
display_message "Installing Certbot and generating SSL certificates using Let's Encrypt..."
# Install Certbot
sudo apt install certbot python3-certbot-nginx -y
# Obtain an SSL certificate with Certbot using your email
sudo certbot --nginx -d $domain_without_www -d www.$domain_without_www --non-interactive --agree-tos --email [email protected]
# Step 7: Reload Nginx to apply SSL
display_message "Reloading Nginx to apply SSL..."
sudo systemctl reload nginx
# Display the database credentials for WordPress
display_message "WordPress installed successfully!"
echo "Database Name: $db_name"
echo "Database User: $db_user"
echo "Database Password: $db_password"
echo "Please complete the WordPress installation by visiting: https://$domain_without_www or https://www.$domain_without_www"