More than just setting up a backend development environment for LNMP

Keywords: MySQL PHP Nginx yum Redis

Preface

The goal of this project is to install a series of common application services for back-end development on the newly installed CentOS 7.5 system (CentOS installation is not described here), and to deploy a website using phpMyAdmin as an example.

  • Nginx(1.16)
  • MySQL(8.0, Start Up)
  • PHP(7.3)
  • Redis(1.5)
  • memcached(1.4)
  • Composer(1.8)
  • phpMyAdmin(4.8)
  • nodejs(10.15)
  • yarn(1.15)

Important: Recommended if you find installation and configuration too cumbersome pagoda Visual management tools to operate, basically one-click operation greatly improves efficiency, in a word, cool! (Of course, it is not perfect, such as when the version of the tool does not support installing MySQL8.0 in ubuntu)

Appointment

The virtual machine ip is 192.168.8.15,
The administrator username sorgo,
Service program execution username www,
The website eradicates directories/data/wwwroot,
root user action except for special instructions when installing and configuring,
After executing the Edit File command, the next line begins to indent four spaces to the right for this edit

CentOS

The vmware virtual machine can be installed in less than 20 minutes (base web server version 1810, selected Chinese environment, yum source defaults to 163.com)

User Settings

useradd www -s /usr/sbin/nologin
useradd -G www sorgo
passwd sorgo
#Give sorgo user root permission, and switch root without entering a password, `-l`View existing permissions
visudo
    #Edit Content:
    #Add an entry to indicate that sorgo can use `sudo` to perform root permissions
    sorgo ALL=(ALL) NOPASSWD: ALL

Global Command Alias Settings

vim /etc/bashrc
    # Add content:
    alias s-start='systemctl start'
    alias s-stop='systemctl stop'
    alias s-restart='systemctl restart'
    alias s-enable='systemctl enable'
    alias s-disable='systemctl disablep'
    alias s-status='systemctl status'
    alias vi='vim'
    alias ll='ls -laph'
    alias ..='cd ..'
    alias nrd='npm run dev'
    alias nrh='npm run hot'
    alias nrp='npm run production'
    alias nrw='npm run watch'
    alias nrww='npm run watch-poll'
    alias yrd='yarn run dev'
    alias yrh='yarn run hot'
    alias yrp='yarn run production'
    alias yrw='yarn run watch'
    alias yrwp='yarn run watch-poll'
    alias pa='php artisan'
    alias phpspec='vendor/bin/phpspec'
    alias phpunit='vendor/bin/phpunit'
#Make changes take effect immediately
source /etc/bashrc

Site Directory and Permissions

#Project Directory
mkdir -p /data/wwwroot/
#Log Directory
mkdir -p  /data/wwwlogs/nginx/
#Installation directory for site infrastructure applications
mkdir /data/server/
#Empowerment
chown -R sorgo:www /data/
chmod -Rf g+s /data/

Network Card Configuration

#Look at the name of the network card. That's in the left column
ifconfig
#Edit network card configuration to secure nat mode ip
vi /etc/sysconfig/network-scripts/ifcfg-ens33
    #Edit Content:
    #Profile key content, for reference only
    NAME=ens33
    DEVICE=ens33
    DEFROUTE=yes
    ONBOOT=yes
    #Set to static with `IPADDR`
    BOOTPROTO=static
    IPADDR=192.168.8.15
    NETMASK=255.255.255.0
    GATEWAY=192.168.8.2
    DNS1=192.168.8.2

yum source configuration

Aliyuan

cd /etc/yum.repos.d
mv CentOS-Base.repo CentOS-Base.repo.bak
wget -O CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all
yum makecache

163 Source
Official Use of Help

cd /etc/yum.repos.d/  
mv CentOS-Base.repo CentOS-Base.repo.backup  
wget -O CentOS-Base.repo https://mirrors.163.com/.help/CentOS7-Base-163.repo
yum clean all
yum makecache

#Update all yum installed programs on the system, choose
yum update -y

Close Firewall

In centos7.5, the firewall is turned on by default and turned off here for convenience in the development environment

#View Status
systemctl status firewalld
#Cancel startup
systemctl disable firewalld
#Close Firewall
systemctl stop firewalld
#Turn off selinux, otherwise affect the use of samba
vi /etc/selinux/config
    #Edit Content:
    SELINUX=disabled

pstree

yum install psmisc -y
#View currently running processes
pstree

Tools for compiling Installers

#centos1810 is installed by default
yum install -y gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel 

Useful Commands

#View pid by process name
pidof nginx
#Terminate by process name
pkill redis-server
killall redis-server

ssh

ssh Connection Tool Recommended MobaXterm , with its own ftp file transfer function

Nginx(1.16+)

This installation is stable (currently 1.16. *)
Official Installation Guidance Document

Add nginx source files

#Create Source File
vi /etc/yum.repos.d/nginx.repo
    #Add the following
    [nginx-stable]
    name=nginx stable repo
    baseurl=http://nginx.org/packages/centos/7/$basearch/
    gpgcheck=1
    enabled=1
    gpgkey=https://nginx.org/keys/nginx_signing.key
    
    [nginx-mainline]
    name=nginx mainline repo
    baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
    gpgcheck=1
    enabled=0
    gpgkey=https://nginx.org/keys/nginx_signing.key

If you want to set the main line version as the default installed version, execute yum-config-manager --enable nginx-mainline

install

yum install yum-utils
#The following command installs the stable version set by the previous source file
yum install -y nginx
#View installed versions
nginx -v

Site Directory Configuration

Global Configuration

#Configuration Execution User Name
vi /etc/nginx/nginx.conf
    #Edit Content:
    user www;

Add a nginx site configuration for the new project. This is a configuration template for the php site. The configuration of other sites will be modified and matched based on this template in the future.

#vi /etc/nginx/conf.d/your_project_name.conf
    #Edit Content:
    #Add to
    server
    {
      listen 1700;
      #listen 443 ssl http2;
      #listen [::]:443 ssl http2;
      #listen [::]:80;
      #server_name domain.com;
      
      #Character set settings can only be within a server block
      charset utf-8;
      
      index index.php index.html index.htm default.php default.htm default.html;
      root /data/wwwroot/your_project_name/public;
      
      #access_log  /data/wwwlogs/nginx/your_project_name.log;
      #error_log  /data/wwwlogs/nginx/your_project_name.error.log;
    
      #laravel, thinkPHP Routing Configuration
      location / {
       try_files $uri $uri/ /index.php?$query_string;
      }
      
      #Run php file
      location ~ \.php$ {
        try_files $uri =404;
        fastcgi_index  index.php;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
      }
    
      #Prohibited Files or Directories
      location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
      {
        return 404;
      }
      
      location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
      {
        expires      30d;
        error_log off;
        access_log /dev/null;
      }
      
      location ~ .*\.(js|css)?$
      {
        expires      12h;
        error_log off;
        access_log /dev/null; 
      }
    }

Function

#Test configuration information for syntax issues before it can be started
nginx -t
#start-up
nginx

All Instructions

Nginx-t #Test Configuration Information
nginx #default start mode
Nginx-v #Displays version information, -V (large V) displays compile-time parameters
Nginx-s stop #Quick stop service
Nginx-s quit #Stop service normally
Nginx-s reload #Restart

PHP(7.3)

PHP7.3

install

#Installation Source
yum install -y epel-release
yum install -y http://rpms.remirepo.net/enterprise/remi-release-7.rpm
#Search for eligible packages
yum search php73
#Install php7.3 and its extensions
#php extensions including redis, memcached, msgpack, and yaf framework
yum install -y php73-php php73-php-fpm php73-php-pear php73-php-bcmath php73-php-mbstring php73-php-cli php73-php-pdo php73-php-pecl-mysql php73-php-mysqlnd php73-php-pecl-redis4 php73-php-pecl-memcached php73-php-pecl-msgpack php73-php-pecl-yaf php73-php-pecl-jsond-devel php73-php-gd php73-php-common php73-php-intl php73-php-xml php73-php-opcache php73-php-pecl-apcu php73-php-gmp php73-php-process php73-php-pecl-imagick php73-php-devel php73-php-zip php73-php-ldap php73-php-imap php73-php-pecl-mcrypt

#Installation path of the program: `/opt/remi/php73/`;
#Profile path: `/etc/opt/remi/php73/php.ini`;

To configure

cp /etc/opt/remi/php73/php.ini /etc/opt/remi/php73/php.ini.bak
#Modify php time zone and expand file directory path
vi /etc/opt/remi/php73/php.ini
    #Edit Content:
    date.timezone = PRC
    extension_dir = "/opt/remi/php73/root/usr/lib64/php/modules/"
    
#Modify php-fpm execution user
vi /etc/opt/remi/php73/php-fpm.d/www.conf
    #Edit Content:
    user=www
    group=www
    
#Empowerment
chown -R root:www /var/opt/remi/

#Soft Chain to General Directory
ln -s /etc/opt/remi/php73/php.ini /etc/php.ini
ln -s /opt/remi/php73/root/usr/bin/php /usr/bin/php
ln -s /opt/remi/php73/root/usr/sbin/php-fpm /usr/bin/php-fpm
ln -s /opt/remi/php73/root/usr/bin/php-cgi /usr/bin/php-cgi
ln -s /opt/remi/php73/root/usr/bin/pear /usr/bin/php-pear
ln -s /opt/remi/php73/root/usr/bin/phar.phar /usr/bin/php-phar

#View installed php version
php -v
#View installed php extensions
php -m

Run php-fpm

#start-up
php-fpm
#termination
kill -QUIT `cat /var/opt/remi/php73/run/php-fpm/php-fpm.pid`
#restart
kill -USR2 `cat /var/opt/remi/php73/run/php-fpm/php-fpm.pid`

MySQL(8.0)

Official Installation Guide

yum installation

install

#Download Source
wget https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm
#Installation Source
yum localinstall -y mysql80-community-release-el7-1.noarch.rpm
  #List available mysql packages
  #yum repolist enabled | grep mysql
  #If you plan to install 5.7, switch to the default version by executing the following two sentences
  #yum-config-manager --disable mysql80-community
  #yum-config-manager --enable mysql57-community
#install
yum install -y mysql-community-server
    #Update Version
    #yum update mysql-server

# View Version
mysql -V

To configure

vi /etc/my.cnf
    #Edit Content
    [mysqld]
    # Set up port 3301
    port=3301
    # Maximum number of connections allowed
    max_connections=200
    # Number of failed connections allowed.This is to prevent someone from attempting to attack the database system from this host
    max_connect_errors=10
    # The character set used by the server defaults to UTF8
    #character-set-server=utf8
    # Default storage engine to be used when creating new tables
    default-storage-engine=INNODB
    # Defaults to "mysql_native_password" plug-in authentication, saved as MySQL 5.7 password handling, compatible with older connectors, and 8.0 defaults to caching_sha2_password
    default_authentication_plugin=mysql_native_password
    #Set Default Time Zone
    default-time_zone = '+8:00'

Function

# start-up
systemctl start mysqld
# Start Up
systemctl enable mysqld
# state
systemctl status mysqld
# View root initial password
grep 'temporary password' /var/log/mysqld.log

# Sign in
mysql -uroot -p
# The initial password must be modified. The new password must contain at least 8 characters for size letters, numbers, and special characters
# Passwords are treated as mysql_native_password, saved as MySQL 5.7, compatible with older connectors, and 8.0 defaults to caching_sha2_password
  mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Sorgo123.';
# Enter MySQL database
  mysql> use mysql;
# Set the root user to log on to any host
  mysql> update user set host='%' where user='root' and host='localhost';
  mysql> FLUSH PRIVILEGES;   
# Query user table information
  mysql> select user,host,plugin from user;

Redis(5.0)

Recommended Manual

install

cd /data/server/
wget http://download.redis.io/releases/redis-5.0.4.tar.gz
tar -zvxf redis-5.0.4.tar.gz && cd redis-5.0.4
make
#Soft chain into a unified directory for easy multi-version management
ln -s /data/server/redis-5.0.4 /data/server/redis
ln -s /data/server/redis/redis.conf /etc/redis.conf
#Soft Chain to Command Directory
ln -s /data/server/redis/src/redis-cli /usr/local/bin/redis-cli
ln -s /data/server/redis/src/redis-server /usr/local/bin/redis-server
ln -s /data/server/redis/src/redis-sentinel /usr/local/bin/redis-sentinel
#View Version
redis-server -v

To configure

# vi /etc/redis.conf
    #Accessible to any host
    bind 0.0.0.0
    #Password Logon
    requirepass Sorgo123.
    #Run as a background process
    daemonize yes

Function

#Open by Profile
redis-server /etc/redis.conf
#Enter the Interaction Tool
redis-cli -h 127.0.0.1 -p 6379 -a Sorgo123.
#View redis process information
info server
#Shut down services
shutdown

memcached(1.4)

yum installation 1.4

yum install -y memcached
#View Help Information and Versions
memcached -h
#Open to open multiple processes on different ports
memcached -d -p 11211 -m 150 -u www
#Shut down, shutting down all processes whose names contain `memcached`
pkill memcached

Startup options:
-d is to start a daemon;
-m is the amount of memory allocated to Memcache in MB;
-u is the user running Memcache;
-l is the IP address of the server being listened on and can have multiple addresses;
-p is the port on which Memcache listens, preferably 1024 or more;
-c is the maximum number of concurrent connections to run, defaulting to 1024;
-P is the pid file that sets up to save Memcache.

Compile Installation, 1.5

cd /data/server/
#Install dependencies first, otherwise they cannot be compiled
yum install -y libevent-devel
#Download Source
wget http://www.memcached.org/files/memcached-1.5.13.tar.gz
#Unzip and enter directory
tar -zvxf memcached-1.5.13.tar.gz && cd memcached-1.5.13
#Execute the configuration, where only the installation directory is set
./configure --prefix=/data/server/memcached-1.5.13/
#Compile and Install
make && make install
ln -s /data/server/memcached-1.5.13 /data/server/memcached
ln -s /data/server/memcached/bin/memcached /usr/local/bin/memcached
#View installed versions
memcached -h

samba(4.8)

#install
yum install -y samba samba-client samba-common 
#Add samba user, user name that already exists, password specific to samba login:sorgo
smbpasswd -a sorgo
#View added users
pdbedit -L

#Modify Configuration
cp /etc/samba/smb.conf /etc/samba/smb.conf.bak
vim /etc/samba/smb.conf
    #Add directory paths accessible through samba
    [wwwroot]
             comment = website root
             path = /data/wwwroot
             valid users = sorgo
             writable = yes
             create mask = 0750
             directory mask = 0750
             
#Detect profile syntax
testparm
#start-up
systemctl enable smb && systemctl enable nmb
systemctl restart smb && systemctl restart nmb
#View Status
smbstatus

# linux side use
##Test Connection
smbclient -L //192.168.8.15 -U smb_user_naem
##Mount to Application
mount -t cifs //192.168.8.15/wwwroot/  /smb/ -o  username=smb_user_naem,password=1234
df -h /smb
##uninstall
umount /smb

# windows side use
\\${ip}

# Matters needing attention
* Never write Chinese in your profile,Even Chinese notes,otherwise windows Cannot enter the directory!!!
* If the server is in windows in ping Impossible(Not because the server is blocked ping,But for unknown reasons),That's not even possible.
* phpstrom On Open samba Exceptional Carton on Project File

Composer(1.8)

install

#Generate composer.phar
php73 -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"
php73 composer-setup.php
php73 -r "unlink('composer-setup.php');"
#Move to Command Directory
mv composer.phar /usr/local/bin/composer
#Version and other information
composer

Set up

#Set Source to Domestic Mirror
composer config -g repo.packagist composer https://packagist.phpcomposer.com
#Unset the above settings
composer config -g --unset repos.packagist
#View global configuration
composer config -gl

phpMyAdmin

phpMyAdmin is actually a website written by php. Download the PHP file package to the server on the official website and set nginx to point to the root directory.

cd /data/server/

#Get 1: Download using composer (sometimes at a slow speed)
#composer create-project phpmyadmin/phpmyadmin
#Get it 2: wget downloads directly from the official website (this is how this demo works)
wget https://files.phpmyadmin.net/phpMyAdmin/4.8.5/phpMyAdmin-4.8.5-all-languages.zip
#Get it 3: Download it elsewhere, then upload it to this server via ftp, which is convenient in principle and which

#Unzip to current directory
unzip phpMyAdmin-4.8.5-all-languages.zip
#Soft Chain Easy Version Control
ln -s phpMyAdmin-4.8.5-all-languages phpmyadmin

#Edit Profile
cp phpmyadmin/config.sample.inc.php phpmyadmin/config.inc.php
vi phpmyadmin/config.inc.php
    #Here are the edits: 
    #Set keys of any length above 32 bits
    $cfg['blowfish_secret'] = 'dNE9GVnon3LWSVGvhGPuZdBhb7c7RtQ3'; 

#Empowerment
chmod -R 770  phpmyadmin/

#nginx site configuration, using sed command to replace a key value in a template configuration
#Major changes to listening port`8888` and log file name`phpmyadmin`
cat /etc/nginx/conf.d/your_project_name.conf | sed 's/1700/8888/g' | sed 's/your_project_name\./phpmyadmin\./g' | sed 's/wwwroot\/your_project_name\/public/server\/phpmyadmin\//g' > /etc/nginx/conf.d/phpmyadmin.conf

#Check the configuration file for errors before starting
nginx -t
#Restart nginx
nginx -s reload
#Visit
http://192.168.8.15:8888/

node.js(10.15lts)

Chinese API Documentation
node.js is installed in:

  1. epel source yum installation (currently version 6.16);
  2. Binary file installation;
  3. Source code compilation and installation;

Now the latter two methods are introduced, and they are also the ones with richer version choices.

Binary installation (10.15.3) (recommended)

This is available for download

cd /data/server/
#Download binary package, about 12M
wget https://nodejs.org/dist/v10.15.3/node-v10.15.3-linux-x64.tar.xz
#Unzip, note that this unzip command is different from unzipping the file at the end of `.gz`
tar -xJvf node-v10.15.3-linux-x64.tar.xz
#Soft Chain
ln -s node-v10.15.3-linux-x64 node
ln -s /data/server/node/bin/node /usr/local/bin/node
ln -s /data/server/node/bin/npm /usr/local/bin/npm
ln -s /data/server/node/bin/npx /usr/local/bin/npx

#View Version
node -v
npm -v

Compile and Install (10.15.3)

The compilation process is slow, testing for yourself for about an hour

cd /data/server/
#Download source, about 35M
wget https://nodejs.org/dist/v10.15.3/node-v10.15.3.tar.gz
tar -zvxf node-v10.15.3.tar.gz && cd node-v10.15.3
#Execute Configuration Script
./configure
#Compile and Install
make && make install
#View Version
node -v
npm -v

yarn(1.15)

Official Installation Documentation

#Add a yum source for yarn
curl --silent --location https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo
#Install, make sure nodejs is installed
yum install -y yarn
#View Version
yarn -v

Posted by spamyboy on Fri, 26 Apr 2019 10:12:35 -0700