Skip to content

Instantly share code, notes, and snippets.

@atikju
Last active June 12, 2024 15:46
Show Gist options
  • Save atikju/1fb8d3e856e32f3b0a678d393914351b to your computer and use it in GitHub Desktop.
Save atikju/1fb8d3e856e32f3b0a678d393914351b to your computer and use it in GitHub Desktop.
NGINX, PHP, MYSQL installation with Amazon Linux 2023
Step 1: Let's start with NGINX installation
sudo dnf update //To Install Latest Update
sudo dnf install -y nginx // Install Nginx
sudo systemctl start nginx.service //Start Nginx Server
sudo systemctl status nginx.service // Check Server Status
sudo systemctl enable nginx.service // Enable Auto Server Start on Reboot
Now you should be able to see the server running by hitting your public ip.
Step 2: Let's install PHP Now
1. sudo dnf install php8.2 -y
2. php -v //shows the php version
To make sure you are using the correct memory, please tweak the memory_limit
inside php.ini. The location of this file is retrievable by echoing out
phpinfo();. But it's mostly available inside '/etc/php.ini'
(A handful doc - https://chrisshennan.com/blog/10-essential-phpini-tweaks-for-improved-web-performance)
Step 3: Let's install MYSQL Now
Download mysql. (If you don’t have the wget tool then get it using – sudo dnf install wget)
1.sudo wget https://dev.mysql.com/get/mysql80-community-release-el9-3.noarch.rpm
Once downloaded, run -
2. sudo dnf install mysql80-community-release-el9-3.noarch.rpm
Now, pack it with dnf by running -
3. sudo dnf update
Finally, you can now install mysql by running -
4. sudo dnf install mysql-community-server
5. sudo systemctl start mysqld
6. sudo systemctl enable mysqld
7. sudo systemctl status mysqld
>> Secure MYSQL now -
1. sudo grep 'temporary password' /var/log/mysqld.log //setup temp pass
2. sudo mysql_secure_installation -p //enter temp pass and reset. harden other security here.
3. mysql -u root -p //login to mysql with the newly set up pass.
>> If you can't connect to db with PHP, your mysqli driver is probably missing.
To install mysqli/pdo, use the following command -
1. yum -y install php-mysqlnd
Alright, Now we have completed installations of NGINX, PHP & MYSQL
Now let's set up our real domain to be served. Make sure your domain is
correctly pointed to the ec2 instance either by ip or load balancers
(A Record).
Now, let's set up the conf file---
1. Go inside cd /etc/nginx, you'll see the root nginx.conf file here.
2. You would go inside conf.d directory by hitting cd /etc/nginx.conf
3. Then create a new conf for your site - sudo nano mysite.conf
4. Copy and paste the contents below -
server {
listen 80;
server_name mysite.com;
root /var/www/html/mysite.com;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm/www.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
4.1 Note: refrain from putting #comments here as it sometimes doesn't
let you restart nginx.
4.2 Make sure you put the right directory tree for the php sock file.
If you are unsure about the directory, you see the php-fpm.conf file
here to know the directory tree.
5. Now you restart nginx by sudo systemctl restart nginx
Testing -
Hook an index.php file inside /var/www/html/mysite.com and load the domain
in your browser to see if this works.
Permissions -
You need to allow NGINX to control your web files. To change the
ownership of your html file, you can type -
1. sudo chown -R "$USER":www-data /var/www/html
If you are using Laravel, some of your directories might need to be open
to the web for it to write. i.e storages.
Run the command below. This will grant the storage access to 777
1. sudo chmod -R 777 /var/www/html/storage/*
As you run your applications, if you still encounter permissions issue,
you can replicate the previous command. The generic form of that is -
2. sudo chmod -R 777 /path/to/directory
Documents used:
1. https://cloudkatha.com/how-to-install-nginx-on-amazon-linux-2023-instance/
2. https://cloudkatha.com/how-to-install-php-8-1-on-amazon-linux-2023/
3. https://linux.how2shout.com/installing-mysql-8-server-client-on-amazon-linux-2023/
4. https://askubuntu.com/questions/9402/what-file-permissions-should-i-set-on-web-root
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment