Skip to content

Instantly share code, notes, and snippets.

@virbo
Last active August 16, 2023 19:55
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save virbo/71776c0a7f3c1442147eb9f5b4306af5 to your computer and use it in GitHub Desktop.
Save virbo/71776c0a7f3c1442147eb9f5b4306af5 to your computer and use it in GitHub Desktop.
Install Linux Centos 7, Nginx, MySQL, Postgres, PHP 8.0

Update LANG

Edit environtment vi /etc/environment add these lines...

LANG=en_US.utf-8
LC_ALL=en_US.utf-8

Update and upgrade Core

yum -y update
yum -y upgrade

Install NGINX

yum install epel-release
yum install nginx

Start NGINX and Enable Service

systemctl start nginx
systemctl enable nginx

Install PHP 8.0

Enable Remi Repo

yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

Check

yum --disablerepo="*" --enablerepo="remi-safe" list php[7-9][0-9].x86_64

Enable Remi php8.0

yum -y install yum-utils
yum-config-manager --enable remi-php80

Install PHP

yum -y install php php-fpm php-mysqlnd php-pgsql php-gd php-imagick php-json php-opcache php-mcrypt php-curl php-mongodb php-mbstring php-intl php-dom php-zip

Make sure the /var/lib/php directory has the correct ownership :

chown -R root:nginx /var/lib/php

Edit nano /etc/php-fpm.d/www.conf

user = nginx
group = nginx
listen = /var/run/php-fpm/php-fpm.sock
listen.owner = nginx
listen.group = nginx
listen.mode = 0660

Start and Enable php-fpm

systemctl start php-fpm
systemctl enable php-fpm

Create VirtualHost Default NGINX nano /etc/nginx/conf.d/default.conf

server {
    listen       80;
    server_name  server_domain_or_IP;

    root   /usr/share/nginx/html;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;

    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Check dan Restart Nginx

nginx -t
systemctl restart nginx

Testing PHP nano /usr/share/nginx/html/info.php

<?php
  echo phpinfo();
?>

Install Postgres (Latest Version)

rpm -Uvh https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
yum list postgresql*
yum -y install postgresql13-server postgresql13-contrib postgresql13

Init Postgres

/usr/pgsql-13/bin/postgresql-13-setup initdb

Start and Enable Postgres

systemctl start postgresql-13
systemctl enable postgresql-13

Edit pg_hba.conf

nano /var/lib/pgsql/13/data/pg_hba.conf

Replace ident to md5

host    all             all             127.0.0.1/32            md5
host    all             all             ::1/128                 md5

Create Database

su postgres
psql

CREATE DATABASE db_name;
CREATE ROLE username WITH PASSWORD 'password'
GRANT ALL PRIVILEGES ON DATABASE db_name TO username;
ALTER ROLE username WITH LOGIN;

Install MariaDB 10 (Centos 7 default using MariaDB 5)

First download and use the mariadb_repo_setup script to configure the MariaDB repositories for YUM

wget https://downloads.mariadb.com/MariaDB/mariadb_repo_setup
chmod +x mariadb_repo_setup
./mariadb_repo_setup

then

yum install mariadb-server

Configuring and Securing MariaDB Server

systemctl start mariadb
systemctl enable mariadb
sudo mysql_secure_installation

Testing the Installation

mysqladmin -u root -p version

indicates the installation has been successful like this

mysqladmin  Ver 9.1 Distrib 10.5.8-MariaDB, for Linux on x86_64
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Server version		10.5.8-MariaDB
Protocol version	10
Connection		Localhost via UNIX socket
UNIX socket		/var/lib/mysql/mysql.sock
Uptime:			2 hours 50 min 24 sec

Threads: 2  Questions: 690  Slow queries: 0  Opens: 100  Open tables: 94  Queries per second avg: 0.067

Install phpMyAdmin

Download latest version

cd /tmp
wget https://files.phpmyadmin.net/phpMyAdmin/5.0.4/phpMyAdmin-5.0.4-all-languages.zip
unzip phpMyAdmin-5.0.4-all-languages.zip
mv phpMyAdmin-5.0.4-all-languages /usr/share

Update config virtualhost

nano /etc/nginx/conf.d/default.conf

Add this line

location /phpMyAdmin {
    root /usr/share/;
    index index.php index.html index.htm;
    
    location ~ ^/phpMyAdmin/(.+\.php)$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
    
    location ~* ^/phpMyAdmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
        root /usr/share/;
    }
}

location /phpmyadmin {
    rewrite ^/* /phpMyAdmin last;
}

Reload nginx

nginx -s reload

Install CSF & LFD

dependency

yum -y install perl-libwww-perl.noarch perl-LWP-Protocol-https.noarch perl-GDGraph dns-utils

now install CSF

cd /usr/src
rm -fv csf.tgz
wget https://download.configserver.com/csf.tgz
tar -xzf csf.tgz
cd csf
sh install.sh

Disable Firewall

systemctl stop firewalld
systemctl disable firewalld

Install Let'sencrypt

yum install certbot-nginx

Solving problem error nginx nginx open() failed (13: Permission denied)

Check selinux, you must disable selinux for solving this error

setenforce 0

if still error, do it step below. Check permission

sudo -u username stat /home/username/public_html
sudo -u nginx stat /home/username/public_html

Add username to group nginx

gpasswd -a nginx username

Change permission

chmod g+x /home && chmod g+x /home/username/public_html

Upgrade NGINX (if version nginx installed lower than current version)

Create nginx repo /etc/yum.repos.d/nginx.repo

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

then

yum update

if error after upgrade (module "/usr/lib64/nginx/modules/ngx_http_image_filter_module.so" version 1016001 instead of 1018000 in /usr/share/nginx/modules/mod-http-image-filter.conf:1), do it this step

yum remove nginx-mod*
yum install nginx-module-*

Source

  1. https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-centos-7
  2. https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-centos-7
  3. https://linuxize.com/post/install-php-7-on-centos-7/
  4. https://www.woktron.com/secure/knowledgebase/77/Installation-CSF-Firewall-on-CentOS.html
  5. https://www.pusathosting.com/kb/linux/csf_error/warning_binary_location_for_host
  6. https://stackoverflow.com/questions/25774999/nginx-stat-failed-13-permission-denied
  7. https://stackoverflow.com/questions/19285355/nginx-403-error-directory-index-of-folder-is-forbidden
  8. https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-on-centos-7
  9. https://phoenixnap.com/kb/how-to-set-up-nginx-server-blocks-virtual-hosts-centos-7
  10. https://phppedia.com/en/knowledge-base/51254473/php-with-nginx---403-forbidden
  11. https://varunver.wordpress.com/2016/06/03/centos-7-install-php-and-postgres/
  12. https://stackoverflow.com/questions/43550336/nginx-failed-test-version-1010002-instead-of-1012000-in-usr-share-nginx-modules
  13. http://nginx.org/en/linux_packages.html#RHEL-CentOS
  14. https://www.prado.lt/5-minute-upgrade-nginx-1-12-to-1-17-on-centos-7-rhel-7
  15. https://mariadb.com/resources/blog/installing-mariadb-10-on-centos-7-rhel-7/
  16. https://www.digitalocean.com/community/tutorials/how-to-install-mariadb-on-centos-7
  17. https://linuxize.com/post/how-to-install-phpmyadmin-with-nginx-on-centos-7/
@unnellu
Copy link

unnellu commented Jan 24, 2022

mysql_secure_installation command not found
--->
mariadb-secure-installation

@virbo
Copy link
Author

virbo commented Jan 24, 2022

thanks @unnellu

@voedev
Copy link

voedev commented Aug 16, 2023

Thank you so much for the work you've done! Could you please tell me how to use multiple versions of php?

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