Skip to content

Instantly share code, notes, and snippets.

@meigwilym
Last active August 16, 2023 19:51
Show Gist options
  • Save meigwilym/6438c110e3925ad1137a776edeb28d6a to your computer and use it in GitHub Desktop.
Save meigwilym/6438c110e3925ad1137a776edeb28d6a to your computer and use it in GitHub Desktop.
RedHat 7.3 setup Nginx, PHP 7.1, MySQL 5.7, Composer, Supervisor. For Laravel installations.
#!/bin/bash
# setup an environment for laravel on Red Hat 7.3
# Nginx
# PHP 7.1
# optional MySQL 5.7 or PostgreSQL 9.6
# Composer
# Supervisor
# check for root user
if [ "$(whoami)" != "root" ]; then
echo "You need to be root to perform this command."
exit 1
fi
# Enterprise Linux Extra Packages
yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm &> /dev/null
# red hat defaults
yum -y install vim wget unzip git &> /dev/null
# webserver
yum -y install nginx &> /dev/null
# Set The Nginx & PHP-FPM User
sed -i "s/user www-data;/user nginx;/" /etc/nginx/nginx.conf
sed -i "s/# server_names_hash_bucket_size.*/server_names_hash_bucket_size 64;/" /etc/nginx/nginx.conf
# sites-available in nginx.conf
sudo sed -i "s/include \/etc\/nginx\/conf\.d\/\*\.conf;/a \n include \/etc\/nginx\/sites-available/*;" /etc/nginx/nginx.conf
include \/etc\/nginx\/conf\.d\/\*\.conf;/a \n include \/etc\/nginx\/sites-available/\*;" /etc/nginx/nginx.conf
sudo sed -i "/include \/etc\/nginx\/conf\.d\/\*\.conf;/ a\ include /etc/nginx/sites-enabled/*;" /etc/nginx/nginx.conf
mkdir /etc/nginx/sites-enabled
mkdir /etc/nginx/sites-available
cp /etc/nginx/nginx.conf.default /etc/nginx/sites-available/example.conf
sed -i '1 i\ upstream php {\n server unix:/var/run/php-fpm/php-fpm.sock;\n server 127.0.0.1:9000 backup;\n}' /etc/nginx/sites-available/example.conf
systemctl start nginx
systemctl enable nginx
systemctl status nginx
# php
yum -y install http://rpms.remirepo.net/enterprise/remi-release-7.rpm &> /dev/null
yum -y install yum-utils &> /dev/null
yum-config-manager --enable remi-php71
yum -y install php71 &> /dev/null
yum -y install php71-php-mysql php71-php-xml php71-php-mbstring php71-php-imagick php71-php-fpm &> /dev/null
ln -s /usr/bin/php71 /usr/bin/php &> /dev/null
# Setup Some PHP-FPM Options
# directory to keep the socket file
mkdir /var/run/php-fpm
sed -i "s/upload_max_filesize = .*/upload_max_filesize = 100M/" /etc/opt/remi/php71/php.ini
sed -i "s/post_max_size = .*/post_max_size = 100M/" /etc/opt/remi/php71/php.ini
sed -i "s/user = apache/user = nginx/" /etc/opt/remi/php71/php-fpm.d/www.conf
sed -i "s/group = apache/group = nginx/" /etc/opt/remi/php71/php-fpm.d/www.conf
sed -i "s/;listen\.owner.*/listen.owner = nginx/" /etc/opt/remi/php71/php-fpm.d/www.conf
sed -i "s/;listen\.group.*/listen.group = nginx/" /etc/opt/remi/php71/php-fpm.d/www.conf
sed -i "s/;listen\.mode.*/listen.mode = 0666/" /etc/opt/remi/php71/php-fpm.d/www.conf
sed -i "s/listen = 127\.0\.0\.1:9000/listen = \/var\/run\/php-fpm\/php-fpm\.sock/" /etc/opt/remi/php71/php-fpm.d/www.conf
sed -i "/;clear_env = no/clear_env = no/" /etc/opt/remi/php71/php-fpm.d/www.conf
systemctl start php71-php-fpm
# composer
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
# supervisor
# https://rayed.com/wordpress/?p=1496
yum -y install python-setuptools &> /dev/null
easy_install pip
pip install supervisor
echo_supervisord_conf > supervisord.conf
mv supervisord.conf /etc/supervisord.conf
mkdir /etc/supervisord.d/
sed -i '$ a [include]\nfiles = /etc/supervisord.d/*.conf' /etc/supervisord.conf
# mysql 5.7
# https://www.if-not-true-then-false.com/2010/install-mysql-on-fedora-centos-red-hat-rhel/
echo Would you like to install MySQL 5.7? "(Y or N)"
read x
# now check if $x is "y"
if [ "$x" = "y" ] || [ "$x" = "Y" ]; then
wget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm &> /dev/null
rpm -ivh mysql57-community-release-el7-9.noarch.rpm &> /dev/null
rm -f mysql57-community-release-el7-9.noarch.rpm
yum -y install mysql-community-server &> /dev/null
# start mysql5.7
systemctl start mysqld.service
systemctl enable mysqld.service
echo "MySQL 5.7 has been installed and enabled"
# get the temporary password
string=$(grep 'A temporary password is generated for root@localhost' /var/log/mysqld.log |tail -1)
read -ra arr <<< "$string"
echo "Your temporary MySQL password is "
echo ${arr[-1]}
/usr/bin/mysql_secure_installation
fi
echo Would you like to install PostgreSQL? "(Y or N)"
read x
# now check if $x is "y"
if [ "$x" = "y" ] || [ "$x" = "Y" ]; then
# install postgres on Red Hat 7.3
yum install -y https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-redhat96-9.6-3.noarch.rpm &> /dev/null
yum install -y postgresql96-server postgresql96 &> /dev/null
/usr/pgsql-9.6/bin/postgresql96-setup initdb
# start and enable on system startup
systemctl start postgresql-9.6
systemctl enable postgresql-9.6
echo "postgresql 9.6 installed and enabled"
echo "to use, run this command:"
echo "sudo -u postgres psql"
fi
# all done?
echo ""
echo "Basic installation and configuration done. "
echo "next tasks are: "
echo "1. Use and rename the example conf file /etc/nginx/sites-available/example.conf"
echo "2a. create a MySQL database user : CREATE USER 'dbuser'@'127.0.0.1' IDENTIFIED BY '========='; GRANT ALL ON *.* TO 'dbuser'@'127.0.0.1';FLUSH PRIVILEGES;"
echo "2b. setup PostgreSQL sudo -u postgres psql"
echo "3. set up the supervisor init file with https://rayed.com/wordpress/?p=1496"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment