Skip to content

Instantly share code, notes, and snippets.

@linuxoracledev
Last active December 10, 2019 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linuxoracledev/39bb9b57b3f836de090a201689459a0f to your computer and use it in GitHub Desktop.
Save linuxoracledev/39bb9b57b3f836de090a201689459a0f to your computer and use it in GitHub Desktop.
How To Install LEMP stack(Linux, Nginx, MySQL, PHP) on Ubuntu 18.04
##Install NGINX
#Update the system
sudo apt update
#Install nginx
sudo apt install nginx
#Allow connections to Nginx on firewall
sudo ufw allow 'Nginx HTTP'
#verify the change
sudo ufw status
#
#Enable ufw firewall
#sudo ufw enable
#
##Install the MySQL Database server
sudo apt-get install mysql-server -y
#Configure MySQL Security
sudo mysql_secure_installation
#Check, Start, Enable MySql service to automatically start upon boot
sudo service mysql status
sudo systemctl start mysql
sudo systemctl enable mysql
#Chek MySql Version
sudo mysqladmin -p -u root version
###################Optional################################
#Open up the MySQL prompt
sudo mysql
#Check authentication method for database user
# SELECT user,authentication_string,plugin,host FROM mysql.user;
#Configure the root account to authenticate with a password
# ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
#Reload the grant tables and put changes into effect
FLUSH PRIVILEGES;
#After configuring root user to authenticate with a password, it is not possible to access MySQL with the sudo mysql command, Instead, use mysql -u root -p
exit
###########################################################################
##Installing PHP and Configuring Nginx to Use the PHP
# Install Ubuntu’s universe repository
sudo add-apt-repository universe
#Install fastCGI process manager- php-fpm along with php-mysql module
sudo apt install php-fpm php-mysql
#Check PHP Version
php --version
#List the contents for the directory /var/run/php/
ls /var/run/php/
#.sock file should be there this .sock file will be in location section in server block file
##server block By default located in /etc/nginx/sites-available/default. Open a new server block configuration file within the /etc/nginx/sites-available/ directory name the config file mylemp.com
sudo nano /etc/nginx/sites-available/mylemp.com
#Add the following content in mylemp.com file
#
#server {
# listen 80;
# root /var/www/html;
# index index.php index.html index.htm index.nginx-debian.html;
# server_name mylemp.com;
#
# location / {
# try_files $uri $uri/ =404;
# }
#
# location ~ \.php$ {
# include snippets/fastcgi-php.conf;
# fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
# }
#
# location ~ /\.ht {
# deny all;
# }
#}
#After adding this content, save and close the file
#Enable new server block file by creating a symbolic link from (/etc/nginx/sites-available/ directory) to (/etc/nginx/sites-enabled/)
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
#Unlink the default configuration file from the /sites-enabled/ directory
sudo unlink /etc/nginx/sites-enabled/default
sudo unlink /etc/nginx/sites-available/default
#Edit the file in nano.
#sudo nano /etc/nginx/sites-available/default
#Test your new configuration file for syntax errors
sudo nginx -t
#If any errors are reported, go back and recheck your file before continuing
#Reload Nginx to make the necessary changes
sudo systemctl reload nginx
#Test if PHP is working properly place a file called info.php inside nginx web server root directory (/var/www/html/)
sudo nano /var/www/html/info.php
#Enter the following content inside the file and save it
###############
# <?php #
# phpinfo(); #
# ?> #
###############
#Reload Nginx to make the necessary changes
sudo systemctl reload nginx
#Visit page: http://your_server_ip_address/info.php or http://localhost/info.php
#Uninstall NginX
sudo apt-get autoremove nginx
sudo apt-get --purge remove nginx
sudo apt-get autoremove && sudo apt-get autoclean
sudo find / | grep nginx | sudo xargs rm -rf
#the last command will remove the repository also so you've to add it again by:
sudo add-apt-repository ppa:nginx/stable
#Uninstall mysql server
sudo apt-get autoremove mysql-server
sudo apt-get --purge remove mysql-server
sudo apt-get autoremove && sudo apt-get autoclean
#Uninstall php
sudo apt-get autoremove php.*
sudo apt-get --purge remove php.*
sudo apt-get autoremove && sudo apt-get autoclean
@linuxoracledev
Copy link
Author

The LEMP software stack is a group of software that can be used to serve dynamic web pages and web applications. This is an acronym that describes a Linux operating system, with an Nginx (pronounced like “Engine-X”) web server. The backend data is stored in the MySQL database and the dynamic processing is handled by PHP.

@linuxoracledev
Copy link
Author

linuxoracledev commented Dec 9, 2019

In the server block configuration file
listen — Defines what port Nginx will listen on. In this case, it will listen on port 80, the default port for HTTP

root — Defines the document root where the files served by the website are stored

index — Configures Nginx to prioritize serving files named index.php when an index file is requested, if they’re available

server_name — Defines which server block should be used for a given request to your server. Point this directive to your server’s domain name or public IP address

location / — The first location block includes a try_files directive, which checks for the existence of files matching a URI request. If Nginx cannot find the appropriate file, it will return a 404 error

location ~ .php$ — This location block handles the actual PHP processing by pointing Nginx to the fastcgi-php.conf configuration file and the php7.2-fpm.sock file, which declares what socket is associated with php-fpm

location ~ /.ht — The last location block deals with .htaccess files, which Nginx does not process. By adding the deny all directive, if any .htaccess files happen to find their way into the document root they will not be served to visitors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment