As a PHP developer, we must know how to build a PHP development environment. At present, the mainstream PHP development environment combination is LAMP and LNMP. This article will introduce how to build an LNMP development environment on CentOS 7. *.
Version Description:
CentOS7: 7.7
Nginx: 1.16.1
MySQL:5.7.28
PHP:7.2.25
I put all the resources needed for installation in / usr/local/src directory
preparation
Install wget
wget is a free tool for automatically downloading files from the network. It supports downloading through three most common TCP/IP protocols: http, HTTPS and FTP, and can use HTTP proxy.
sudo yum -y install wget
Install net tools
Minimize the installation of CentOS7. If you cannot use ifconfig command, you need to install net tools. If you are installing CentOS6, you do not need to install it
sudo yum -y install net-tools
Install vim
sudo yum -y install vim
Configure display line number
vim ~/.vimrc # Edit. vimrc configuration file set nu # Exit saving after entering set nu
Turn off firewall
systemctl stop firewalld.service to shut down the firewall systemctl disable firewalld.service ා turn off the firewall, power on and start automatically Whether the IP test is successful through browser input
Install Nginx
Installation dependency
(1) to install nginx, you need to compile the source code downloaded from the official website first. Compilation depends on the GCC environment. If there is no GCC environment, you need to install gcc-c + +.
yum -y install gcc gcc-c++
(2) PCRE is a Perl library, Chinese "Perl compatible regular expression library". The purpose of installing Nginx is to enable Nginx to support the Rewrite Module with URI rewriting function. If pcre library is not installed, Nginx cannot use the rewrite module function. The Rewrite Module Function of Nginx is almost necessary for enterprise applications.
yum -y install pcre pcre-devel
(3) zlib library provides many ways to compress and decompress. nginx uses zlib to gzip the content of http package, so you need to install zlib library on Centos.
yum -y install zlib zlib-devel
(4) OpenSSL is a powerful secure socket layer cipher library, which includes the main cipher algorithm, common key and certificate encapsulation management function and ssl protocol, and provides rich application programs for testing or other purposes. nginx supports not only http protocol, but also https (that is, http is transmitted over ssl Protocol), so OpenSSL library needs to be installed.
yum -y install openssl openssl-devel
Note: the version of pcre installed in yum installation mode is relatively low, but it basically does not affect the use, but it is better to manually compile and install the latest stable version of openssl on the official website.
Check basic dependency package
After the above dependency installation is completed, you can check whether each dependency installation is successful through the following command
rpm -qa pcre pcre-devel rpm -qa zlib zlib-devel rpm -qa pcre pcre-devel
Compile and install Nginx
# Here we put all the installation packages in the / usr/local/src directory for unified management cd /usr/local/src #Switch to package directory wget http://nginx.org/download/nginx-1.16.1.tar.gz ා download the nginx source package useradd nginx -s /sbin/nologin -M #Creating nginx users to manage nginx programs tar -zxvf nginx-1.16.1.tar.gz #Unzip nginx source package cd nginx-1.16.1 #precompile ./configure \ --user=nginx \ --group=nginx \ --prefix=/usr/local/nginx-1.16.1 \ --with-http_v2_module \ --with-http_ssl_module \ --with-http_stub_status_module make && make install #Compile and install cd /usr/local ln -s nginx-1.16.1 nginx #Creating soft links for nginx
Installation instructions
--prefix=PATH #Set the installation Road force --user=USER #Process user rights --group=GROUP #Process user group permissions --with-http_v2_module # HTTP2 --with-http_stub_status_module #Activation status information --with-http_ssl_module #Activate ssl function
Configure environment variables
vim /etc/profile export PATH=/usr/local/nginx/sbin:$PATH source /etc/profile
Systemd management
Create and edit the / usr/lib/systemd/system/nginx.service file
vim /usr/lib/systemd/system/nginx.service
Add the following content (the configuration here is based on the path where you install Nginx, which is installed in / usr/local directory)
[Unit] Description=The nginx HTTP and reverse proxy server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking ExecStartPre=/usr/local/nginx/sbin/nginx -t ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s stop PrivateTmp=true [Install] WantedBy=multi-user.target
Through the nginx installed from yum, the default nginx.service configuration is as follows, which can be used as a reference
# /usr/lib/systemd/system/nginx.service [Unit] Description=The nginx HTTP and reverse proxy server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/run/nginx.pid # Nginx will fail to start if /run/nginx.pid already exists but has the wrong # SELinux context. This might happen when running `nginx -t` from the cmdline. # https://bugzilla.redhat.com/show_bug.cgi?id=1268621 ExecStartPre=/usr/bin/rm -f /run/nginx.pid ExecStartPre=/usr/sbin/nginx -t ExecStart=/usr/sbin/nginx ExecReload=/bin/kill -s HUP $MAINPID KillSignal=SIGQUIT TimeoutStopSec=5 KillMode=process PrivateTmp=true [Install] WantedBy=multi-user.target
Heavy duty daemon
Execute the following command to reload systemd and scan for new or changed cells
systemctl daemon-reload
Set power on self start
systemctl enable nginx.service # Set power on self start systemctl disable nginx.service # Cancel power on self start service
Common commands for Nginx service management
systemctl status nginx.service # View Nginx status systemctl start nginx.service # Turn on Nginx systemctl stop nginx.service # Close Nginx systemctl reload nginx.service # Overload configuration systemctl restart nginx.service # Restart nginx (equivalent to stop & start)
Service startup check
You can use this command to query who occupies port 80
lsof -i :80
If the command is not recognized, lsof needs to be installed
sudo yum -y install lsof
Install MySQL
Installation dependency
(1)cmake is a compilation tool of MySQL, which must be installed
sudo yum -y install gcc gcc-c++ cmake ncurses-devel perl perl-devel autoconf bison bison-devel libtirpc libtirpc-devel
Download boost
If mysql 5.7 or above is installed, you need to install boost before compiling and installing, because the higher version of mysql requires the installation of boots library to run normally. Otherwise, CMake Error at cmake/boost.cmake:81 will be reported
Switch to the directory / usr/local/src, and download boost in this directory
MySQL 5.7.27 requires that the boost version is 1.59, and the higher version is not applicable to MySQL 5.7.28
wget http://www.sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
Baidu online disk link: boost_1_59_0.tar.gz
Compile and install MySQL
# Add MySQL user useradd -s /sbin/nologin -M mysql # Switch to the / usr/src directory cd /usr/local/src # Download MySQL wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.28.tar.gz # Unzip MySQL tar -zxvf mysql-5.7.28.tar.gz #Extract boost and move it to mysql/boost tar -zxvf boost_1_59_0.tar.gz mv boost_1_59_0 mysql-5.7.28/boost # Go to MySQL directory cd mysql-5.7.28 # precompile cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.7.28 \ -DWITH_BOOST=boost \ -DWITH_SYSTEMD=1 \ -DWITH_SSL=system \ -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \ -DMYSQL_DATADIR=/var/lib/mysql/data \ -DDEFAULT_CHARSET=utf8mb4 \ -DDEFAULT_COLLATION=utf8mb4_general_ci \ -DWITH_EXTRA_CHARSETS=all \ -DWITH_MYISAM_STORAGE_ENGINE=1 \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_INNODB_MEMCACHED=1 \ -DWITH_DEBUG=OFF \ -DWITH_ZLIB=bundled \ -DENABLED_LOCAL_INFILE=1 \ -DENABLED_PROFILING=ON \ -DMYSQL_MAINTAINER_MODE=OFF \ -DMYSQL_TCP_PORT=3306 # Compile & install make && make install # Create soft link cd /usr/local ln -s mysql-5.7.28 mysql
Configure environment variables
# Add to environment variable vim /etc/profile export PATH=/usr/local/mysql/bin:$PATH source /etc/profile
Modify profile
-
Create a mysql folder in / var/lib directory
mkdir -p /var/lib/{mysql,mysql/data} touch /var/lib/mysql/mysqld.pid chown mysql.mysql -R /var/lib/mysql/
-
Modify the / etc/my.cnf file
# Modify the / etc/my.cnf file and edit the configuration file as follows [mysqld] character-set-server=utf8mb4 collation-server=utf8mb4_general_ci datadir=/var/lib/mysql/data socket=/var/lib/mysql/mysql.sock [mysqld_safe] log-error=/var/log/mysql/mysqld.log pid-file=/var/lib/mysql/mysqld.pid [client] default-character-set=utf8mb4
-
Create mysqld.log and mysqld.pid files and modify file permissions
# Create mysqld.log and mysqld.pid files mkdir /var/log/mysql touch /var/log/mysql/mysqld.log chown mysql.mysql -R /var/log/mysql/
-
Initialize database
# Initialize the database, – initialize indicates that a secure password is generated by default, – initialize execute indicates that no password is generated mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/var/lib/mysql/data
Systemd management
Create a / usr/lib/systemd/system/mysqld.service file and edit it as follows
vim /usr/lib/systemd/system/mysqld.service
[Unit] Description=MySQL Server Documentation=man:mysqld(8) Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html After=network.target After=syslog.target [Install] WantedBy=multi-user.target [Service] User=mysql Group=mysql Type=forking PIDFile=/var/lib/mysql/mysqld.pid # Disable service start and stop timeout logic of systemd for mysqld service. TimeoutSec=0 # Execute pre and post scripts as root PermissionsStartOnly=true # Needed to create system tables ExecStartPre=/usr/local/mysql/bin/mysqld_pre_systemd # Start main service ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/var/lib/mysql/mysqld.pid $MYSQLD_OPTS # Use this to switch malloc implementation EnvironmentFile=/etc/my.cnf # Sets open_files_limit LimitNOFILE = 5000 Restart=on-failure RestartPreventExitStatus=1 PrivateTmp=true
Heavy duty daemon
Execute the following command to reload systemd and scan for new or changed cells
systemctl daemon-reload
Start MySQL
systemctl start mysqld.service # Start MySQL systemctl stop mysqld.service # Close MySQL systemctl status mysqld.service # View MySQL status
Boot from boot
systemctl enable mysqld.service # Set power on self start systemctl disable mysqld.service # Cancel power on self start
Log in to MySQL
mysql -u root -p #No password is required for the first login, just enter set password for root@localhost = password('root'); #Change Password
Install PHP
Installation dependency
sudo yum -y install gcc gcc-c++ zip unzip libxml2 libxml2-devel curl-devel autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel gd-devel bzip2 bzip2-devel libzip libzip-devel libwebp libwebp-devel
Compile and install PHP
cd /usr/local/src tar -zxvf php-7.2.25.tar.gz cd php-7.2.25 ./configure \ --prefix=/usr/local/php-7.2.25 \ --enable-fpm \ --with-fpm-user=nginx \ --with-fpm-group=nginx \ --enable-mysqlnd \ --with-pdo-mysql=mysqlnd \ --with-mysqli=mysqlnd \ --with-mysql-sock=/var/lib/mysql/mysql.sock \ --with-gd \ --with-webp-dir \ --with-png-dir \ --with-gettext \ --with-jpeg-dir \ --with-freetype-dir \ --with-iconv-dir \ --with-zlib-dir \ --with-bz2 \ --with-openssl \ --with-curl \ --enable-mbstring \ --enable-static \ --enable-zip \ --enable-bcmath \ --enable-ftp \ --enable-pcntl \ --enable-soap \ --enable-calendar \ --enable-sockets \ --enable-exif \ --enable-xml make && make install
Detailed explanation of compilation parameters
./configure \ --prefix=/usr/local/php-7.2.25 \ # Specify installation path --enable-fpm \ # Indicates to activate PHP-FPM mode service, that is, to run PHP service in FactCGI mode. --with-fpm-user=nginx \ # Specify nginx as the user of PHP-FPM process management. It is better to unify with nginx service user here. --with-fpm-group=nginx \ # Specify the PHP-FPM process management user group as nginx, which is better to be unified with the nginx service user group. --enable-mysqlnd \ --with-pdo-mysql=mysqlnd \ --with-mysqli=mysqlnd \ --with-mysql-sock=/var/lib/mysql/mysql.sock \ --with-gd \ # Open gd library support --with-webp-dir \ --with-png-dir \ --with-gettext \ # NLS (Native Language Support) API is implemented, which can be used to internationalize your PHP program --with-jpeg-dir \ --with-freetype-dir \ --with-iconv-dir \ # Contains the interface for iconv character set conversion. --with-zlib-dir \ # Open zlib library support for http compressed transport --with-bz2 \ # For transparently reading and writing bzip2 (. bz2) compressed files. --with-openssl \ # Open openssl, which is used for encrypted transmission --with-curl \ # Turn on the support of curl browsing tool --enable-mbstring \ # Multibyte, string support --enable-static \ # Build static link library --enable-zip \ # Open support for zip --enable-bcmath \ --enable-ftp \ # Provide client access to the file server through file transfer protocol (FTP) --enable-pcntl \ # Multiprocess --enable-soap \ --enable-calendar \ --enable-sockets \ # Turn on sockets support --enable-exif \ # Exchangeable image information --enable-xml
To configure
cd /usr/local ln -s php-7.2.25 php cp /usr/local/src/php-7.2.25/php.ini-development /usr/local/php-7.2.25/lib/php.ini vim /usr/local/php/lib/php.ini date.timezone = PRC (About 934 lines) expose_php = Off #Avoid exposing PHP information to http headers (about 369 lines) display_errors = Off(Production environment set to off,The development environment is set to On,Easy to debug) //Note: after setting disable error to off, you need to open the error logging path in php-fpm.conf error log = log / php-fpm.log cd /usr/local/php cp etc/php-fpm.conf.default etc/php-fpm.conf cd /usr/local/php/etc/php-fpm.d/ cp www.conf.default www.conf cd /usr/local/php sbin/php-fpm ps -e | grep php-fpm //If the parameters of -- with MySQL = mysqlnd and -- with PDO MySQL = mysqlnd are specified when compiling PHP, the socket connection problem may be encountered in production. The solution is to add the command PDO ﹣ mysql. Default ﹣ socket = / var / lib / MySQL / MySQL. Socket into php.ini //It is better to specify the location of mysql.socket when compiling PHP: --with-mysql-sock=/var/lib/mysql/mysql.sock
Manage PHP-FPM
vim /usr/local/php/etc/php-fpm.conf pid = run/php-fpm.pid Error? Log = log / php-fpm.log? Line 24 this is enabled when php.ini sets display? Errors = off Restart the server after setting up Send signals to the process to complete process management Stop: Kill - Int ` cat / usr / local / PHP / var / run / PHP fpm.pid` Smooth stop: Kill - quit ` cat / usr / local / PHP / var / run / PHP fpm.pid` Restart: Kill - usr2 ` cat / usr / local / PHP / var / run / PHP fpm.pid` Reopen the log: Kill - usr1 ` cat / usr / local / PHP / var / run / PHP fpm.pid`
Configure environment variables
vim /etc/profile export PATH=/usr/local/php/bin:$PATH source /etc/profile
Configure Systemd services
In fact, php-fpm.service file PHP has been configured for us. We just need to copy it to the specified location and enable it.
cp /usr/local/src/php-7.2.25/sapi/fpm/php-fpm.service /usr/lib/systemd/system/
Edit the / usr/lib/systemd/system/php-fpm.service file and change it to the following:
# It's not recommended to modify this file in-place, because it # will be overwritten during upgrades. If you want to customize, # the best way is to use the "systemctl edit" command. [Unit] Description=The PHP FastCGI Process Manager After=network.target [Service] Type=simple PIDFile=/usr/local/php/var/run/php-fpm.pid ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf ExecReload=/bin/kill -USR2 $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
Heavy duty daemon
Execute the following command to reload systemd and scan for new or changed cells
systemctl daemon-reload
Boot from boot
systemctl enable php-fpm.service
Start PHP FPM
systemctl start php-fpm.service
Associate Nginx with PHP
nginx.conf configuration
#user nobody; # There is a working subprocess, which can be modified by itself, but it is too big and useless, because it needs to compete for CPU # General setting CPU number * core number worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { #In general, it is to configure the characteristics of Nginx process and connection #If several work at the same time multi_accept on; #Turn on the ability to accept multiple new network connection requests at the same time. use epoll; #Use epoll event driver, because the performance of epoll is much better than other event drivers worker_connections 10240; #This means that a subprocess can connect up to 10240 connections } http { # This is the main section of configuring the http server include mime.types; default_type application/octet-stream; #Hide Nginx software version number server_tokens off; #Activate TCP ﹣ nodelay function to improve I/O performance tcp_nodelay on; # Set the timeout for reading the client request header data. The value here is 15, and its unit is seconds, which is the empirical reference value client_header_timeout 15; # Set the timeout for reading the client request body client_body_timeout 15; # Specify the timeout for responding to clients send_timeout 25; # Upload file size limit client_max_body_size 8m; #Compression configuration gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain text/css text/xml application/javascript; gzip_vary on; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #include extra/*.conf; server { listen 80; server_name www.blog.com; root html; #charset koi8-r; #access_log logs/host.access.log main; location / { index index.php index.html index.htm; if (!-e $request_filename) { rewrite ^/(.*)$ /index.php/$1 last; } } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_split_path_info ^(.+\.php)(.*)$; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } }
Install Redis
Build install
# Extract the source file tar -zxvf redis-5.0.7.tar.gz # Switch to the unzip directory cd redis-5.0.7 # Build install make PREFIX=/usr/local/redis-5.0.7 install mkdir /usr/local/redis-5.0.7/etc cp redis.conf /usr/local/redis-5.0.7/etc/ # Create soft link cd /usr/local ln -s redis-5.0.7 redis
Configure environment variables
vim /etc/profile export PATH=/usr/local/redis/bin:$PATH source /etc/profile # Make changes effective immediately
Configure background operation
Let redis run as a future process
vim /usr/local/redis/etc/redis.conf # Daeonize no (about 136 lines) # Change to - > daemonize yes
Configure Systemd services
Add a redis.service file in / usr/lib/systemd/system /
[Unit] Description=Redis After=network.target [Service] Type=forking PIDFile=/var/run/redis_6379.pid ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf ExecStop=/usr/local/redis/bin/redis-cli shutdown PrivateTmp=true [Install] WantedBy=multi-user.target
Heavy duty daemon
Execute the following command to reload systemd and scan for new or changed cells
systemctl daemon-reload
Boot from boot
systemctl enable redis.service
Start redis service
systemctl start redis.service
reference material
centos7 source compilation and installation mysql5.7
Related configuration of system D for mysql under Linux 7
Managing MySQL Server with systemd
Centos7 7 7.3 PHP compilation and installation
Compiling and installing php7.3 under centos7
cmake installation configuration and Getting Started Guide