Categories
Uncategorized

Under Linux environment to build PHP + Nginx

PHP + Nginx environment to build

Author: Wang Yuyang (Mirror) ^ _ ^

Reference article:

Nginx + PHP + MySQL Installation Reference

PHP source installation experience

PHP source environment to build the course FAQs

CentOS environment

Network configuration CentOS-7:

The CentOS (minimum installation) the default is not open network

    Start Network

vi open: / etc / sysconfig / network-scripts / ifcfg-ens33 file

The “ONBOOT: no” property changed to: “ONBOOT: yes”

    Network Service Restart

# sudo service network restart 
  • OK
# ip addr

Nginx services:

Installation dependencies

  • installation:

    # yum -y install build-essential
  • Installation: More dependencies

    # yum -y install gcc automake autoconf libtool make
  • Installation: g ++

    # yum -y install gcc gcc-c++

Installation PCRE library

The selected source directory ==> ftp download PCRE library ==> install the PCRE 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 package

Download Source Package zlib ==> 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 \

[注:当前所在目录一定要是/usr/local/src/nginx-1.16.1

--with-pcre=/usr/local/src/pcre-8.41 指的是pcre-8.42 的源码路径。

--with-zlib=/usr/local/src/zlib-1.2.11 指的是zlib-1.2.11 的源码路径。

--with-openssl=/usr/local/src/openssl-1.1.0g 指的是openssl-1.1.0k 的源码路径。]

# make && make install

nginx compiler option

is used to make compiled, it reads instructions from the Makefile, then compiled.

make install is used to install, it also reads instructions from the Makefile, mounted to the specified location.

configure command is used to detect the target feature your installation platform. It defines various aspects of the system, including the method of connecting process is allowed to use nginx, such as it detects that you are not a CC or GCC, is not required CC or GCC, it is a shell script, at the end of the implementation, it will Creating a Makefile. nginx configure command supports the following parameters:

    –prefix = * path * define a directory to store files on the server, nginx is the installation directory. Default / usr / local / nginx.

    –sbin-path = * path * provided nginx executable file path, default * prefix * / sbin / nginx.

    –conf-path = * path * nginx.conf disposed in the path of the configuration file. nginx allows the use of different profiles start from the command line -c option. The default is * prefix * / conf / nginx.conf.

    –pid-path = * path * Set nginx.pid file, the process ID of the main process of storage. After installation is complete, you can always change the file name, use the PID instruction in nginx.conf configuration file. By default, the file name is “ * prefix * / logs / nginx.pid.

    –error-log-path = * path * Set the main error, the name of warnings, and diagnostic files. After installation is complete, you can always change the name of the file, error_log nginx.conf instructions used in the configuration file. By default, the file name for the * prefix * / logs / error.log.

    –http-log-path = * path * set the name of the log file of the main HTTP server requests. After installation is complete, you can always change the file name, access_log nginx.conf instructions used in the configuration file. By default, the file name for the * prefix * / logs / access.log.

    –user = * name * Set user nginx worker processes. After installation, user instructions name can be changed at any time nginx.conf used in the configuration file. The default user name is nobody.

    –group = * name * nginx worker processes setting the user group. After installation, user instructions name can be changed at any time nginx.conf used in the configuration file. The default is non-privileged users.

    –with-select_module –without-select_module enabled or disabled to allow the server to build a module using the select () method. The module will automatically establish if the platform does not support kqueue, epoll, rtsig or / dev / poll.

    –with-poll_module –without-poll_module enabled or disabled to allow the server to build a module using the poll () method. The module will automatically establish if the platform does not support kqueue, epoll, rtsig or / dev / poll.

    –without-http_gzip_module – responsive module without compiling compressed HTTP server. Compile and run this module requires zlib library.

    –without-http_rewrite_module not compile rewrite module. Compile and run this module requires PCRE library support.

    –without-http_proxy_module – http_proxy not compiled module.

    –with-http_ssl_module – using the https protocol module. By default, the module is not constructed. Build and run this module OpenSSL libraries are required.

    –with-pcre = * path * – source path disposed PCRE library. Source PCRE library (version 4.4 – 8.30) to download and extract from PCRE website. The rest of the work is the Nginx ./ configure and make to complete. Regular expressions used in the location command and ngx_http_rewrite_module module.

    –with-pcre-jit – compiler PCRE comprising “just-in-time compilation” (1.1.12 in, pcre_jit instructions).

    –with-zlib = * path * – source path disposed zlib library. To download zlib (1.1.3 – 1.2.5) and unzip. The rest of the work is the Nginx ./ configure and make complete. ngx_http_gzip_module module requires zlib.

    –with-cc-opt = * parameters * – to provide an additional parameter is added to the CFLAGS variable. For example, when you use the PCRE library on FreeBSD requires: – with-cc-opt = “- I / usr / local / include .. If you need need to increase the select () the number of file support: – with-cc -opt = “- D FD_SETSIZE = 2048”.

    –with-ld-opt = * parameters * – setting additional parameters will be used during the link. For example, when using the system PCRE library under FreeBSD, should specify: – with-ld-opt = “- L / usr / local / lib”.

After installation is complete, according to the parameters of the installation, the installation in the startup directory / usr / local / nginx

[[email protected] nginx]# ls -l
总用量 76
drwxr-xr-x. 2 root root 4096 9月   8 09:46 conf
-rw-r--r--. 1 root root 1077 9月   8 10:34 fastcgi.conf
-rw-r--r--. 1 root root 1077 9月   8 10:34 fastcgi.conf.default
-rw-r--r--. 1 root root 1007 9月   8 10:34 fastcgi_params
-rw-r--r--. 1 root root 1007 9月   8 10:34 fastcgi_params.default
drwxr-xr-x. 2 root root   40 9月   8 09:46 html
-rw-r--r--. 1 root root 2837 9月   8 10:34 koi-utf
-rw-r--r--. 1 root root 2223 9月   8 10:34 koi-win
drwxr-xr-x. 2 root root   41 9月   8 10:37 logs
-rw-r--r--. 1 root root 5231 9月   8 10:34 mime.types
-rw-r--r--. 1 root root 5231 9月   8 10:34 mime.types.default
-rw-r--r--. 1 root root 2656 9月   8 10:34 nginx.conf
-rw-r--r--. 1 root root 2656 9月   8 10:34 nginx.conf.default
-rw-r--r--. 1 root root    6 9月   8 10:37 nginx.pid
drwxr-xr-x. 2 root root   36 9月   8 10:34 sbin
-rw-r--r--. 1 root root  636 9月   8 10:34 scgi_params
-rw-r--r--. 1 root root  636 9月   8 10:34 scgi_params.default
-rw-r--r--. 1 root root  664 9月   8 10:34 uwsgi_params
-rw-r--r--. 1 root root  664 9月   8 10:34 uwsgi_params.default
-rw-r--r--. 1 root root 3610 9月   8 10:34 win-utf
[[email protected] nginx]# pwd
/usr/local/nginx

Start Nginx services:

Since the firewall does not develop CentOS-7 ports, so the local tests, you can disable the firewall port 80 or allow the development of

CentOS firewall

# systemctl status firewalld     ==> 防火墙状态
# systemctl start firewalld      ==> 开启防火墙
# systemctl stop firewalld       ==> 关闭防火墙
# systemctl restart firewalld    ==> 重启防火墙
# firewall-cmd --reload          ==> 防火墙重载
# firewall-cmd --permanent --zone=public --add-port=80/tcp
    permanent: 永久有效
    zone:作用域
    --add-port=80/tcp:添加-端口=端口/通信协议

Turn off the firewall or open ports after the service can start nginx

Service starts

[[email protected] 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    
[[email protected] 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()

Port View network status using netstat, whether there are 80 occupied port services; achieve nginx nginx service starts by calling the startup directory

Figure: Start Success

Nginx Maintenance Services

To avoid manually start each boot, you can use the command script, registration services, such as boot from the start

Create a startup command script nginx

`vi /etc/init.d/nginx`

Insert the following content, pay attention to modify the PATH and NAME field, matching its own installation path (this is the copy from the Internet)

`#! /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 execute permissions

`chmod a+x /etc/init.d/nginx`

Register as a service

`chkconfig --add nginx`

Set boot

`chkconfig nginx ``on`

Restart to see whether the service starts automatically nginx

`shutdown -h 0 -r``ss -apn|grep nginx`

Nginx perform service stop / start / re-read configuration files

`#启动nginx服务``systemctl start nginx.service``#停止nginx服务``systemctl stop nginx.service``#重启nginx服务``systemctl restart nginx.service``#重新读取nginx配置(这个最常用, 不用停止nginx服务就能使修改的配置生效)``systemctl reload nginx.service`

MySQL installation:

Linux yum-rpm

[[email protected] src]# rpm -qa |grep mysql

[[email protected] src]# wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm

[[email protected] src]# rpm -ivh mysql-community-release-el7-5.noarch.rpm

[[email protected] src]# yum dpdate

[[email protected] src]# yum install mysql-server 


[[email protected] src]# chown mysql:mysql -R /var/lib/mysql   设置权限

[[email protected] src]# mysqld --initialize 初始化mysql

[[email protected] src]# systemctl start mysqld  启动mysql

PHP environment:

Compile and install php-fpm

PHP-FPM is a PHP FastCGI manager, is only for PHP

    Installation dependencies

# 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 install 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 execute configure, occur error: Please reinstall the libzip distribution So I deleted the old version of libzip “yum remove libzip”, after downloading libzip source package for local execution configure + make && make install

Libzip execution of 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

PHP install a new problem: error when installing run configure

configure: error: off_t undefined; check your library configuration

Solution:

vim /etc/ld.so.conf 
#添加如下几行
/usr/local/lib64
/usr/local/lib
/usr/lib
/usr/lib64 
#保存退出
:wq
ldconfig -v # 执行命令,使之生效

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 refer to Baidu / Google solve (common php installation program)!

So far! The basic flow PHP-fpm installation is over, the next step requires a configuration file

User Profiles

    Provide profiles for php: php.ini

# cp php.ini-production /usr/local/php/lib/php.ini

    Provide a profile for php-fpm

# cd /usr/local/php
# cp etc/php.fpm.conf.default etc/php-fpm.conf
# vi etc/php-fpm.conf

vi open php-fpm.conf file:

The tail of the index file; modified to the actual directory

include=/usr/local/php/etc/php-fpm.d/*.conf

Adding users and groups:

useradd mirror
groupadd -g mirror mirror

By default, there is a “www .conf.defalut” user profile under 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 “www.conf” file of the user and group value; add users and groups

user = mirror
group = mirror

    Start php-fpm Service

# /usr/local/php/sbin/php-fpm
# ps aux | grep php-fpm [验证服务启动]
# netstat -tln | grep 9000 [验证网络端口是否使用]
[[email protected] /]# 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
[[email protected] /]# 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)

[[email protected] 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;
    #    }
    #}
}

Php modification location and a rear end block configuration server requesting 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;
        #}
    }

Added index.php Home location configuration block

php php-fpm request and a rear end communicating module, configure location ~ .php $ configuration block

root: root directory php configuration program files

*** modify the configuration file of the first line: “user” attribute for users ** We previously configured expressed permission nginx

So far! Our Nginx and php environment to complete a simple configuration!

Done

Start steps:

  • Start Nginx Service

    # /usr/local/nginx/sbin/nginx
  • Start php-fpm Service

    # /usr/local/php/sbin/php-fpm
  • Start mysql service

    # systemctl start mysqld

phpinfo():

Add a php file in the html directory Nginx: “index.php”

Test Database Connection:

Writing a database connection behavior php file: “mysql.php”

Php and mysql connection between operations rely on “mysqli”

So far! PHP + Nginx + MySQL environment to complete the basic set up!

Leave a Reply