LNMP architecture introduction, MySQL and PHP installation, Nginx introduction

Keywords: Programming PHP MySQL Nginx curl

LNMP architecture introduction

  • Unlike LAMP, Nginx provides web Services
  • And php exists as an independent service, which is called php FPM
  • Nginx processes static requests directly, and dynamic requests are forwarded to PHP FPM

MySQL reinstallation

  • MySQL is installed by compiling free binary installation package. You need to delete the corresponding directory before reinstallation. Before reinstallation, you need to stop the service
[root@test-a ~]# ps -ef | grep mysql
root      1449     1  0 14:35 ?        00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/usr/local/mysql/mysqld.pid
mysql     1930  1449  0 14:35 ?        00:00:02 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/usr/local/mysql/db.err --pid-file=/usr/local/mysql/mysqld.pid --socket=/usr/local/mysql/mysql.sock --port=3306
root      2496  2430  0 15:09 pts/0    00:00:00 grep --color=auto mysql
[root@test-a ~]# service mysqld stop
Shutting down MySQL.. SUCCESS!

[root@test-a ~]# rm -rf /usr/local/mysql # Delete MySQL installation directory
[root@test-a ~]# rm -rf /etc/init.d/mysqld # Delete MySQL service script
[root@test-a ~]# rm /data/mysql/* -rf  # Clear MySQL data 
  • Unpack and reinstall
[root@test-a ~]# cd /usr/local/src/
[root@test-a src]# tar -zxvf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz  
[root@test-a src]# mv mysql-5.7.23-linux-glibc2.12-x86_64 /usr/local/mysql
[root@test-a src]# ls /usr/local/mysql
bin  COPYING  docs  include  lib  man  README  share  support-files  

[root@test-a src]# cd /usr/local/mysql
[root@test-a mysql]# id mysql  # mysql user is required to check whether it exists
uid=1007(mysql) gid=1008(mysql) groups=1008(mysql)

[root@test-a mysql]# ./bin/mysqld --initialize --user=mysql --datadir=/data/mysql #Initialize to generate some directories and files required for MySQL startup
2018-11-25T07:27:03.963759Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-11-25T07:27:03.986922Z 0 [ERROR] Could not open file '/usr/local/mysql/db.err' for error logging: Permission denied
2018-11-25T07:27:03.986975Z 0 [ERROR] Aborting

[root@test-a mysql]# touch /usr/local/mysql/db.err
[root@test-a mysql]# chown mysql:mysql db.err
[root@test-a mysql]# ./bin/mysqld --initialize --user=mysql --datadir=/data/mysql  # The temporary password generated this time is in the db.err file  

[root@test-a mysql]# cp support-files/mysql.server /etc/init.d/mysqld # Copy startup script
[root@test-a mysql]# vim /etc/init.d/mysqld # Edit startup script, set basedir and datadir
basedir=/usr/local/mysql
datadir=/data/mysql  

# start-up
[root@test-a mysql]# /etc/init.d/mysqld start
Starting MySQL...... ERROR! The server quit without updating PID file (/usr/local/mysql/mysqld.pid).
[root@test-a mysql]# chown mysql:mysql -R .
[root@test-a mysql]#
[root@test-a mysql]# /etc/init.d/mysqld start
Starting MySQL.. SUCCESS!
#Set startup
[root@test-a mysql]# chkconfig --add mysqld
[root@test-a mysql]# chkconfig mysqld on

PHP installation

[root@test-a mysql]# cd /usr/local/src/php-5.6.32/
[root@test-a php-5.6.32]# make clean  # Delete previously compiled files
find . -name \*.gcno -o -name \*.gcda | xargs rm -f
find . -name \*.lo -o -name \*.o | xargs rm -f
find . -name \*.la -o -name \*.a | xargs rm -f
find . -name \*.so | xargs rm -f
find . -name .libs -a -type d|xargs rm -rf
rm -f libphp5.la sapi/cli/php sapi/cgi/php-cgi    libphp5.la modules/* libs/*

[root@test-a php-5.6.32]# ./configure --prefix=/usr/local/php-fpm --with-config-file-path=/usr/local/php-fpm/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --with-pear --with-curl  --with-openssl

...
checking whether to enable ctype functions... yes
checking for cURL support... yes
checking for cURL in default path... not found
configure: error: Please reinstall the libcurl distribution -
    easy.h should be in <curl-dir>/include/curl/

# Error reporting, install curl
[root@test-a php-5.6.32]# yum install -y libcurl-devel
# Reinitialize
[root@test-a php-5.6.32]# ./configure --prefix=/usr/local/php-fpm --with-config-file-path=/usr/local/php-fpm/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --with-pear --with-curl  --with-openssl  

...
Thank you for using PHP.

config.status: creating php5.spec
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/fpm/php-fpm.conf
config.status: creating sapi/fpm/init.d.php-fpm
config.status: creating sapi/fpm/php-fpm.service
config.status: creating sapi/fpm/php-fpm.8
config.status: creating sapi/fpm/status.html
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands

# Compilation and installation
[root@test-a php-5.6.32]# make && make install
...
[PEAR] XML_Util       - installed: 1.4.2
[PEAR] PEAR           - installed: 1.10.5
Wrote PEAR system config file at: /usr/local/php-fpm/etc/pear.conf
You may want to add: /usr/local/php-fpm/lib/php to your php.ini include_path
/usr/local/src/php-5.6.32/build/shtool install -c ext/phar/phar.phar /usr/local/php-fpm/bin
ln -s -f phar.phar /usr/local/php-fpm/bin/phar
Installing PDO headers:           /usr/local/php-fpm/include/php/ext/pdo/
  • To configure
[root@test-a php-5.6.32]# ls /usr/local/php-fpm/
bin  etc  include  lib  php  sbin  var
[root@test-a php-5.6.32]# ls /usr/local/php
bin  etc  include  lib  php
# The options of php FPM are similar to php, with - i,-m and so on, and more - t to check the configuration syntax
[root@test-a php-5.6.32]# /usr/local/php-fpm/sbin/php-fpm -t
[25-Nov-2018 16:45:09] ERROR: failed to open configuration file '/usr/local/php-fpm/etc/php-fpm.conf': No such file or directory (2)
[25-Nov-2018 16:45:09] ERROR: failed to load configuration file '/usr/local/php-fpm/etc/php-fpm.conf'
[25-Nov-2018 16:45:09] ERROR: FPM initialization failed

[root@test-a php-5.6.32]# cd /usr/local/php-fpm/etc/
[root@test-a etc]# cp /usr/local/src/php-5.6.32/php.ini-production ./php.ini # Copy configuration file php.ini
[root@test-a etc]# vim php-fpm.conf # Create the php-fpm.conf configuration file and add the following
[global] # Global configuration
pid = /usr/local/php-fpm/var/run/php-fpm.pid
error_log = /usr/local/php-fpm/var/log/php-fpm.log
[www] #Module name
listen = /tmp/php-fcgi.sock #socket monitoring
# listen = 127.0.0.1:9000 # tcp monitoring
listen.mode = 666 # /tmp/php-fcgi.sock file permissions
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024

# Copy startup script
[root@test-a etc]# cp /usr/local/src/php-5.6.32/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm  
[root@test-a etc]# chmod 755 /etc/init.d/php-fpm
[root@test-a etc]# chkconfig --add php-fpm
[root@test-a etc]# chkconfig php-fpm on
[root@test-a etc]# useradd -s /sbin/nologin php-fpm # Create a PHP FPM user who cannot log in  

root@test-a etc]# service php-fpm start
Starting php-fpm  done
[root@test-a etc]# ps -aux|grep php-fpm
root     54315  1.0  0.4 124564  4880 ?        Ss   17:06   0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm  54316  0.0  0.4 124564  4644 ?        S    17:06   0:00 php-fpm: pool www
php-fpm  54317  0.0  0.4 124564  4644 ?        S    17:06   0:00 php-fpm: pool www
php-fpm  54318  0.0  0.4 124564  4644 ?        S    17:06   0:00 php-fpm: pool www
php-fpm  54319  0.0  0.4 124564  4644 ?        S    17:06   0:00 php-fpm: pool www
php-fpm  54320  0.0  0.4 124564  4648 ?        S    17:06   0:00 php-fpm: pool www
php-fpm  54321  0.0  0.4 124564  4652 ?        S    17:06   0:00 php-fpm: pool www
php-fpm  54322  0.0  0.4 124564  4652 ?        S    17:06   0:00 php-fpm: pool www
php-fpm  54323  0.0  0.4 124564  4652 ?        S    17:06   0:00 php-fpm: pool www
php-fpm  54324  0.0  0.4 124564  4652 ?        S    17:06   0:00 php-fpm: pool www
php-fpm  54325  0.0  0.4 124564  4652 ?        S    17:06   0:00 php-fpm: pool www
php-fpm  54326  0.0  0.4 124564  4652 ?        S    17:06   0:00 php-fpm: pool www
php-fpm  54327  0.0  0.4 124564  4652 ?        S    17:06   0:00 php-fpm: pool www
php-fpm  54328  0.0  0.4 124564  4652 ?        S    17:06   0:00 php-fpm: pool www
php-fpm  54329  0.0  0.4 124564  4652 ?        S    17:06   0:00 php-fpm: pool www
php-fpm  54330  0.0  0.4 124564  4652 ?        S    17:06   0:00 php-fpm: pool www
php-fpm  54331  0.0  0.4 124564  4652 ?        S    17:06   0:00 php-fpm: pool www
php-fpm  54332  0.0  0.4 124564  4652 ?        S    17:06   0:00 php-fpm: pool www
php-fpm  54333  0.0  0.4 124564  4652 ?        S    17:06   0:00 php-fpm: pool www
php-fpm  54334  0.0  0.4 124564  4652 ?        S    17:06   0:00 php-fpm: pool www
php-fpm  54335  0.0  0.4 124564  4652 ?        S    17:06   0:00 php-fpm: pool www

Nginx introduction

  • Nginx official website nginx.org
  • Nginx application scenarios: web services, reverse proxy, load balancing
  • The famous branch of Nginx, Tengine developed by Taobao based on Nginx, is the same as Nginx in terms of use, service name and configuration file name. The biggest difference with Nginx is that Tenging has added some customized modules, which are outstanding in terms of safety and speed limit. In addition, it supports the combination of js and css
  • Nginx core + lua related components and modules constitute a high-performance web container supporting lua, openresty http://jinnianshilongnian.iteye.com/blog/2280928

Posted by Jason28 on Thu, 05 Dec 2019 20:56:00 -0800