As a PHP developer, we must know how to set up a PHP development environment. The mainstream combination of PHP development environment is LAMP and LNMP. This article will introduce how to set up a LNMP development environment on CentOS7. *.
Version description:
CentOS7: 7.7
Nginx: 1.16.1
MySQL:5.7.28
PHP:7.4.0
Dead work
Install wget
wget is a free tool for automatically downloading files from the network, supports downloading through the three most common TCP/IP protocols: HTTP, HTTPS, FTP, and may use an HTTP proxy.
sudo yum install wget
Install net-tools
Minimize installation of CentOS7 if ifconfig command is not available, net-tools need to be installed, or CentOS6 if installed
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 save after entering set nu
Close Firewall
systemctl stop firewalld.service #order to close the firewall systemctl disable firewalld.service #Turn off firewall Start-up Success of IP test by browser input
Install Nginx
Installation Dependency
(1) To install nginx, you need to compile the source code downloaded from the official website first. The 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) PCR E is a Perl library, Chinese "Perl compatible regular expression library".Nginx is installed to enable Nginx to support rewrite modules with URI rewriting. Without the pcre library installed, Nginx cannot use Rewrite Module functionality, which is almost necessary for enterprise applications.
yum -y install pcre pcre-devel
(3) The zlib library provides a variety of compression and decompression methods. nginx uses zlib to gzip the contents of http packages, so you need to install the zlib library on Centos.
yum -y install zlib zlib-devel
(4) OpenSSL is a powerful Secure Sockets Layer cryptographic library that includes key cryptographic algorithms, commonly used key and certificate encapsulation management functions, and ssl protocols, and provides rich applications for testing or other purposes.nginx supports not only the http protocol but also https (that is, http is transmitted over the ssl protocol), so the OpenSSL library needs to be installed.
yum -y install openssl openssl-devel
Note: The version of pcre installed by the yum installation method is relatively low, but will not affect use at all, but it is better to manually compile and install the latest stable version of openssl on the official website.
Check Base Dependency Packages
After the above dependent installations are complete, you can check whether each dependent installation was successful by following the 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 #Create nginx users to manage nginx programs tar -zxvf nginx-1.16.1.tar.gz #Unzip nginx source package cd nginx-1.16.1 #Note that because the openssl location is customized, make will cause errors and you will need to modify a conf file in the nginx source code #/usr/local/src/nginx-1.16.1/auto/lib/openssl/conf, modify method see Impression Notes #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 #Create a soft link to nginx
Installation instructions
--prefix=PATH #Set up the installation stiffness --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
Configuring environment variables
vim /etc/profile export PATH=/usr/local/nginx/sbin:$PATH source /etc/profile
Systemd Management
Create and edit a new/usr/lib/systemd/system/nginx.service file
vim /usr/lib/systemd/system/nginx.service
And add the following (the configuration here is based on the path where you installed Nginx, which is installed in the / 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
The default nginx.service configuration for nginx installed through yum is as follows and 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
Overloaded daemon
Reload systemd by executing the following command and scan for new or changed cells
systemctl daemon-reload
Set boot-up self-start
systemctl enable nginx.service # Set boot-up self-start systemctl disable nginx.service # Cancel Start-Up Self-Service
Common commands for Nginx service management
systemctl status nginx.service # View Nginx status systemctl start nginx.service # Open 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
This command allows you to query who is using port 80
lsof -i :80
If the command is not recognized, you need to install lsof
sudo yum -y install lsof
Install MySQL
Installation Dependency
(1)cmake is a compilation tool for the new version of MySQL and must be installed
sudo yum -y install ncurses-devel perl perl-devel autoconf
Install cmake without using yum, because the default cmake version is lower, which will cause later PHP installations to be too low to install. Because installing MySQL requires cmake, install the latest stable cmake directly here
cd /usr/local/src yum remove cmake wget https://github.com/Kitware/CMake/releases/download/v3.16.1/cmake-3.16.1.tar.gz tar -zxvf cmake-3.16.1.tar.gz cd cmake-3.16.1 ./configure \ --prefix=/usr/local/cmake make && make install cd /usr/local ln -s /usr/local/cmake/bin/cmake /usr/bin/cmake # ln -s /usr/local/cmake/share/cmake-3.16.1 /usr/share/cmake
Download boost
If you install mysql version 5.7 or higher, you need to install boost before you compile and install it, because a boots Library installation is required for higher versions of mysql to function properly.Otherwise CMake Error at cmake/boost.cmake:81 error will be reported
Switch to the / usr/local/src directory and download boost from there
MySQL 5.7.28 requires boost version 1.59, higher version does not apply MySQL 5.7.28
wget http://www.sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
Compile and install MySQL
# Add MySQL User useradd -s /sbin/nologin -M mysql # Switch to/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 #Unzip boost and move 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_MEMORY_STORAGE_ENGINE=1 \ -DWITH_READLINE=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 links cd /usr/local ln -s mysql-5.7.28 mysql
Configuring 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 the /var/lib directory
mkdir -p /var/lib/{mysql,mysql/data} touch /var/lib/mysql/mysqld.pid chown mysql.mysql -R /var/lib/mysql/
-
Modify/etc/my.cnf file
# Modify the /etc/my.cnf file to 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 means a secure password is generated by default, - initialize-insecure means 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=false
Overloaded daemon
Reload systemd by executing the following command 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
Start-Up Self-Starting
systemctl enable mysqld.service # Set boot-up self-start systemctl disable mysqld.service # Cancel boot-up self-start
Log in to MySQL
mysql -u root -p #No password required for first login, just return 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 libsqlite3x libsqlite3x-devel oniguruma oniguruma-devel
Upgrade libzip
yum remove libzip cd /usr/local/src wget https://libzip.org/download/libzip-1.5.2.tar.gz tar -zxvf libzip-1.5.2.tar.gz cd libzip-1.5.2 mkdir build cmake . make && make install echo '/usr/local/lib64 /usr/local/lib /usr/lib /usr/lib64'>>/etc/ld.so.conf ldconfig -v
Compile and Install PHP
cd /usr/local/src wget http://hk1.php.net/get/php-7.4.0.tar.gz/from/this/mirror -O php-7.4.0.tar.gz tar -zxvf php-7.4.0.tar.gz cd php-7.4.0 ./configure \ --prefix=/usr/local/php-7.4.0 \ --enable-fpm \ --with-fpm-user=nginx \ --with-fpm-group=nginx \ --with-zlib \ --enable-mysqlnd \ --enable-bcmath \ --enable-gd \ --with-pdo-mysql=mysqlnd \ --with-mysqli=mysqlnd \ --with-mysql-sock=/var/lib/mysql/mysql.sock \ --with-jpeg \ --with-freetype \ --with-iconv \ --with-openssl \ --with-curl \ --enable-mbstring \ --enable-static \ --enable-sockets \ --enable-xml make && make install
Compile parameters in detail
./configure \ --prefix=/usr/local/php-7.4.0 \ # Specify Installation Path --enable-fpm \ # Represents the activation of PHP-FPM mode services, that is, running PHP services in FactCGI mode. --with-fpm-user=nginx \ # The user specified for PHP-FPM process management is www, which is best unified with Nginx service users. --with-fpm-group=nginx \ # Specify that the PHP-FPM process management user group is www, which is best unified with the Nginx service user group. --with-zlib \ # Open zlib library support for http compressed transport --enable-mysqlnd \ --with-pdo-mysql=mysqlnd \ --with-mysqli=mysqlnd \ --with-mysql-sock=/var/lib/mysql/mysql.sock \ --with-gd \ # Support for opening gd Libraries --with-png-dir \ --with-jpeg-dir \ --with-freetype-dir \ --with-openssl \ # Open openssl for encrypted transmission --with-curl \ # Turn on support for curl browsing tools --enable-mbstring \ # Multi-byte, string support --enable-static \ # Generate Static Link Library --enable-zip \ # Open support for zip --enable-sockets \ # Turn on sockets support --enable-xml
To configure
cd /usr/local ln -s php-7.4.0 php cp /usr/local/src/php-7.4.0/php.ini-development /usr/local/php-7.4.0/lib/php.ini vim /usr/local/php/lib/php.ini date.timezone = PRC (About 954 lines) expose_php = Off #Avoid exposing PHP information to http headers (approximately 369 lines) display_errors = Off(Set production environment to off,The development environment is set to On,Easy to debug) //Note: Once dispaly_errors is set to off, the error logging path error_log = log/php-fpm.log needs to be turned on in php-fpm.conf 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 you specify parameters--with-mysql=mysqlnd and--with-pdo-mysql=mysqlnd when compiling PHP, you may encounter socket connection problems in production by adding commands in php.ini: pdo_mysql.default_socket=/usr/local/mysql/tmp/mysql.sock //It is best to specify the location of mysql.socket when compiling PHP: --with-mysql-sock=/usr/local/mysql/tmp/mysql.sock
Manage PHP-FPM
vim /usr/local/php/etc/php-fpm.conf pid = run/php-fpm.pid error_log = log/php-fpm.log #24 Line This is enabled when php.ini sets display_errors = Off Restart the server after setup Send a signal 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 Log: kill-USR1 `cat/usr/local/php/var/run/php-fpm.pid`
Configuring environment variables
vim /etc/profile export PATH=/usr/local/php/bin:$PATH source /etc/profile
Configure Systemd Service
In fact, the php-fpm.service file PHP has been configured for us as long as we copy to the specified location and enable it.
cp /usr/local/src/php-7.4.0/sapi/fpm/php-fpm.service /usr/lib/systemd/system/
And edit the file
vim /usr/lib/systemd/system/php-fpm.service
The php-fpm.service file contains 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
Overloaded daemon
Reload systemd by executing the following command and scan for new or changed cells
systemctl daemon-reload
Start-Up Self-Starting
systemctl enable php-fpm.service systemctl disable 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 that can modify itself, but it is too harmful because you are competing for CPU s # General Set CPU Number*Number of Kernels 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 { #Typically, the Nginx process and connection properties are configured #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-driven because epoll performs much better than other event-driven worker_connections 10240; #This refers to a subprocess that allows a maximum of 10240 connections } http { # This is the main segment for 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; # Sets the timeout for reading client request header data.The value here is 15, in seconds, which is an empirical reference value client_header_timeout 15; # Set timeout for reading client requests client_body_timeout 15; # Specify timeout for responding 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.0; gzip_comp_level 2; gzip_types text/css text/xml application/javascript; gzip_vary on; #include extra/gzip.config; #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; }
Install Redis
Compile Installation
# Unzip Source File tar -zxf redis-5.0.6.tar.gz # Switch to unzipped directory cd redis-5.0.6 # Compile Installation mkdir /usr/local/redis-5.0.6 make PREFIX=/usr/local/redis-5.0.6 install mkdir /usr/local/redis-5.0.6/etc cp redis.conf /usr/local/redis-5.0.6/etc/ # Create soft links cd /usr/local ln -sf redis-5.0.6 redis
Configuring environment variables
vim /etc/profile export PATH=/usr/local/redis/bin:$PATH source /etc/profile # Make changes take effect immediately
Configure Background Run
Let redis run as a background process
vim /usr/local/redis/etc/redis.conf # Daeonize no (instead) # Change to-> daemonize yes
Configure Systemd Service
Add a redis.service file at/etc/systemd/system/and add the following
[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
Overloaded daemon
Reload systemd by executing the following command and scan for new or changed cells
systemctl daemon-reload
Start-Up Self-Starting
systemctl enable redis.service
Start the redis service
systemctl start redis.service
Reference material
centos7 source compilation installation mysql5.7
Configuring mysql for systemd under linux7
Managing MySQL Server with systemd
centos7 7.3php Compile Installation
Compile and install php7.3 under centos7
cmake Installation Configuration and Getting Started Guide