Linux安装PHP7+NGINX小白教程
首先说下服务器,好多人刚开始可能会迷这个(亲眼所见),服务器相当于另一台电脑,而我们可以通过它的公网ip或域名去访问它,但是你在自己电脑上下载的东西永远在自己电脑,在服务器上是不可能找到的!然后你刚买的或者虚拟机刚配的系统是没有开发环境的,需要自己安装。
大家一般可能会直接安装集成环境或者直接yum安装集成开发环境,但是,随着环境的更新换代我们最好还是安装最新版本的开发环境比较好一些。 我在这就放一些安装教程和常见的问题
安装PHP
先安装依赖,我觉得多多宜善
yum install gcc gcc++ libxml2-devel cmake ncurses-devel perl-Data-Dumper boost boost-doc boost-devel libaio
复制代码
然后我们可以去php的官网(自行百度,提示建议选择香港服务器的)下载,这里我用的是php7.0.22,接近的版本应该都能用,有时版本差距会导致本教程不可用。
cd /usr/local/
wget --no-check-certificate http://hk1.php.net/get/php-7.0.22.tar.gz/from/this/mirror
tar -zxvf mirror
复制代码
若是你下载的文件名不是这个的话,自己换一下就行了
接下来安装php,为了防止以后麻烦,我们干脆把很多库直接编译上去
cd php-7.0.22
./configure --prefix=/usr/local/php --with-config-file-path=/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --enable-inline-optimization \
--disable-debug --disable-rpath --enable-shared --enable-soap --with-libxml-dir --with-xmlrpc --with-openssl --with-mcrypt --with-mhash --with-pcre-regex \
--with-sqlite3 --with-zlib --enable-bcmath --with-iconv --with-bz2 --enable-calendar --with-curl --with-cdb --enable-dom --enable-exif --enable-fileinfo \
--enable-filter --with-pcre-dir --enable-ftp --with-gd --with-openssl-dir --with-jpeg-dir --with-png-dir --with-zlib-dir --with-freetype-dir --enable-gd-native-ttf \
--enable-gd-jis-conv --with-gettext --with-gmp --with-mhash --enable-json --enable-mbstring --enable-mbregex --enable-mbregex-backtrack --with-libmbfl --with-onig \
--enable-pdo --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-zlib-dir --with-pdo-sqlite --with-readline --enable-session --enable-shmop --enable-simplexml \
--enable-sockets --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-wddx --with-libxml-dir --with-xsl --enable-zip --enable-mysqlnd-compression-support \
--with-pear --enable-opcache --enable-maintainer-zts --enable-tsrm-pthreads --disable-fileinfo
make
make install
复制代码
每一行后面的\
字符是用来换行的,不要去掉了
如果遇到了make:*** [sapi / cli / php] Error 1
那就可以
vim Makefile
复制代码
在100多行左右的EXTRA_LIBS中添加 -liconv 然后按esc键再按冒号输入wq保存退出,重新
make
make install
复制代码
至此已经安装成功,这时我们不妨来一个测试看它是否正常运行,但是为了方便我们先将它的PATH配置好,
vim /etc/profile
在最后一行写
PATH=$PATH:/usr/local/php/bin/;
保存退出后
source /etc/profile
php -v
php -m
复制代码
接下来我们写一个php文件来测试运行
cd /usr/local/
vim test.php
<?php
phpinfo();
php test.php
rm test.php
复制代码
若打印成功,则无误
安装NGINX
首先我们下载安装一个pcre,也就是正则
cd /usr/local
wget --no-check-certificate ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz
tar -zxvf pcre-8.39.tar.gz
cd pcre-8.39
./configure --prefix=/usr/local/pcre-8.39
make
make install
复制代码
这是下载了正则并安装到/usr/local/pcre-8.39
在安装nginx时要引用
然后我们安装nginx,我们进入nginx的官网 随便点一个版本选择Stable version也就是稳定版中间的下载
cd /usr/local
wget --no-check-certificate http://nginx.org/download/nginx-1.12.1.tar.gz
tar -zxvf nginx-1.12.1.tar.gz
cd nginx-1.12.1
./configure --prefix=/usr/local/nginx --with-pcre=/usr/local/pcre-8.39
make
make install
复制代码
至此我们就安装好了nginx,在启动之前你要先关掉apache
ps aux | grep httpd
kill-9 进程1 进程2 ···
vim /etc/profile
PATH=$PATH:/usr/local/nginx/sbin/;
source /etc/profile
nginx
复制代码
这时我们就可以去图形界面查看,访问nginx首页http://localhost/ 是否可以打开(若是不带图形界面的服务器,可以在本机访问 http://公网ip(或者域名) 如果显示这个页面遍成功了,接下来我们就需要让nginx和php协同工作
cd /usr/local/php7/etc/
ll
mv php-fpm.conf.default php-fpm.conf
cd php-fpm.d
ll
mv www.conf.default www.conf
/usr/local/php7/sbin/php-fpm
ps aux | grep php-fpm
复制代码
若成功运行,我们把它的路径加入PATH
vim /etc/profile
PATH=$PATH:/usr/local/php7/sbin/;
source /etc/profile
复制代码
最后我们来配置下nginx
cd /usr/local/nginx/conf
ll
vim nginx.conf
复制代码
在
location / {
root html;
index index.html index.htm;
}
复制代码
下面加上
location ~ \.php {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index /index.php;
include /usr/local/nginx/conf/fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
复制代码
最后我们来测试一下php和nginx是不是能协同工作
nginx -s reload
cd ../
ll
cd html
vim index.php
<?php
phpinfo();
复制代码
然后我们访问http://localhost/index.php 成功打印phpinfo()界面说明php与nginx可以协同工作了,记得每次重启系统后要开启nginx和php-fpm的服务
nginx
php-fpm
复制代码
附
- 在目录
/usr/local/php-7.0.22/ext/
下面有好多可以用的扩展,一般是cd进去后直接
phpize
./configure
make
make install
复制代码
也可以自己百度找扩展这样安装,但是有的扩展可能还需要其它依赖,那就百度吧。
- php的
./configure
配置可以在/usr/local/php-7.0.22/Makefile
中找到