Script to Install and Secure Apache2 PHP5 MySQL5 in Ubuntu/Debian

In this quick post will  show you how  to install fast en secure lamp in Ubuntu and Debian via this small script.





1- Create file; and add the following code


echo "+Updating apt-get..."
apt-get update

echo "+Installing SSH..."
apt-get install -q -y ssh

echo "+Changing default SSH port..."
sed -i 's/Port 22/Port 28/' /etc/ssh/sshd_config
/etc/init.d/ssh restart

echo "+Installing Apache2..."
apt-get install -q -y apache2
sed -i 's/ServerTokens Full/ServerTokens Prod/' /etc/apache2/conf.d/security
sed -i 's/ServerSignature On/ServerSignature Off/' /etc/apache2/conf.d/security
sed -i 's/TraceEnable On/TraceEnable Off/' /etc/apache2/conf.d/security
/etc/init.d/apache2 reload
a2enmod rewrite
/etc/init.d/apache2 restart

echo "+Installing PHP5..."
apt-get install -q -y php5
echo "+Securing PHP.ini file..."
sed -i 's/disable_functions =/disable_functions = show_source, system, exec/' /etc/php5/apache2/php.ini
sed -i 's/expose_php = On/expose_php = Off/' /etc/php5/apache2/php.ini
sed -i 's/display_errors = On/display_errors = Off/' /etc/php5/apache2/php.ini
sed -i 's/log_errors = Off/log_errors = On/' /etc/php5/apache2/php.ini
sed -i 's/allow_url_fopen = On/allow_url_fopen = Off/' /etc/php5/apache2/php.ini

echo "+Installing MySQL Server..."
DEBIAN_FRONTEND='noninteractive' apt-get install -q -y mysql-server
mysqladmin -u root password $MYSQL_PASS
apt-get install -q -y php5-mysql

history -c

echo "+Installation Completed!"

2- Make the script executable

Chmod +x

3- Run thescript

sudo ./

For questions, please post on ask unixmen. Thanks

  • Reviwan Putra

    I’ve try this tutorial, but i got some trouble, can you help me please..
    when i put the password;

    In order to log into MySQL to secure it, we’ll need the current
    password for the root user.  If you’ve just installed MySQL, and
    you haven’t set the root password yet, the password will be blank,
    so you should just press enter here.

    Enter current password for root (enter for none): <– MYSQL_PASS same with unix pass
    ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

    and i try without pass;
    Enter current password for root (enter for none):
    ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

    there is not effect,

    sorry for my bad english

  • Mohamed Khamlichi

     Hi  Reviwan.
    Please put  your  Mysql admin password  here :

    MYSQL_PASS=”YOUR_PASSWORD_HERE”;and restart the script again

  • Lichenghsu

    It seems like variable assignment without a semicolon works well, too.
    That is,
    works as

  • Steve Popoola

    Thanks for this! I have been looking for a script I can run each time I spin a fresh VM and I did not want to go through the same stuff every time.