Alibaba cloud CentOS 7.4 configures Nginx, PHP and Mariadb

Keywords: MariaDB Nginx PHP yum

CentOS 7.4 basic environment configuration

Add third party source yum

There are two common third-party sources: EPEL and IUS. You can see the latest installation methods at https://ius.io/GettingStarted/

Automatic installation

curl -L https://setup.ius.io | sh

Manual installation (if automatic installation is used, this will be omitted)

$ wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
$ wget https://centos7.iuscommunity.org/ius-release.rpm

$ rpm -ivh epel-release-latest-7.noarch.rpm
$ rpm -ivh ius-release.rpm

Update yum source cache after installation

$ yum clean all
$ yum makecache

Installing the SDK

$ yum -y groupinstall "Development Tools"

Upgrade system package

$ yum -y upgrade

Install zsh and autojump (optional)

//Before installing zsh, check whether git is installed, if not
//$ yum install git
$ yum install -y zsh
$ curl -L http://install.ohmyz.sh | sh
$ chsh -s /bin/zsh //Change shell to zsh
$ yum install -y autojump
$ yum install -y autojump-zsh

Modify the. zshrc file to set the zsh plug-in and style

#style
ZSH_THEME="ys"perhaps"af-magic"
#Plug-in unit
plugins=(git extract sudo autojump)
#Handle command line no match error
setopt no_nomatch

Install jemalloc

$ yum -y install jemalloc

Install redis

$ yum -y install redis

Start the redis service and set it to start automatically

$ systemctl enable redis
$ systemctl start redis

Installing Mariadb 5.5.56

Install mariadb

$ yum -y install mariadb mariadb-server

Move mariadb's data directory to a custom location

Create mariadb data directory

# -The p parameter can automatically generate the full path. For example, if the / data directory does not exist above, it will be created automatically
mkdir -p /data/mariadb

#Modify directory owner
chown -R mysql:mysql /data/mariadb

Configure mariadb

#Back up my.cnf generated by MariaDB LIBS
mv /etc/my.cnf /etc/my.cnf.libs.back
#Copying configuration files from mariadb
cp /usr/share/mysql/my-large.cnf /etc/my.cnf

Modify / etc/my.cnf

[mysqld]
#Set data directory
datadir=/data/mariadb

#Add character set setting utf8
# setting character set
init_connect = 'SET NAMES utf8'
character-set-server = utf8
collation-server = utf8_unicode_ci
skip-character-set-client-handshake

#Modify thread "concurrency to the number of CPU s * 2
thread_concurrency = 2

#Add mysqld_safe ty setting
[mysqld_safe]
log-error = /var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
#Add jemalloc support
malloc-lib=/usr/lib64/libjemalloc.so.1

#Add include configuration directory at the end
!includedir /etc/my.cnf.d

Starting mariadb

$ systemctl start mariadb

Set the startup of mariadb service

$ systemctl enable mariadb

Initialize mariadb

#Modify MySQL? Secure? Installation, otherwise the problem of unable to find the sock file will occur
#Find the Maike ﹣ config function and add it below the sentence with [mysql]
echo "socket=/data/mariadb/mysql.sock" << $config
#Save exit

$ mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

Install Nginx

$ yum -y install nginx
#Set startup
$ systemctl enable nginx

Modify the system CTL startup settings to avoid the problem of unable to find the pid file

$ mkdir -p /etc/systemd/system/nginx.service.d
$ printf "[Service]\nExecStartPost=/bin/sleep 0.1\n" > /etc/systemd/system/nginx.service.d/override.conf
$ systemctl daemon-reload 

Start nginx

$ systemctl start nginx

Install PHP 7.1

$ yum -y install php71u-fpm php71u-fpm-nginx
$ yum -y install php71u-mbstring php71u-common php71u-gd php71u-mcrypt
$ yum -y install php71u-mysql php71u-xml php71u-cli php71u-devel
$ yum -y install php71u-pecl-redis php71u-opcache

Modify / etc/nginx/conf.d/php-fpm.conf

upstream php-fpm {
        #server 127.0.0.1:9000;
        server unix:/run/php-fpm/www.sock;
}

Modify / etc / PHP fpm.d/www.conf

#Modifying PHP FPM process users
user = nginx
group = nginx

#Modify network mode
;listen = 127.0.0.1:9000
listen = /run/php-fpm/www.sock

#Modify the user who generated the sock
listen.owner = nginx
listen.group = nginx
listen.mode = 0666

Start PHP FPM

$ systemctl enable php-fpm
$ systemctl start php-fpm

Configure nginx virtual host

Set up virtual host

# Add virtual host configuration directory
$ mkdir -p /etc/nginx/vhosts.d

Modify nginx configuration file / etc/nginx/nginx.conf

# Add after server configuration
include /etc/nginx/vhosts.d/*.conf;

Create the aaa.conf file in the directory / etc/nginx/vhosts.d, assuming that www.aaa.com is the domain name

server {
    listen       80;
    server_name  www.aaa.com;
    
    access_log  /data/aaa/logs/access.log main;
    error_log   /data/aaa/logs/error.log;

    charset utf-8;

    location / {
        root   /data/aaa/www;
        index  index.php index.html index.htm;
    }

    # pass the PHP scripts to FastCGI server listening on Unix socket
    
    location ~ \.php$ {
         root           /data/aaa/www;
         fastcgi_pass   php-fpm;
         fastcgi_index  index.php;
         fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
         include        fastcgi_params;
    }

    error_page 404 /404.html;
    location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    
    location ~ /\.ht {
        deny  all;
    }
}

Create the web page storage directory and log directory of virtual host

$ mkdir -p /data/aaa/{logs,www}

Create a test file

echo "<?php phpinfo();" > /data/aaa/www/index.php

Restart nginx and PHP FPM

$ systemctl restart nginx
$ systemctl restart php-fpm

Visit the domain name with a browser to see if you can see the phpinfo page

Posted by n1tr0b on Sun, 03 May 2020 02:57:00 -0700