Install NginX CentOS 7

Add Nginx Repository

To add the CentOS 7 EPEL repository, open terminal and use the following command:

sudo yum install epel-release

Install Nginx

Now that the Nginx repository is installed on your server, install Nginx using the following yum command:

sudo yum install nginx -y

Start Nginx

Nginx does not start on its own. To get Nginx running, type:

sudo systemctl start nginx

If you are running a firewall, run the following commands to allow HTTP and HTTPS traffic:

sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --reload

You can do a spot check right away to verify that everything went as planned by visiting your server’s public IP address in your web browser (see the note under the next heading to find out what your public IP address is if you do not have this information already):

http://server_domain_name_or_IP/

Before continuing, you will probably want to enable Nginx to start when your system boots. To do so, enter the following command:

sudo systemctl enable nginx

Magento 2 Enable Redis

Install Redis.

And then edit app/etc/env.php to configure Magento to use Redis for session storage, default and page caching.

'session' =>
array (
'save' => 'redis',
'redis' =>
array (
'host' => '127.0.0.1',
'port' => '6379',
'password' => '',
'timeout' => '2.5',
'persistent_identifier' => '',
'database' => '2',
'compression_threshold' => '2048',
'compression_library' => 'gzip',
'log_level' => '1',
'max_concurrency' => '6',
'break_after_frontend' => '5',
'break_after_adminhtml' => '30',
'first_lifetime' => '600',
'bot_first_lifetime' => '60',
'bot_lifetime' => '7200',
'disable_locking' => '0',
'min_lifetime' => '60',
'max_lifetime' => '2592000'
)
),
'cache' =>
array(
'frontend' =>
array(
'default' =>
array(
'backend' => 'Cm_Cache_Backend_Redis',
'backend_options' =>
array(
'server' => '127.0.0.1',
'database' => '0',
'port' => '6379'
),
),
'page_cache' =>
array(
'backend' => 'Cm_Cache_Backend_Redis',
'backend_options' =>
array(
'server' => '127.0.0.1',
'port' => '6379',
'database' => '1',
'compress_data' => '0'
)
)
)
),

Install Redis CentOS 7 Linux

Run the following commands to install redis

wget http://download.redis.io/redis-stable.tar.gz
tar xvzf redis-stable.tar.gz
cd redis-stable
make
make test
make install

Create a directory where to store your Redis config files and your data:

mkdir /etc/redis
mkdir /var/redis

Copy the init script that you’ll find in the Redis distribution under the utils directory into /etc/init.d. We suggest calling it with the name of the port where you are running this instance of Redis. For example:

cp utils/redis_init_script /etc/init.d/redis_6379

Copy the template configuration file you’ll find in the root directory of the Redis distribution into /etc/redis/ using the port number as name, for instance:

cp redis.conf /etc/redis/6379.conf

Create a directory inside /var/redis that will work as data and working directory for this Redis instance

mkdir /var/redis/6379

Edit the configuration file, making sure to perform the following changes:

vi /etc/redis/6379.conf

Set daemonize to yes (by default it is set to no).
Set the pidfile to /var/run/redis_6379.pid (modify the port if needed).
Change the port accordingly. In our example it is not needed as the default port is already 6379.
Set your preferred loglevel.
Set the logfile to /var/log/redis_6379.log
Set the dir to /var/redis/6379 (very important step!)

Finally add the new Redis init script to all the default runlevels using the following command

chkconfig --add redis_6379

Start Redis instance.

/etc/init.d/redis_6379 start

Check if Redis is working

redis-cli ping

Install Magento 2 Commandline Linux CentOS

Follow the below steps to install Magento 2 with sample-data.

Make sure that httpd / Nginx,PHP,MySQL and Composer are installed in the server by issuing the following commands

httpd -v / nginx -v
php -v
composer -v
mysql

Change the directory to documet root

cd /var/www/html/

Download Magento and issue the following commands

sudo git clone https://github.com/magento/magento2.git

cd magento2

sudo git checkout tags/2.2.1 -b 2.2.1

chown -R httpd:www /var/www/html/magento2/

sudo chmod -R 775 /var/www/html/magento2/

chmod 777 var/ generated/ app/etc/ pub/

composer install

composer update

composer config repositories.0 composer https://repo.magento.com

php bin/magento setup:install --base-url=http://127.0.0.1/magento2/ --backend-frontname=admin \
--db-host=127.0.0.1 --db-name=mage2 --db-user=root --db-password=Root@123 \
--admin-firstname=Magento --admin-lastname=User --admin-email=user@example.com \
--admin-user=admin --admin-password=admin17 --language=en_US \
--currency=USD --timezone=America/Chicago --use-rewrites=1

php bin/magento sampledata:deploy

php bin/magento setup:upgrade

php bin/magento setup:static-content:deploy

Now check the site http://127.0.0.1/magento2/ .

Install MySQL Linux CentOS

Download Package

yum localinstall https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm -y

Install MySQL

yum install mysql-community-server -y

Start MySQL

service mysqld start
service mysqld status

Generate Temporary password

grep 'temporary password' /var/log/mysqld.log

Login To MySQL

mysql -uroot -p

Reset Password and set Privilages

ALTER USER 'root'@'localhost' IDENTIFIED BY 'Root@123';

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'Root@123' WITH GRANT OPTION;

FLUSH PRIVILEGES;

Open MySQL Port 3306

firewall-cmd --zone=public --add-port=3306/tcp --permanent

firewall-cmd --reload

firewall-cmd --list-all

Configure MPM CentOS Linux

Configure Multi-Processing modules contains two parts. First I override default Apache configuration for event mode that I picked during compilation. In that case you will know what is actually configured. So let’s start with that first.

At the bottom of httpd.conf you need to uncomment the line:

Include conf/extra/httpd-mpm.conf

It will enable advanced Apache httpd MPM configuration and it will override the defaults.

Now edit enabled file:

sudo vi /usr/local/apache2/conf/extra/httpd-mpm.conf

This file contains configuration of all MPM There are configuration for each MPM module, so make sure that you are setting correct values. I enabled event mode, so this is the section I care about:

<IfModule mpm_event_module>
StartServers 5
MinSpareThreads 75
MaxSpareThreads 250
ThreadsPerChild 25
MaxRequestWorkers 400
MaxConnectionsPerChild 0
</IfModule>

Install PHP From Source CentOS Linux

Install required tools for compilation

In order to compile PHP from source you need to install few tools and libraries. First you need EPEL repository to be enabled. This repository contains more recent version of packages.

sudo yum install epel-release -y

Once you have it installed execute following command to install required packages

yum install autoconf libtool re2c bison libxml2-devel bzip2-devel libcurl-devel libpng-devel libicu-devel gcc-c++ libmcrypt-devel libwebp-devel libjpeg-devel openssl-devel libxslt-devel -y

Download and unpack PHP Source code

Next step is downloading PHP source code. Easiest option is to download it from GitHub PHP releases. Choose the version you would like to install. In my case it’s 7.2.3. Copy link to tar.gz archive and execute following commands:

curl -O -L https://github.com/php/php-src/archive/php-7.1.18.tar.gz

tar -xvf php-7.1.18.tar.gz

cd php-src-php-7.1.18/

It will download the archive from GitHub, unpack the sources and change working directory to unpacked sources.

Compile PHP

Now it’s time to compile PHP. First we need to build configure command. In order to do that execute following command:

./buildconf --force

Once configure command is created we can use it to configure PHP installation. This process will enable certain PHP extensions such as PDO, FPM, OPCache, GD library etc. If you need any libraries that are not provided here, you can execute ./configure –help option and check if there is something you need. Following command will install PHP with most common extensions:

./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs --with-freetype-dir=/usr/include/freetype2 --disable-short-tags --enable-xml --enable-cli --with-openssl --with-pcre-regex --with-pcre-jit --with-zlib --enable-bcmath --with-bz2 --with-curl --enable-exif --with-gd --enable-intl --with-mysqli --enable-pcntl --with-pdo-mysql --enable-soap --enable-sockets --with-xmlrpc --enable-zip --with-webp-dir --with-jpeg-dir --with-png-dir --enable-json --enable-hash --enable-mbstring --with-mcrypt --enable-libxml --with-libxml-dir --enable-ctype --enable-calendar --enable-dom --enable-fileinfo --with-mhash --with-incov --enable-opcache --enable-phar --enable-simplexml --with-xsl --with-pear

Apart from enabling extensions command above will also set where PHP will be installed. In my case it’s /usr/local/php location. If you will want to remove compiled PHP you will simply have to remove entire directory given under –prefix option.

Next it’s time to compile PHP. Please be aware that it takes few minutes:

make clean
make
make test

Install compiled PHP

Once PHP is compiled it is time to install it. Simply execute following command:

sudo make install

php.ini and OPCache configuration

Second thing is php.ini file. After installation php.ini file should located in /usr/local/php/lib. This is only the location. After compiling from source You won’t anything there so we need to copy it from uncompressed sources.

cd /usr/local/php/lib
cp /tmp/php-src-php-7.1.18/php.ini-development ./php.ini
vi php.ini

Add PHP to $PATH

We can do one more thing to make our life easier:) Add PHP executable to PATH, so we’ll be able to call php command from every directory.

echo 'pathmunge /usr/local/php/bin' > /etc/profile.d/php.sh

Execute such command, log out, log in and You’ll be able to execute:

php -v

Setup GZIP compression CentOS Linux

GZIP compression has serious impact on performance and your website loading time. In order to use it make sure that mod_deflate and mod_filter are enabled.

First create new file that will contain GZIP settings for Apache:

sudo vi /usr/local/apache2/conf/extra/httpd-deflate.conf

Paste there following content and save the file:

<IfModule mod_deflate.c>
<IfModule mod_filter.c>
AddOutputFilterByType DEFLATE application/ecmascript
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
</IfModule>
</IfModule>

It will add GZIP compression to most popular file types such as HTML, CSS, JS etc. If you need anything else that requires compressing, simply add more MIME types.

Now you need to include it to main Apache configuration. so open httpd.conf and Include httpd-deflate.conf.

vi /usr/local/apache2/conf/httpd.conf and make sure the following line is added.

Include conf/extra/httpd-deflate.conf

Apache Shows PHP Code Instead Of Executing CentOS Linux

Make sure that PHP is installed and running correctly. An easy way to check is to run php -v from a command line and see if returns version information or any errors.

Make sure that the PHP module is listed and uncommented inside of your Apache’s httpd.conf

LoadModule php7_module modules/libphp7.so

Make sure that Apache’s httpd.conf file has the PHP MIME type in it. This should be something like AddType application/x-httpd-php .php. This tells Apache to run .php files as PHP. Search for AddType, and then make sure there is an entry for PHP, and that it is uncommented.