Nginx on Linux:
Shell script to install nginx:
#!/bin/bash
sudo yum update -y
sudo amazon-linux-extras install nginx1 -y
sudo systemctl enable nginx
sudo systemctl start nginx
configure firewall:
sudo ufw allow OpenSSH
sudo ufw app list
sudo ufw allow ‘Nginx HTTP’
sudo ufw allow ‘Nginx Full’
sudo ufw enable
sudo ufw status
Ubuntu:
yum install epel-release -y
yum install nginx -y
export HOSTNAME=$(curl -s http://169.254.169.254/metadata/v1/hostname)
export PUBLIC_IPV4=$(curl -s http://169.254.169.254/metadata/v1/interfaces/public/0/ipv4/address)
echo Droplet: $HOSTNAME, IP Address: $PUBLIC_IPV4 > /usr/share/nginx/html/index.html
systemctl enable nginx
systemctl start nginx
chkconfig nginx on
iptables -A INPUT -p tcp –dport 80 -j ACCEPT
service iptables save
service iptables restart
mkdir /sites
chcon -Rt httpd_sys_content_t /sites/
vim /sites/index.html
Add the default index: This is new documentroot
vi /etc/nginx/conf.d/default.conf
Add this: root /myroot
service nginx restart
Install mysql
sudo apt install mysql-server
sudo mysql_secure_installation
This will ask if you want to configure the VALIDATE PASSWORD PLUGIN. Answer Y for yes, or anything else to continue without enabling. If you answer “yes”, you’ll be asked to select a level of password validation.
Your server will next ask you to select and confirm a password for the MySQL root user. Even though the default authentication method for the MySQL root user dispenses the use of a password, even when one is set, you should define a strong password here as an additional safety measure.
For the rest of the questions, press Y and hit the ENTER key at each prompt.
Remove anonymous users? – y
install php
sudo add-apt-repository universe
sudo apt update && sudo apt install php-fpm php-mysql
or
apt-get install php7.4 -y
or
apt-get install php7.4-fpm php7.4-cli php7.4-mysql php7.4-curl php7.4-json -y
Configure Nginx for PHP
We now need to make some changes to our Nginx server block.
The location of the server block may vary depending on your setup. By default, it is located in /etc/nginx/sites-available/default.
Edit the file in nano.
sudo nano /etc/nginx/sites-available/default
7.1. Prioritize index.php
Press CTRL + W and search for index.html.
Now add index.php before index.html
/etc/nginx/sites-available/default
index index.php index.html index.htm index.nginx-debian.html;
7.2. Server Name
Press CTRL + W and search for the line server_name.
Enter your server’s IP here or domain name if you have one.
/etc/nginx/sites-available/default
server_name YOUR_DOMAIN_OR_IP_HERE;
7.3. PHP Socket
Press CTRL + W and search for the line location ~ \.php.
You will need to uncomment some lines here by removing the # signs before the four lines marked in red below.
Also ensure value for fastcgi_pass socket path is correct. For example, if you installed PHP version 7.4, the socket should be: /var/run/php/php7.4-fpm.sock
If you are unsure which socket to use here, exit out of nano and run ls /var/run/php/
/etc/nginx/sites-available/default
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}
Make sure to remove the # sign before the closing bracket } in red above.
7.4. Save and Test
Once you’ve made the necessary changes, save and close (Press CTRL + X, then press y and ENTER to confirm save)
Now check the config file to make sure there are no syntax errors.
sudo nginx -t
Output:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
If no errors, you can reload the Nginx config.
sudo service nginx reload
configure Nginx to process PHP files.
To do so, create a new Nginx virtual host configuration file with the following command:
vi /etc/nginx/sites-available/example
Add the following lines:
server {
listen 80;
server_name test.example.com;
root /var/www/html;
index info.php;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
}
Save and close the file then enable the Nginx virtual host configuration file with the following command:
ln -s /etc/nginx/sites-available/example /etc/nginx/sites-enabled/
Next, verify the Nginx for any syntax error with the following command:
nginx -t
You should get the following output:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Finally, restart the Nginx service to apply the configuration changes:
systemctl restart nginx
Next, create a info.php file to verify the PHP version:
nano /var/www/html/info.php
test it.
How To Install an FTP server (vsftpd) on Ubuntu 20.04/20.10
sudo apt update && sudo apt install vsftpd
sudo service vsftpd status
sudo ufw allow 20/tcp
sudo ufw allow 21/tcp
sudo ufw allow 40000:50000/tcp
sudo ufw allow 990/tcp
Now, enable the firewall if it isn’t already. Press y and ENTER if warned about disrupting the SSH connection.
sudo ufw enable
To check the status of the firewall, run:
sudo ufw status
Adding domain:
Step 1: Create the website folder and a public folder to put the static assets inside /var/www
mkdir /var/www/example-one.com/public_html
chown -R www-data: /var/www/example-one.com
vi /var/www/example-one.com/public_html/index.html
<!DOCTYPE html>
<html lang=”en” dir=”ltr”>
<head>
<meta charset=”utf-8″>
<title>Welcome to example-one.com</title>
</head>
<body>
<h1>Welcome To example-one.com home page!</h1>
</body>
</html>
vi /etc/nginx/sites-available/example-one.com.conf
server {
listen 80;
listen [::]:80;
root /var/www/example-one.com/public_html;
index index.html;
server_name example-one.com www.example-one.com;
access_log /var/log/nginx/example-one.com.access.log;
error_log /var/log/nginx/example-one.com.error.log;
location / {
try_files $uri $uri/ =404;
}
}
execute the following command to create a symbolic link to sites-enabled folder
sudo ln -s /etc/nginx/sites-available/example-one.com.conf /etc/nginx/sites-enabled/example-one.com.conf
sudo nginx -t
sudo systemctl restart nginx
If you use the public DNS of the ec2 instance , the server will throw the following error when you restart the Nginx server.
nginx: [emerg] could not build the server_names_hash, you should increase server_names_hash_bucket_size: 64
Since the DNS name is pretty long , you have to add the server name bucket size to 128 to the example-one.conf file.
server_names_hash_bucket_size 128;