CentOS 7系统安装nginx+php
安装介绍
1.系统环境CentOS7
2.nginx版本1.12
3.PHP版本7.2 下载地址
4.MySQL版本5.7
安装nginx
添加centos7的 nginx yum源 然后执行安装
1 sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm 2 sudo yum install -y nginx
启动nginx
1 sudo systemctl start nginx.service
PHP安装
安装依赖
yum -y install gcc gcc-c++ libxml2 libxml2-devel autoconf automake pcre-devel zlib zlib-devel bzip2 bzip2-devel openssl openssl-devel
libcurl-devel libjpeg-devel libpng-devel freetype-devel readline readline-devel libxslt-devel perl perl-devel psmisc.x86_64 recode recode-devel curl-devel expat-devel gettext-devel zlib-devel
以上的依赖包有这个可能安装不了【 libmcrypt libmcrypt-devel libtidy libtidy-devel】,貌似也没有影响
下载PHP源码
1 mkdir /home/download && cd /home/download #下载到任何地方都可以 2 3 wget http://cn2.php.net/distributions/php-7.2.0.tar.gz 4 5 tar -zxvf php-7.2.0.tar.gz
开始编译
1 cd php-7.2.0 2 3 ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-opcache --with-gd --with-iconv --with-zlib --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-mbregex --enable-fpm --enable-mbstring --enable-ftp --enable-gd-native-ttf --enable-pcntl --enable-calendar --with-openssl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --with-curl --with-jpeg-dir --with-freetype-dir --with-mysqli --enable-embedded-mysqli --with-pdo-mysql
编译完成后执行安装,安装过程耗时较长
make && make install
生成 PHP 配置
1 sudo cp php.ini-production /usr/local/php/etc/php.ini 2 3 sudo cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm 4 5 sudo chmod +x /etc/init.d/php-fpm 6 7 sudo cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf 8 9 sudo cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf 10 11 sudo cp /usr/local/php/bin/php /usr/bin/php
查看PHP版本,执行/usr/local/php/bin/php -v 显示下面的结果表示安装完成
PHP 7.1.4 (cli) (built: May 1 2017 00:26:16) ( NTS ) Copyright (c) 1997-2017 The PHP Group Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
配置 Nginx
1 server { 2 listen 80 default_server; 3 listen [::]:80 default_server; 4 server_name _; #这里可以配置域名 5 root /var/www; #这里配置项目目录 6 index index.php index.html index.htm; 7 # Load configuration files for the default server block. 8 include /etc/nginx/default.d/*.conf; 9 location / { 10 try_files $uri $uri/ /index.php?$query_string; 11 } 12 error_page 404 /404.html; 13 location = /40x.html { 14 } 15 error_page 500 502 503 504 /50x.html; 16 location = /50x.html { 17 } 18 19 location ~ \.php$ { 20 21 root /var/www; #这里配置项目目录和上面一样 22 23 fastcgi_pass 127.0.0.1:9000; 24 25 try_files $uri /index.php =404; 26 27 fastcgi_split_path_info ^(.+\.php)(/.+)$; 28 29 fastcgi_index index.php; 30 31 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 32 33 include fastcgi_params; 34 35 } 36 37 }
重启服务完成所有配置
1 service nginx restart #重启 Nginx 2 service php-fpm restart # 重启 php-fpm
phpfpm和nginx设置开机自动启动
## 添加执行权限
1 chmod a+x /etc/init.d/nginx 2 3 chmod a+x /etc/init.d/php-fpm
## 加入服务
1 chkconfig --add nginx 2 3 chkconfig --add php-fpm
## 开机自启
1 chkconfig nginx on 2 3 chkconfig php-fpm on
查看服务列表:
1 chkconfig --list