Install Wordpress with Apache, PHP and Mysql (Automated Startup Script)

edited September 2014 in Proposed How-Tos
You can copy and paste the following bash script into the Vultr Startup Script area:

It will install all of necessary packages to run Wordpress, generate and import database credentials, and open the necessary firewall ports.
#/bin/sh install_dir="/var/www/html" #Creating Random WP Database Credenitals db_name="wp`date +%s`" db_user=$db_name db_password=`date |md5sum |cut -c '1-12'` sleep 1 mysqlrootpass=`date |md5sum |cut -c '1-12'` sleep 1 ftp_password=`date |md5sum |cut -c '1-12'` #### Install Packages for https and mysql yum -y install httpd httpd-devel yum -y install mysql mysql-server mysql-devel yum -y install lynx vsftpd ##### Open firewall for http and SSL iptables -F iptables -A INPUT -i lo -j ACCEPT iptables -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT iptables -A INPUT -p tcp -m tcp --dport 443 -j ACCEPT /etc/init.d/iptables save /etc/init.d/iptables restart #### Start http /etc/init.d/httpd start chkconfig httpd on #### Start mysql and set root password /etc/init.d/mysqld start chkconfig mysqld on /usr/bin/mysql -e "USE mysql;" /usr/bin/mysql -e "UPDATE user SET Password=PASSWORD($mysqlrootpass) WHERE user='root';" /usr/bin/mysql -e "FLUSH PRIVILEGES;" touch /root/.my.cnf chmod 640 /root/.my.cnf echo "[client]">/root/.my.cnf echo "user=root">/root/.my.cnf echo "password="$mysqlrootpass>/root/.my.cnf ####Install PHP yum -y install php php-common php-mysql php-gd php-mbstring php-mcrypt php-xml php-devel sed -i '0,/AllowOverride\ None/! {0,/AllowOverride\ None/ s/AllowOverride\ None/AllowOverride\ All/}' /etc/httpd/conf/httpd.conf #Allow htaccess usage /etc/init.d/httpd restart ####Download and extract latest Wordpress Package if test -f /tmp/latest.tar.gz then echo "WP is already downloaded." else echo "Downloading Wordpress" cd /tmp/ && wget "" fi /bin/tar -C $install_dir -zxf /tmp/latest.tar.gz --strip-components=1 chown apache: $install_dir -R #### Set FTP Credentials echo $ftp_password | passwd apache --stdin /etc/init.d/vsftpd start chkconfig vsftpd on #### Create WP-config and set DB credentials /bin/mv $install_dir/wp-config-sample.php $install_dir/wp-config.php /bin/sed -i "s/database_name_here/$db_name/g" $install_dir/wp-config.php /bin/sed -i "s/username_here/$db_user/g" $install_dir/wp-config.php /bin/sed -i "s/password_here/$db_password/g" $install_dir/wp-config.php cat << EOF >> $install_dir/wp-config.php define('FS_METHOD', 'ftpext'); define('FTP_BASE', '$install_dir'); define('FTP_USER', 'apache'); define('FTP_PASS', '$ftp_password'); define('FTP_HOST', ''); define('FTP_SSL', false); EOF cat << EOF >> $install_dir/.htaccess # BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress EOF chown apache: $install_dir -R ##### Set WP Salts grep -A50 'table_prefix' $install_dir/wp-config.php > /tmp/wp-tmp-config /bin/sed -i '/**#@/,/$p/d' $install_dir/wp-config.php /usr/bin/lynx --dump -width 200 >> $install_dir/wp-config.php /bin/cat /tmp/wp-tmp-config >> $install_dir/wp-config.php && rm /tmp/wp-tmp-config -f /usr/bin/mysql -u root -e "CREATE DATABASE $db_name" /usr/bin/mysql -u root -e "GRANT ALL PRIVILEGES ON $db_name.* to '"$db_user"'@'localhost' IDENTIFIED BY '"$db_password"';" ######Display generated passwords to log file. echo "Database Name: " $db_name echo "Database User: " $db_user echo "Database Password: " $db_password echo "Mysql root password: " $mysqlrootpass echo "FTP Password: " $ftp_password

You will not need, but can retrieve, the database credentials by tailing the end of the /tmp/firstboot.log file after installation. It is also recommended to remove the /tmp/firstboot.log file upon installation. You will be able to access your installation at http://yourip


  • edited September 2014
    Based on the YUM-usage, I tried your script on CentOS 6.0. It worked like a treat up to the point where you need/want to install themes/plugins from the wp-admin. It comes up with FTP-credentials.

    Changing "chown -R nobody:nobody /var/www/" didn't solve the issue as it usually does on Ubuntu. I do like your script but somewhere I am missing a critical step to flawlessly work from the WP-Admin pages.

    Also, some admins (not specifically me) would like to have phpMyAdmin as well installed on their servers. I agree it might be a bigger attack-threat for people that want to probe a server but not everyone has got the knowledge to remotely run phpMyAdmin and connect to each of their instances elsewhere :)
  • Hi Marshall,

    I will go ahead and add support for uploading via the wp-admin area over the next couple of days. You are correct in my reasoning for not adding PMA by default. I will go ahead and get a version together with PMA included as well in a future release. Thank you for the feedback.
  • I forgot to mention, although a minor issue for most Linux-admins; they Permalink-settings from Wordpress don't seem to behave properly. Once you change from the default /?p=123 to /sample-post/ or any other format, the httpd.conf and .htaccess seem to be in the way.

    Unfortunately I am an oldschool/leisure admin and only do everything by trial & error without bash-scripts so I cannot imagine how/where you can correct this in your script :)
  • edited September 2014
    The above code has been updated to allow installation of themes, plugins, and media via the wp-admin area. It also now properly rewrites urls for any permalink settings.
  • > chown -R nobody:nobody /var/www/


    "Nobody" should be renamed 'to-all-intents-and-purposes-practically-anybody'
  • Just reinstalled my VPS with the script above and apart from 1 double-quote, it works perfectly and my small CentOS-instance was running in under 10 minutes.

    The wrong line: cd /tmp/ && wget "";
    The correct line: cd /tmp/ && wget "";

    Permalink/rewrites work properly as well.

    Got some ideas for future script-update; not posting them here (yet) to avoid hijacking your topic.
Sign In or Register to comment.