PHP+Nginx Environment Setup
Author: Mirror ^^
Reference article:
Nginx+PHP+MySQL Installation Reference
CentOS environment
Configure the CentOS-7 network:
CentOS (minimum installation) does not open the network by default
- Start the network
vi Open: /etc/sysconfig/network-scripts/ifcfg-ens33 file
Modify the "ONBOOT:no" property to "ONBOOT:yes"
- service network restart
# sudo service network restart
- OK:
# ip addr
Nginx service:
Install Dependent Packages
-
Installation:
# yum -y install build-essential
-
Installation: More dependent packages
# yum -y install gcc automake autoconf libtool make
-
Installation: g++.
# yum -y install gcc gcc-c++
Install the PCR E Library
Selected Source Directory==> FTP Download PCR E Library==> Install PCR E Library
# cd /usr/local/src # wget ftp://ftp.pcre.org/pub/pcre/pcre-8.42.tar.gz # tar -zxvf pcre-8.42.tar.gz # cd /pcre-8.42 # ./configure # make && make install
Install zlib source packages
Download zlib source package==>Install zlib package
# cd /usr/local/src # wget http://zlib.net/zlib-1.2.11.tar.gz # tar -zxvf zlib-1.2.11.tar.gz # cd zlib-1.2.11 # ./configure # make && make install
Install openssl source package
# cd /usr/local/src # wget https://www.openssl.org/source/openssl-1.1.0k.tar.gz # tar -zxvf openssl-1.1.0k.tar.gz # cd ./openssl-1.1.0k # ./configure # make && make install
Install Nginx
# cd /usr/local/src # wget http://nginx.org/download/nginx-1.16.1.tar.gz # tar -zxvf nginx-1.16.1.tar.gz # cd nginx-1.16.1 # groupadd -r nginx # useradd -r -g nginx nginx # ./configure \ --prefix=/usr/local/nginx \ --sbin-path=/usr/local/nginx/sbin/nginx \ --conf-path=/usr/local/nginx/nginx.conf \ --pid-path=/usr/local/nginx/nginx.pid \ --user=nginx \ --group=nginx \ --with-http_ssl_module \ --with-http_flv_module \ --with-http_mp4_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --http-client-body-temp-path=/var/tmp/nginx/client/ \ --http-proxy-temp-path=/var/tmp/nginx/proxy/ \ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \ --http-scgi-temp-path=/var/tmp/nginx/scgi \ --with-pcre=/usr/local/src/pcre-8.42 \ --with-zlib=/usr/local/src/zlib-1.2.11 \ --with-openssl=/usr/local/src/openssl-1.1.0k \ [Note: The current directory must be/usr/local/src/nginx-1.16.1 --with-pcre=/usr/local/src/pcre-8.41 Refer to pcre-8.42 Source path of. --with-zlib=/usr/local/src/zlib-1.2.11 Refer to zlib-1.2.11 Source path of. --with-openssl=/usr/local/src/openssl-1.1.0g Refer to openssl-1.1.0k Source path of.] # make && make install
nginx compilation options
make is compiled by reading instructions from Makefile and compiling them.
Makeinstall is used for installation and it also reads instructions from Makefile to install to the specified location.
The configure command is used to detect the target characteristics of your installation platform.It defines all aspects of the system, including how nginx handles allowed connections, such as whether it detects you have a CC or a GCC and does not require a CC or a GCC. It is a shell script that creates a Makefile file at the end of execution.The configure command for nginx supports the following parameters:
- --prefix=*path* defines a directory for files on the server, which is the installation directory for nginx.By default, /usr/local/nginx is used.
- --sbin-path=*path*Sets the path to the nginx executable, defaulting to *prefix*/sbin/nginx.
- --conf-path=*path*Sets the path to the nginx.conf configuration file.Nginx allows you to start using a different configuration file, using the -c option on the command line.Default is *prefix*/conf/nginx.conf.
- --pid-path=*path*Sets the nginx.pid file to store the process number of the main process.After installation, you can change the file name at any time and use the PID command in the nginx.conf configuration file.By default, the file name is ``*prefix*/logs/nginx.pid.
- --error-log-path=*path*Sets the name of the primary error, warning, and diagnostic file.After installation, you can change the file name at any time, using the error_log directive in the nginx.conf configuration file.By default, the file name is *prefix*/logs/error.log.
- --http-log-path=*path*Sets the name of the log file of the HTTP server for the master request.After installation, you can change the file name at any time, using the access_log directive in the nginx.conf configuration file.By default, the file name is *prefix*/logs/access.log.
- --user=*name*Sets the user for the nginx worker process.The user directive used in the nginx.conf configuration file for names that can be changed at any time after the installation is complete.The default user name is nobody.
- --group=*name*Sets the user group for the nginx worker process.The user directive used in the nginx.conf configuration file for names that can be changed at any time after the installation is complete.The default is an unprivileged user.
- --with-select_module--without-select_module Enables or disables building a module to allow the server to use the select() method.The module will be built automatically if the platform does not support kqueue, epoll, rtsig or/dev/poll.
- --with-poll_module--without-poll_module Enables or disables building a module to allow the server to use the poll() method.The module will be built automatically if the platform does not support kqueue, epoll, rtsig or/dev/poll.
- --without-http_gzip_module - does not compile the response module of a compressed HTTP server.The zlib library is required to compile and run this module.
- --without-http_rewrite_module does not compile rewrite modules.Compiling and running this module requires support from the PCR E library.
- --without-http_proxy_module - Does not compile the http_proxy module.
- --with-http_ssl_module -- uses the https protocol module.By default, the module is not built.It is necessary to establish and run an OpenSSL library for this module.
- --with-pcre=*path*--Sets the source path of the PCR E library.The source code of the PCR E library (version 4.4 - 8.30) needs to be downloaded and decompressed from the PCR E website.The rest is done by Nginx. / configure and make.Regular expressions are used in the location directive and in the ngx_http_rewrite_module.
- --with-pcre-jit - Compile PCR E contains "just-in-time compilation" (pcre_jit directive, 1.1.12).
- --with-zlib=*path*--The source path of the set zlib library.To download the decompression from zlib (version 1.1.3 - 1.2.5).The rest of the work is done by Nginx. / configure and make.The ngx_http_gzip_module requires zlib.
- --with-cc-opt=*parameters*--Setting additional parameters will be added to the CFLAGS variable.For example, when you use the PCR E library on FreeBSD, you need to use: --with-cc-opt="-I/usr/local/include".If necessary, increase the number of files supported by select(): --with-cc-opt="-D FD_SETSIZE=2048".
- --with-ld-opt=*parameters*--Sets additional parameters that will be used during the link.For example, when using the system's PCR E Library under FreeBSD, you should specify: --with-ld-opt="-L/usr/local/lib".
After installation, follow the installation parameters and install the startup directory at/usr/local/nginx
[root@localhost nginx]# ls -l //Total usage 76 drwxr-xr-x. 2 root root 4096 9 Month 8 09:46 conf -rw-r--r--. 1 root root 1077 9 August 10:34 fastcgi.conf -rw-r--r--. 1 root root 1077 9 August 10:34 fastcgi.conf.default -rw-r--r--. 1 root root 1007 9 August 10:34 fastcgi_params -rw-r--r--. 1 root root 1007 9 August 10:34 fastcgi_params.default drwxr-xr-x. 2 root root 40 9 Month 8 09:46 html -rw-r--r--. 1 root root 2837 9 August 10:34 koi-utf -rw-r--r--. 1 root root 2223 9 August 10:34 koi-win drwxr-xr-x. 2 root root 41 9 August 10:37 logs -rw-r--r--. 1 root root 5231 9 August 10:34 mime.types -rw-r--r--. 1 root root 5231 9 August 10:34 mime.types.default -rw-r--r--. 1 root root 2656 9 August 10:34 nginx.conf -rw-r--r--. 1 root root 2656 9 August 10:34 nginx.conf.default -rw-r--r--. 1 root root 6 9 August 10:37 nginx.pid drwxr-xr-x. 2 root root 36 9 August 10:34 sbin -rw-r--r--. 1 root root 636 9 August 10:34 scgi_params -rw-r--r--. 1 root root 636 9 August 10:34 scgi_params.default -rw-r--r--. 1 root root 664 9 August 10:34 uwsgi_params -rw-r--r--. 1 root root 664 9 August 10:34 uwsgi_params.default -rw-r--r--. 1 root root 3610 9 August 10:34 win-utf [root@localhost nginx]# pwd /usr/local/nginx
Start the Nginx service:
Because the CentOS-7 firewall does not open ports, you can choose to close the firewall or allow port 80 to be developed in local tests
CentOS Firewall
# Systemctl status firewalld ==>firewall status # Systemctl start firewalld ==>Turn on the firewall # Systemctl stop firewalld ==>Close firewall # Systemctl restart firewalld ==>Restart the firewall # Firewall-cmd --reload ==>Firewall overload # firewall-cmd --permanent --zone=public --add-port=80/tcp permanent: Permanent validity zone: Scope --add-port=80/tcp: Add to-port=port/communication protocol
nginx service can be started after opening the port or closing the firewall
Service Start
[root@localhost nginx]# netstat -ano | grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN off (0.00/0/0) unix 3 [ ] STREAM CONNECTED 80900 unix 3 [ ] STREAM CONNECTED 80899 [root@localhost nginx]# /usr/local/nginx/sbin/nginx nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] still could not bind()
View port network status through netstat and see if any services occupy 80 ports; Start nginx service by calling the startup directory of nginx
Figure: Start successfully
Nginx Service Maintenance
To avoid starting manually at each boot, you can use command scripts, register services, boot-up self-start, and so on.
Create nginx startup command script
`vi /etc/init.d/nginx`
Insert the following, note that modify the PATH and NAME fields to match your installation path (this section was copied from the web)
`#! /bin/bash``# chkconfig: - 85 15``PATH=/usr/local/nginx``DESC=``"nginx daemon"``NAME=nginx``DAEMON=$PATH/sbin/$NAME``CONFIGFILE=$PATH/$NAME.conf``PIDFILE=$PATH/logs/$NAME.pid``SCRIPTNAME=/etc/init.d/$NAME``set` `-e``[ -x ``"$DAEMON"` `] || exit 0``do_start() {``$DAEMON -c $CONFIGFILE || echo -n ``"nginx already running"``}``do_stop() {``$DAEMON -s stop || echo -n ``"nginx not running"``}``do_reload() {``$DAEMON -s reload || echo -n ``"nginx can't reload"``}``case` `"$1"` `in``start)``echo -n ``"Starting $DESC: $NAME"``do_start``echo ``"."``;;``stop)``echo -n ``"Stopping $DESC: $NAME"``do_stop``echo ``"."``;;``reload|graceful)``echo -n ``"Reloading $DESC configuration..."``do_reload``echo ``"."``;;``restart)``echo -n ``"Restarting $DESC: $NAME"``do_stop``do_start``echo ``"."``;;``*)``echo ``"Usage: $SCRIPTNAME {start|stop|reload|restart}"` `>&2``exit 3``;;``esac``exit 0`
Set Execution Permissions
`chmod a+x /etc/init.d/nginx`
Register as Service
`chkconfig --add nginx`
Set up boot-up
`chkconfig nginx ``on`
Restart to see if the nginx service starts automatically
`shutdown -h 0 -r``ss -apn|grep nginx`
Perform stop/start/re-read profile operations on nginx service
`#Start the nginx service```systemctl start nginx.service`` #Stop the nginx service````systemctl stop nginx.service`` `#Restart the nginx service`` ``systemctl restart nginx.service`` `#Re read the nginx configuration (this is the most common one that takes effect without stopping the nginx service)``systemctl reload nginx.service`
MySQL installation:
Linux yum-rpm
[root@localhost src]# rpm -qa |grep mysql [root@localhost src]# wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm [root@localhost src]# rpm -ivh mysql-community-release-el7-5.noarch.rpm [root@localhost src]# yum dpdate [root@localhost src]# yum install mysql-server [root@localhost src]# Chown mysql:mysql-R/var/lib/mysql Set permissions [root@localhost src]# mysqld --initialize initializes mysql [root@localhost src]# systemctl start mysqld start mysql
PHP environment:
Compile and install php-fpm
PHP-FPM is a PHP FastCGI manager and is for PHP only
- Install Dependent Packages
# yum -y install libmcrypt-devel mhash-devel libxslt-devel \ libjpeg libjpeg-devel libpng libpng-dvevl freetype freetype-devel libxml2 libxml2-devel \ zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel \ ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel \ krb5 krb5-devel libidn libidn-devel openssl openssl-devel # yum -y install libzip # wget http://103.40.19.56/lnmp/libzip-1.3.2.tar.gz # tar -zxvf libzip-1.3.2.tar.gz cd libzip-1.3.2 # ./configure # make && make install # wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/attic/libmcrypt/libmcrypt-2.5.7.tar.gz # tar -zxvf libmcrypt-2.5.7.tar.gz # cd libmcrypt-2.5.7 # ./configure –prefix=/usr/local # make && make install
- Source installation PHP:
# cd /usr/local/src # wget http://php.net/get/php-5.6.27.tar.gz/from/a/mirror # tar -zxvf php-5.6.27.tar.gz # ./configure --prefix=/usr/local/php --enable-fpm --with-mcrypt \ --enable-mbstring --enable-pdo --with-curl --disable-debug --disable-rpath \ --enable-inline-optimization --with-bz2 --with-zlib --enable-sockets \ --enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex \ --with-mhash --enable-zip --with-pcre-regex --with-pdo-mysql --with-mysqli \ --with-gd --with-jpeg-dir --with-freetype-dir --enable-calendar\ # make && make install
When I executed configure, an error occurred: Please reinstall the libzip distribution, so I deleted the old version of libzip "yum remove libzip", and then downloaded the libzip source package to perform configure+make&install locally
Perform the libzip installation:
# wget https://libzip.org/download/libzip-1.5.2.tar.gz # tar -zxf libzip-1.2.0.tar.gz # cd libzip-1.2.0 # ./configure # make && make install
New PHP Installation Issue: Error while installing configure
configure: error: off_t undefined; check your library configuration
Solution:
vim /etc/ld.so.conf #Add the following lines /usr/local/lib64 /usr/local/lib /usr/lib /usr/lib64 #Save Exit :wq ldconfig -v # Execute the command for it to take effect
Error: configure: WARNING: unrecognized options: --with-mcrypt
Solution: PHP 7.2+ does not support --with-mcrypt; --enable-gd-native-ttf
# ./configure --prefix=/usr/local/php --enable-fpm \ --enable-mbstring --enable-pdo --with-curl --disable-debug --disable-rpath \ --enable-inline-optimization --with-bz2 --with-zlib --enable-sockets \ --enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex \ --with-mhash --enable-zip --with-pcre-regex --with-pdo-mysql --with-mysqli \ --with-gd --with-jpeg-dir --with-freetype-dir --enable-calendar\
Other errors can be referred to Baidu/Google Solution (common php installations)!
So far!The basic process of installing PHP-fpm is over and the configuration file is needed next
User Profile
- Provide a configuration file for php: php.ini
# cp php.ini-production /usr/local/php/lib/php.ini
- Provide a configuration file for php-fpm
# cd /usr/local/php # cp etc/php.fpm.conf.default etc/php-fpm.conf # vi etc/php-fpm.conf
vi Open the php-fpm.conf file:
Index the end of the file; change to actual directory
include=/usr/local/php/etc/php-fpm.d/*.conf
Add users and groups:
useradd mirror groupadd -g mirror mirror
By default, there is a "www.conf.defalut" user profile in the etc/php-fpm.d/directory
# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf # vi /usr/local/php/etc/php-fpm.d/www.conf
Modify the value s of users and groups in the "www.conf" file; add users and groups
user = mirror group = mirror
- Start php-fpm service
# /usr/local/php/sbin/php-fpm # ps aux | grep php-fpm [Validation Service Start] # Netstat-tln | grep 9000 [Verify network port use]
[root@localhost /]# ps aux | grep php-fpm root 41831 0.0 0.3 221264 6220 ? Ss 08:54 0:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf) mirror 41832 0.0 0.2 221264 5748 ? S 08:54 0:00 php-fpm: pool www mirror 41833 0.0 0.2 221264 5748 ? S 08:54 0:00 php-fpm: pool www root 41835 0.0 0.0 110292 916 pts/0 R+ 08:54 0:00 grep --color=auto php-fpm [root@localhost /]# netstat -tln | grep 9000 tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN
So far!php-fpm service started successfully!
Nginx+PHP Environment Configuration
- Open nginx.conf (nginx configuration file)
[root@localhost nginx]# vi ./nginx.conf #user nobody; 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 { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #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; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #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$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$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; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
Modify the location and php backend request configuration blocks in the server configuration block
server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm index.php } #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$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /scripts$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; #} }
Add index.php home page in location configuration block
The PHP request communicates with the back-end php-fpm module, requiring configuration of the location ~.php$configuration block
Root: root directory for configuring php program files
***Modify the first line of the configuration file:'user'property is the user we previously configured**, indicating the permissions of nginx
So far!Our Nginx and php environments are simple to configure!
Be accomplished
Start-up steps:
-
Start the Nginx service
# /usr/local/nginx/sbin/nginx
-
Start php-fpm service
# /usr/local/php/sbin/php-fpm
-
service mysql start
# systemctl start mysqld
phpinfo():
Add a PHP file to the directory html for Nginx: "index.php"
<?php phpinfo(); ?>
Test the database connection:
Write a PHP file that connects to the database behavior: "mysql.php"
The connection between php and mysql depends on "mysqli"
<?php $conn = mysqli_connect("127.0.0.1","root","926498"); if(! $conn ) { echo "connection failed".mysqli_connect_error(); } else { echo "Connection Successful"; } ?>
So far!PHP+Nginx+MySQL environment completed the basic setup!