Compile and install nginx1.9.7+php7.0.0 server environment

Disclaimer: This article is just a copy and backup of @Bowen, and the copyright belongs to the original author.

Original address→  

Compile and install nginx1.9.7+php7.0.0 server environment  

Compile and install nginx

Compiler Environment

To install in linux using make, you need to ensure that linux already has a relatively OK compilation environment, such as gcc and other compilation tools. Generally speaking, the server provider has integrated these software by default in the installed system, but for the sake of insurance, we still run all these dependent packages in some relatively basic ways to prevent errors in subsequent compilation. .

$ yum -y install gcc gcc-c++ autoconf automake libtool make cmake
$ yum -y install zlib zlib-devel openssl openssl-devel pcre-devel

zlib: Provides gzip module for nginx, requires zlib library support

openssl: provides ssl capabilities for nginx

pcre: rewrite function to support address rewrite

Make sure pcre is installed correctly

In the above basic environment installation, we have installed pcre on yum, but there is no pcre installation package on some servers, so we can compile and install pcre by downloading the tar package.

Search for pcre, enter its official website, find the latest version, copy the download link of tar.gz, and perform the following operations:

$ wget http://pcre/xxx/xxx/xxx最新版xxx.tar.gz
$ tar zxvf xxxx.tar.gz
$ cd xxxx
$ ./configure
$ make install

This ensures that pcre is installed.

Create a user and group for running nginx

We create a new user and user group to run nginx, which can separate nginx from root and ensure that nginx does not have root privileges. However, we do not want nginx to become a real user who can log in to perform operations remotely, so we do not create a home directory for it. When useradd, use the -M parameter:

$ groupadd nginx
$ useradd -g nginx -M nginx

The -g parameter specifies a group for the nginx user. The -M parameter ensures that it does not automatically generate the home directory.

But after the above user is created, the nginx user can log in to the server by setting a password. This is not what we want. We disable its ssh login permission. It is also very convenient to prohibit user login, only need to modify the relevant user and user in the configuration file. User group information.

$ vi /etc/passwd

Find nginx and change the following /bin/bashto /sbin/nologin.

OK, the user is processed.

Compile and install Nginx

So much has been said before, but the key points have not been mentioned yet. Next, let's compile and install nginx. First enter the official website of nginx, find the link of the latest version of the tar.gz package, copy the link, and then perform the following actions:

$ wget http://xxxxxxxxxx/nginx1.7.x.tar.gz
$ tar zxvf nginx1.7.x.tar.gz
$ cd nginx1.7.x

Next, we need to execute ./configure. Different developers have different habits. For users who are just getting started, they do not like the troublesome configuration. They always hope that the default is the best, but the actual situation is exactly the opposite. On the road of linux, please love tossing, and since you choose to compile and install, please love make.

I hope to install the software under /user/local, one software per directory, and it will be easier to clean up when uninstalling in the future. The software directory is also classified and managed according to the Linux directory form, using conf, etc, sbin, run, etc., so my final ./configure configuration is as follows:

$ ./configure --prefix=/usr/local/nginx \
--pid-path=/usr/local/nginx/run/nginx.pid \
--with-http_ssl_module \
--user=nginx \
 --group=nginx \
--with-pcre \
--without-mail_pop3_module \
--without-mail_imap_module \
--without-mail_smtp_module

The last three are to disable nginx as a mail proxy server. I generally only use the server as a website or database server, so I disable them here. If you want to build a mail server, then you should read the tutorial on building a mail server with nginx .

You can read the results of ./configure carefully to see if there are any errors, or whether the loaded modules are complete. If everything is OK, then continue down. If it feels wrong, you can read it carefully with ./configure --help .

$ make
$ make install

There is a little trick in the make place. If the server is dual-core, you can use -j2 to specify dual-core compilation, and -j4 represents 4-core compilation.

The installation is over here, but there is nothing to do after the installation, nginx is not running yet, you can go to see the results of the installation first, and run the nginx server:

$ cd /usr/local/nginx
$ ls
$ sbin/nginx

This is up and running, visit your server ip and see if you can see the nginx welcome page. (Don't let other software occupy port 80) By default, web files are placed under /usr/local/nginx/html, which is not in line with our usage habits. This needs to be modified by modifying the nginx configuration file, but even if it is not modified, we It can also be used normally, we will not explain the configuration of nginx in detail.

Loading nginx service

However, the software compiled and installed by make is not like the service installed by yum. The familiar service command does not work, otherwise you can try it with service nginx restart. This is because the service calls the program in the /etc/ini.d/ directory to complete, and the nginx program does not exist in this directory. So how to restart nginx at this time? Do as follows:

$ /usr/local/nginx/sbin/nginx -s reload

This operation can reload the nginx configuration file, which is equivalent to restarting (when the configuration file fails, it will not restart). If you must restart the entire service, it can only be done by killing the nginx process and then running the program.

However, in order to use the familiar service operation, here is a program , put it in the /etc/ini.d/ directory, and execute:

$ chmod +x /etc/init.d/nginx 
$ chkconfig --add nginx
$ chkconfig nginx on

In this way, nginx can be operated by methods such as service nginx restart. You can download the program and study it briefly. If your nginx installation path is different from mine, modify the variable settings at the beginning of the program.

Compilation and installation of PHP7

dependent environment

The convention is to resolve some compiled dependencies first

$ yum -y install libxml2 libxml2-devel openssl openssl-devel curl-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel

Compile and install php7

In the article " Lamp upgrades php to php7 ", I talked about how to upgrade php to 7 in the apache server environment, and in the nginx environment, we no longer use the php apxs module, but directly use the php-fpm module. Next, let's try to compile and install php7.

First download php7 from the official website and unzip it. Since the above article already has the relevant steps, I won't go into too much detail:

$ wget http://am1.php.net/get/php-7.0.0.tar.gz/from/this/mirror
$ tar zvxf php-7.0.0.tar.gz
$ cd php-7.0.0

Next, we need to configure the configuration before compilation. Unlike the previous article, we do not provide apxs parameters. Instead, we provide php-fpm related parameters:

 

$ ./configure --prefix=/usr/local/php7 \
--with-config-file-path=/usr/local/php7/etc \
--with-config-file-scan-dir=/usr/local/php7/etc/php.d \
--with-mcrypt=/usr/include \
--enable-mysqlnd \
--with-mysqli \
--with-pdo-mysql \
--enable-fpm \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--with-gd \
--with-iconv \
--with-zlib \
--enable-xml \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--enable-mbregex \
--enable-mbstring \
--enable-ftp \
--enable-gd-native-ttf \
--with-openssl \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--without-pear \
--with-gettext \
--enable-session \
--with-curl \
--with-jpeg-dir \
--with-freetype-dir \
--enable-opcache

 

The checking has been unsuccessful, and the prompt configure: error: mcrypt.h not found. Please reinstall libmcrypt.

But my computer has mcrypt.h  , and yum  libmcrypt prompt has been installed.

所以我删掉了 --with-config-file-path=/usr/local/php7/etc  --with-config-file-scan-dir=/usr/local/php7/etc/php.d  --with-mcrypt=/usr/local/sinasrv2/include/mutils

终于编译通过,可以安装了。

 

方便复制:

 ./configure --prefix=/usr/local/php7   --enable-mysqlnd --with-mysqli  --with-pdo-mysql  --enable-fpm  --with-fpm-user=nginx --with-fpm-group=nginx --with-gd  --with-iconv  --with-zlib --enable-xml  --enable-shmop  --enable-sysvsem  --enable-inline-optimization --enable-mbregex --enable-mbstring --enable-ftp  --enable-gd-native-ttf  --with-openssl  --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext  --enable-session --with-curl --with-jpeg-dir --with-freetype-dir --enable-opcache

 

 

配置无误后执行:

$ make
$ make install

同样可以使用-j2哦。如果安装成功,OK,那么php7的安装就OK了。

调整php配置

默认安装好之后,你会发现/usr/local/php7/etc下面没有php.ini文件,这个去哪里要呢?在php7的源码安装包都有。

$ cd /usr/src/php-7.0.0/
$ ls

可以看到有两个php.ini-xxx文件,我们可以分别vi打开来看下,一个是产品模式,一个是开发模式。

$ cp php.ini-production /usr/local/php7/etc/php.ini
$ vi /usr/local/php7/etc/php.ini

可以看到php的配置。本文就不做过多的配置解释了。

启用php-fpm服务

上面我们在编译php7的时候,已经将fpm模块编译了,那么接下来,我们要启用php-fpm。但是默认情况下它的配置文件和服务都没有启用,所以要我们自己来搞定。

搞定配置文件:

$ cd /usr/local/php7/etc
$ mv php-fpm.conf.default php-fpm.conf
$ mv php-fpm.d/www.conf.default php-fpm.d/www.conf

php-fpm的具体配置我们也不做深入去详解,因为在编译之前./configure的时候,我们都已经确定了一些配置,比如运行fpm的用户和用户组之类的,所以默认配置应该不会存在路径问题和权限问题。

搞定php-fpm的服务载入:

就像上面的nginx一样,我们希望使用service php-fpm start|stop|restart这些操作来实现服务的重启,但没有像nginx那么复杂,php编译好之后,给我们提供了一个php-fpm的程序,不需要我再编写分享了。这个文件放在php编译源码目录中:

$ cd /usr/src/php-7.0.0/sapi/fpm
$ ls
$ cp init.d.php-fpm /etc/init.d/php-fpm
$ chmod +x /etc/init.d/php-fpm
$ chkconfig --add php-fpm
$ chkconfig php-fpm on

通过上面这个操作,我们就可以使用sevice php-fpm start来启用php-fpm了。用ps -ef | grep php-fpm看看进程吧。

nginx代理php实现访问

通过上面的操作,nginx和php-fpm服务都被我们跑起来了,但是php-fpm走的是127.0.0.1:9000,外网是无法访问的,而且我们也不可能直接通过php-fpm给外网提供服务,我们用nginx去代理9000端口执行php。

实际上这个过程只需要对nginx进行配置即可,fpm已经在后台运行了,我们需要在nginx的配置文件中增加代理的规则,即可让用户在访问80端口,请求php的时候,交由后端的fpm去执行,并返回结果。

$ vi /usr/local/nginx/conf/nginx.conf

如果你大致了解过nginx的配置,应该能够很快分辨出这个配置文件里面的结构,并且知道server代表一个虚拟主机,要增加虚拟主机就再增加一个server,而且这个conf文件中也给出了例子。那么怎么代理php-fpm呢?找到:

#location ~ \.php$ {
  #   root           html;
  #  fastcgi_pass   127.0.0.1:9000;
  #  fastcgi_index  index.php;
  #  fastcgi_param  SCRIPT_FILENAME  /script$fastcgi_script_name;
  #  include        fastcgi_params;
#}

把前面的#注释符号去掉,把script改为$document_root最终如下:

location ~ \.php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /$document_root$fastcgi_script_name;
    include        fastcgi_params;
}

这样就OK了,重新载入nginx配置即可

$ service nginx reload

然后到/usr/local/nginx/html去写一个php文档,进行测试吧。

如果你的程序能够正常运行起来,用ip作为外网访问地址访问成功,那么恭喜你,本篇文章的目的就达到了。

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326719746&siteId=291194637