Created
June 14, 2021 09:58
-
-
Save Sarfarazsajjad/90498a58ab489f8b2fee5e82d40cb45d to your computer and use it in GitHub Desktop.
Install Nginx and php on Amazon Linux 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
sudo yum update -y | |
sudo amazon-linux-extras install nginx1 php7.4 -y | |
sudo yum clean metadata | |
sudo yum install git mariadb-server php-{pear,cgi,common,curl,mbstring,gd,mysqlnd,gettext,bcmath,json,xml,fpm,intl,zip} -y | |
# Back up existing config | |
sudo cp -R /etc/nginx /etc/nginx-backup | |
sudo chmod -R 777 /var/log | |
sudo chown -R ec2-user:ec2-user /usr/share/nginx/html | |
echo "<?php phpinfo(); ?>" > /usr/share/nginx/html/index.php | |
sudo sed -i 's|;*user = nginx|user = nginx|g' /etc/php-fpm.d/www.conf | |
sudo sed -i 's|;*group = nginx|group = nginx|g' /etc/php-fpm.d/www.conf | |
sudo sed -i 's|;*pm = ondemand|pm = ondemand|g' /etc/php-fpm.d/www.conf | |
# configure php | |
sudo sed -i 's|;cgi.fix_pathinfo=1|cgi.fix_pathinfo=0|g' /etc/php.ini | |
sudo sed -i 's|;*expose_php=.*|expose_php=0|g' /etc/php.ini | |
#sudo sed -i 's|;*memory_limit = 128M|memory_limit = 512M|g' /etc/php.ini | |
sudo sed -i 's|;*post_max_size = 8M|post_max_size = 50M|g' /etc/php.ini | |
sudo sed -i 's|;*upload_max_filesize = 2M|upload_max_filesize = 10M|g' /etc/php.ini | |
sudo sed -i 's|;*max_file_uploads = 20|max_file_uploads = 20|g' /etc/php.ini | |
# nginx.conf | |
cat << EOF > /etc/nginx/nginx.conf | |
# For more information on configuration, see: | |
# * Official English Documentation: http://nginx.org/en/docs/ | |
# * Official Russian Documentation: http://nginx.org/ru/docs/ | |
user nginx; | |
worker_processes auto; | |
error_log /var/log/nginx/error.log; | |
pid /run/nginx.pid; | |
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic. | |
include /usr/share/nginx/modules/*.conf; | |
events { | |
worker_connections 1024; | |
} | |
http { | |
log_format main '\$remote_addr - \$remote_user [\$time_local] "\$request" ' | |
'\$status \$body_bytes_sent "\$http_referer" ' | |
'"\$http_user_agent" "\$http_x_forwarded_for"'; | |
access_log /var/log/nginx/access.log main; | |
sendfile on; | |
tcp_nopush on; | |
tcp_nodelay on; | |
keepalive_timeout 65; | |
types_hash_max_size 4096; | |
include /etc/nginx/mime.types; | |
default_type application/octet-stream; | |
# Load modular configuration files from the /etc/nginx/conf.d directory. | |
# See http://nginx.org/en/docs/ngx_core_module.html#include | |
# for more information. | |
include /etc/nginx/conf.d/*.conf; | |
server { | |
listen 80; | |
server_name _; | |
root /data/blog/public; | |
add_header X-Frame-Options "SAMEORIGIN"; | |
add_header X-XSS-Protection "1; mode=block"; | |
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; } | |
error_page 404 /index.php; | |
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; | |
} | |
} | |
} | |
EOF | |
for i in nginx php-fpm mariadb; do sudo systemctl enable $i --now; done | |
for i in nginx php-fpm mariadb; do sudo systemctl start $i; done | |
# lets encrypt | |
#sudo amazon-linux-extras install epel -y | |
#sudo yum install certbot certbox-nginx -y | |
#sudo systemctl restart nginx.service | |
# install Composer | |
if [[ "$HOME" == "" ]]; then | |
export COMPOSER_HOME=/root | |
fi | |
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" | |
sudo php composer-setup.php --quiet --install-dir=/usr/local/bin/ --filename=composer | |
rm -f composer-setup.php | |
# node | |
curl -sL https://rpm.nodesource.com/setup_10.x | bash - | |
sudo yum install -y nodejs | |
# clone the project | |
sudo mkdir /data | |
sudo chown -R ec2-user:ec2-user /data | |
cd /data | |
git clone https://github.com/ammlyf/blog.git | |
find /data/blog/storage -type d -exec chmod 777 {} \; | |
find /data/blog/bootstrap/cache -type d -exec chmod 777 {} \; | |
find /data -type f -exec chmod 0664 {} \; | |
# Install project dependencies | |
cd /data/change | |
composer install && npm install && npm run prod | |
cp .env.example .env | |
php artisan key:generate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment