How To Install LAMP Stack On Ubuntu 16.04
LAMP is a combination of operating system and open-source software stack. The acronym LAMP came from the first letters ofLinux,Apache HTTP Server,Mysql or MariaDB database,and PHP/Perl/Python. This tutorial describes how to install LAMP stack on Ubuntu 16.04,however the steps described below should work on prevIoUs Ubuntu versions such as Ubuntu 15.10/15.04/14.10/14.04/13.10 etc.
Install LAMP Stack On Ubuntu 16.04
1. Install Apache
Apache is an open-source multi-platform web server. It provides a full range of web server features including CGI,SSL and virtual domains.
To install Apache,enter:
sudoapt-getinstallapache2
Enable and start your apache
root@ubuntu:~#systemctlenableapache2 root@ubuntu:~#systemctlstartapache2 root@ubuntu:~#systemctlstatusapache2 ●apache2.service-LSB:Apache2webserver Loaded:loaded(/etc/init.d/apache2;bad;vendorpreset:enabled) Active:active(running)sinceWed2016-03-0209:23:37PST;1min2sago Docs:man:systemd-sysv-generator(8) CGroup:/system.slice/apache2.service ├─22328/usr/sbin/apache2-kstart ├─22331/usr/sbin/apache2-kstart └─22332/usr/sbin/apache2-kstart Mar0209:23:36ubuntusystemd[1]:StartingLSB:Apache2webserver... Mar0209:23:36ubuntuapache2[22304]:*Startingwebserverapache2 Mar0209:23:36ubuntuapache2[22304]:AH00558:apache2:Couldnotreliablydeterminetheserver'sfullyqualifieddomainname,using127.0.1.1.Setthe'S Mar0209:23:37ubuntuapache2[22304]:* Mar0209:23:37ubuntusystemd[1]:StartedLSB:Apache2webserver. Mar0209:24:34ubuntusystemd[1]:StartedLSB:Apache2webserver.
Test Apache:
Open your web browser and navigate to http://localhost/ or http://server-ip-address/.
2. Install MysqL
MysqL is a relational database management system (RDBMS) that runs as a server providing multi-user access to a number of databases,though sqlite probably has more total embedded deployments
sudoapt-getinstallMysqL-serverMysqL-client
During installation,you’ll be asked to setup the MysqL “root” user password. Enter the password and click Ok.
Re-enter the password.
MysqL is installed now.
You can verify the MysqL server status using command:
On Ubuntu 16.04/15.10/15.04:
sudosystemctlstatusMysqL
On Ubuntu 14.10 and prevIoUs versions:
sudoserviceMysqLstatus
Sample output:
●MysqL.service-MysqLCommunityServer Loaded:loaded(/lib/systemd/system/MysqL.service;enabled;vendorpreset:enabled) Active:active(running)sinceMon2015-10-2614:23:01IST;28sago MainPID:3577(MysqLd_safe) CGroup:/system.slice/MysqL.service ├─3577/bin/sh/usr/bin/MysqLd_safe └─3924/usr/sbin/MysqLd--basedir=/usr--datadir=/var/lib/MysqL--... Oct2614:23:00serversystemd[1]:StartingMysqLCommunityServer... Oct2614:23:00serverMysqLd_safe[3577]:15102614:23:00MysqLd_safeCan't.... Oct2614:23:00serverMysqLd_safe[3577]:15102614:23:00MysqLd_safeLoggin.... Oct2614:23:01serverMysqLd_safe[3577]:15102614:23:01MysqLd_safeStarti...l Oct2614:23:01serversystemd[1]:StartedMysqLCommunityServer. Hint:Somelineswereellipsized,use-ltoshowinfull.
3. Install MariaDB
In case you want to use MariaDB instead of MysqL community edition,follow the steps given below.
MariaDB is a drop in replacement for MysqL. It is a robust,scalable and reliable sql server that comes rich set of enhancements.
First you have to remove existing MysqL packages if any. To completely uninstall MysqL along with its configuration files,enter the following commands one by one:
sudosystemctlstopMysqL
sudoapt-getremove--purgeMysqL-serverMysqL-clientMysqL-common
sudoapt-getautoremove
sudoapt-getautoclean
sudorm-rf/var/lib/MysqL/
sudorm-rf/etc/MysqL/
After removing MysqL,run the following command to install MariaDB.
sudoapt-getinstallmariadb-server
Alternatively,you can install it using MariaDB repository if you want to try most recent version of MariaDB. Run the following commands to add PPA. As of writing this,MariaDB PPA is not yet updated to Ubuntu 16.04. However,we can use the repository of Ubuntu 15.10 instead.
sudoapt-getinstallsoftware-properties-common
sudoapt-keyadv--recv-keys--keyserverhkp://keyserver.ubuntu.com:800xcbcb082a1bb943db
sudoadd-apt-repository'debhttp://download.nus.edu.sg/mirror/mariadb/repo/10.1/ubuntuvividmain'
Update the software sources list and install MariaDB using following commands:
sudoapt-getupdate
sudoapt-getinstallmariadb-server
During installation you will be asked to set MysqL ‘root’ user password. Enter the password twice,and complete the installation.
Important Note: In Ubuntu 16.04/15.10/15.04,MariaDB won’t ask you to set root user password during installation.
Also,you can’t set password manually using the following command too:
MysqL_secure_installation
It will throw the following error.
NOTE:RUNNINGALLPARTSOFTHISSCRIPTISRECOMMENDEDFORALLMariaDB SERVERSINPRODUCTIONUSE!PLEASEREADEACHSTEPCAREFULLY! InordertologintoMariaDBtosecureit,we'llneedthecurrent passwordfortherootuser.Ifyou'vejustinstalledMariaDB,and youhaven'tsettherootpasswordyet,thepasswordwillbeblank,soyoushouldjustpressenterhere. Entercurrentpasswordforroot(enterfornone): ERROR1698(28000):Accessdeniedforuser'root'@'localhost'Entercurrentpasswordforroot(enterfornone):
To fix this error,log in to MysqL prompt without password as root user:
sudoMysqL-uroot
Then,run the following commands one by one in the MysqL prompt:
useMysqL;
updateusersetplugin=''whereUser='root';
flushprivileges;
\q
That’s it. Now,set database administrative “root” user password using command. Press Enter and go with the default settings.
MysqL_secure_installation
Sample output:
NOTE:RUNNINGALLPARTSOFTHISSCRIPTISRECOMMENDEDFORALLMariaDB SERVERSINPRODUCTIONUSE!PLEASEREADEACHSTEPCAREFULLY! InordertologintoMariaDBtosecureit,soyoushouldjustpressenterhere. Entercurrentpasswordforroot(enterfornone): OK,successfullyusedpassword,movingon... SettingtherootpasswordensuresthatnobodycanlogintotheMariaDB rootuserwithouttheproperauthorisation. Setrootpassword?[Y/n]y Newpassword: Re-enternewpassword: Passwordupdatedsuccessfully! Reloadingprivilegetables.. ...Success! Bydefault,aMariaDBinstallationhasananonymoususer,allowinganyone tologintoMariaDBwithouthavingtohaveauseraccountcreatedfor them.Thisisintendedonlyfortesting,andtomaketheinstallation goabitsmoother.Youshouldremovethembeforemovingintoa productionenvironment. Removeanonymoususers?[Y/n] ...Success! Normally,rootshouldonlybeallowedtoconnectfrom'localhost'.This ensuresthatsomeonecannotguessattherootpasswordfromthenetwork. Disallowrootloginremotely?[Y/n] ...Success! Bydefault,MariaDBcomeswithadatabasenamed'test'thatanyonecan access.Thisisalsointendedonlyfortesting,andshouldberemoved beforemovingintoaproductionenvironment. Removetestdatabaseandaccesstoit?[Y/n] -Droppingtestdatabase... ERROR1008(HY000)atline1:Can'tdropdatabase'test';databasedoesn'texist ...Failed!Notcritical,keepmoving... -Removingprivilegesontestdatabase... ...Success! Reloadingtheprivilegetableswillensurethatallchangesmadesofar willtakeeffectimmediately. Reloadprivilegetablesnow?[Y/n] ...Success! Cleaningup... Alldone!Ifyou'vecompletedalloftheabovesteps,yourMariaDB installationshouldnowbesecure. ThanksforusingMariaDB!
Check if mariadb is running or not,using the following command:
sudoserviceMysqLstatus
Sample output:
●MysqL.service-LSB:StartandstoptheMysqLdatabaseserverdaemonLoaded:loaded(/etc/init.d/MysqL)Active:active(running)sinceMon2015-10-2614:26:36IST;2min5sago Docs:man:systemd-sysv-generator(8) CGroup:/system.slice/MysqL.service ├─6387/bin/bash/usr/bin/MysqLd_safe ├─6388logger-pdaemonerr-t/etc/init.d/MysqL-i └─6544/usr/sbin/MysqLd--basedir=/usr--datadir=/var/lib/MysqL--... Oct2614:26:39server/etc/MysqL/debian-start[6595]:MysqL.time_zone_transit... Oct2614:26:39server/etc/MysqL/debian-start[6595]:MysqL.time_zone_transit... Oct2614:26:39server/etc/MysqL/debian-start[6595]:MysqL.user... Oct2614:26:39server/etc/MysqL/debian-start[6595]:Phase2/6:Fixingviews Oct2614:26:39server/etc/MysqL/debian-start[6595]:Processingdatabases Oct2614:26:39server/etc/MysqL/debian-start[6595]:information_schema Oct2614:26:39server/etc/MysqL/debian-start[6595]:MysqL Oct2614:26:39server/etc/MysqL/debian-start[6595]:performance_schema Oct2614:26:39server/etc/MysqL/debian-start[6653]:Checkingforinsecurer... Oct2614:26:39server/etc/MysqL/debian-start[6657]:Triggeringmyisam-recov... Hint:Somelineswereellipsized,use-ltoshowinfull.
4. Install PHP (PHP7)
PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely used open-source general purpose scripting language that is especially suited for web development and can be embedded into HTML.
Install PHP with following command:
sudoapt-getupdate sudoapt-getinstallPHP7.0-MysqLPHP7.0-curlPHP7.0-jsonPHP7.0-cgiPHP7.0libapache2-mod-PHP7
Test your PHP version
root@ubuntu:~#PHP-v PHP7.0.3-3(cli)(NTS) Copyright(c)1997-2016ThePHPGroup ZendEnginev3.0.0,Copyright(c)1998-2016ZendTechnologies withZendOPcachev7.0.6-dev,Copyright(c)1999-2016,byZendTechnologies root@ubuntu:~#
To test PHP,create a sample “testPHP.PHP” file in Apache document root folder.
sudovi/var/www/html/testPHP.PHP
Add the following lines:
<?PHP PHPinfo(); ?>
Restart apache2 service.
On Ubuntu 15.10/15.04:
sudosystemctlrestartapache2
On Ubuntu 14.10 and lower versions:
sudoserviceapache2restart
Navigate to http://server-ip-address/testPHP.PHP. It will display all the details about PHP such as version,build date and commands etc.
PHP7.03" height="851" width="1200" src="http://www.unixmen.com/wp-content/uploads/2016/03/PHP7.03.png">
If you want to install all PHP modules at once,enter the command sudo apt-get install PHP* and restart the apache2 service. To verify the modules,open web browser and navigate to http://server-ip-address/testPHP.PHP. You will able to see all installed PHP modules.
5. Manage MysqL Databases (Optional)
Install PHPMyAdmin
PHPMyAdmin is a free open-source web interface tool used to manage your MysqL databases. It is available in the Official Debian repositories. So install it with command:
sudoapt-getinstallPHPmyadmin
Select the Web server that should be automatically configured to run PHPMyAdmin. In my case,it is apache2.
The PHPMyAdmin must have a database installed and configured before it can be used. This can be optionally handled by dbconfig-common.
Select ‘Yes’ to configure database for PHPmyadmin wjth dbconfig-common.
Enter password of the database’s administrative user.
Enter MysqL application password for PHPmyadmin:
Re-enter password:
Success! PHPMyAdmin installation is installed.
Additional Note: if you followed all steps carefully,PHPMyAdmin should work just fine. In case PHPMyAdmin is not working,please do the following steps.
Open terminal,and type:
sudonano/etc/apache2/apache2.conf
Add the following line at the end.
Include/etc/PHPmyadmin/apache.conf
Save and Exit. Restart apache service:
On Ubuntu 16.04/15.10/15.04:
sudosystemctlrestartapache2
On Ubuntu 14.10 and lower versions:
sudo/etc/init.d/apache2restart
6. Access PHPMyAdmin Web Console
Now,you can access the PHPmyadmin console by navigating to http://server-ip-address/PHPmyadmin/ from your browser.
Enter your MysqL username and password which you have given in prevIoUs steps. In my case its “root” and “ubuntu”.
You will be redirected to PHPMyAdmin main web interface.
From here,you can manage your MysqL databases from PHPMyAdmin web interface.
That’s it. Your LAMP stack is ready to use.