LNMP(CentOS+Nginx+Mysql+PHP)服务器环境配置
一、简介
Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,它的发音为 “engine X”, 是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP 代理服务器.Nginx是由俄罗斯人 Igor Sysoev为俄罗斯访问量第二的 Rambler.ru站点开发的,它已经在该站点运行超过三年了。Igor Sysoev在建立的项目时,使用基于BSD许可。
在高并发连接的情况下,Nginx是Apache服务器不错的替代品。Nginx同时也可以作为7层负载均衡服务器来使用。Nginx 0.8.46 + PHP 5.2.14 (FastCGI) 可以承受3万以上的并发连接数,相当于同等环境下Apache的10倍。
Nginx 超越 Apache 的高性能和稳定性,使得国内使用 Nginx 作为 Web 服务器的网站也越来越多,其中包括新浪博客、新浪播客、网易新闻、腾讯网、搜狐博客等门户网站频道,以及六间房、56.com等视频分享网站,Discuz!官方论坛、水木社区等知名论坛,盛大在线、金山逍遥网等网络游戏网站,豆瓣、人人网、 YUPOO相册、金山爱词霸、迅雷在线等新兴Web 2.0网站。
为什么Nginx的性能要比Apache高得多?
这得益于Nginx使用了最新的epoll(Linux 2.6内核)和kqueue(freebsd)网络I/O模型,而Apache则使用的是传统的select模型。
目前Linux下能够承受高并发访问的 Squid、Memcached都采用的是epoll网络I/O模型。而处理大量的连接的读写,Apache所采用的select网络I/O模型非常低效。
下面用一个比喻来解析Apache采用的select模型和Nginx采用的epoll模型进行之间的区别:
假设你在大学读书,住的宿舍楼有很多间房间,你的朋友要来找你。
select 版: 宿管大妈就会带着你的朋友挨个房间去找,直到找到你为止。
epoll 版: 宿管大妈会先记下每位同学的房间号,你的朋友来时,只需告诉你的朋友你住在哪个房间即可,不用亲自带着你的朋友满大楼找人。
如果来了10000个人,都要找自己住这栋楼的同学时,select版和epoll版宿管大妈,谁的效率更高,不言自明。同理,在高并发服务器中,轮询I/O是最耗时间的操作之 一,select和epoll的性能谁的性能更高,同样十分明了。
Nginx 官方中文维基:http://wiki.nginx.org/NginxChs (推荐)
Nginx 官网:https://nginx.org , Nginx历史版本
PHP 官网:https://www.php.net , PHP历史版本
二、系统环境
系统平台:RHEL 5.4(系统要求:Linux 2.6+ 内核)
[root@mimvp ~]# uname -a
Linux mimvp 2.6.32-358.6.2.el6.x86_64 #1 SMP Thu May 16 20:59:36 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
Nginx 版本:nginx/1.7.9
MySQL 版本:MariaDB-server.x86_64 10.0.15-1.el6 (或 MySQL Community Server 5.6.22)
PHP 版本:PHP 5.5.9
三、安装准备
1、获取相关安装包
提前自定义安装最新版本 sqlite3、pcre2、apr、apr-iconv、apr-util、openssl等,详见米扑博客:Ubuntu / CentOS 配置Apache、apr、apr-util、apr-iconv、sqlite3 、CentOS 安装OpenSSL 支持 QUIC for http/3
CentOS、Redhat等Linux发行版可直接利用CentOS的RPM包安装,如有关联包,安装时一起安装。
注:先安装如下关联包,否则下面的安装会提示错误
sudo yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel 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 openldap openldap-devel nss_ldap openldap-clients openldap-servers unzip subversion
2、RPM包搜索网站
http://code.google.com/p/zed-lnmp/
3、nginx软件包准备
Nginx所需要的软件包可以从下面位置获取,也可以记住名字在google中搜索。
最新版本截止 2024.06.30
php-8.3.8.tar.gz (2024-06-06)
php-8.2.8.tar.gz (2023-07-06)
php-7.2.8.tar.gz (2018-07-19)
nginx-1.26.1.tar.gz (2024-05-28)
以下最新版本截止 2018.07.24
httpd-2.4.34.tar.gz 官方下载地址 (2018-07-16)
nginx-1.14.0.tar.gz 官方下载地址 (2018-04-17)
php-7.2.8.tar.gz 官方下载地址 (2018-07-19)
php-5.2.17-fpm-0.5.11.diff.gz 官方下载地址,官方主页 (2011-01-11)
mysql-8.0.11.tar.gz 官方下载地址 (2018-04-19)
MariaDB 10.3.8 官方下载地址 (2018-07-19)
PDO_MYSQL-1.0.2.tgz 官方下载地址(2006-05-01)
MySQL-python-1.2.4b4.tar.gz 官方下载地址(2012-10-08)
MySQL-python-1.2.5.zip 官网下载地址 (2014-01-02)
libiconv-1.15.tar.gz , libiconv-1.17.tar.gz 官方下载地址 ,官网主页(2017-02-02)
libmcrypt-2.5.8.tar.gz 官方下载地址,官网主页(2007-02-19)
mcrypt-2.6.8.tar.gz 官方下载地址,官网主页(2008-11-22)
mhash-0.9.9.9.tar.gz 官方下载地址 ,官网主页(2008-12-08)
memcached-1.5.9.tar.gz 官方下载地址 (2018-07-07)
memcache-3.0.8.tgz 官方下载地址 (2013-04-07)
memcached-3.0.4.tgz 官方下载地址 (2017-11-21)
php-memcached-3.0.4.tar.gz github下载地址 (2017-11-21)
libmemcached-1.0.18.tar.gz 官方下载地址 (2014-02-09)
pcre-8.42 官方下载地址,官方主页 (2018-03-20)
pcre2-10.31 官方下载地址,官方主页 (2018-02-12)
openssl-3.3.1.tar.gz (包含QUIC for HTTP/3)官方下载地址,官方主页 (2024-06-04)
eaccelerator-0.9.5.3.tar.bz2 官方下载地址,github源码 (2012-06-25)
ImageMagick-7.0.8-8.tar.gz 官方下载地址,官方主页 (2018-07-24)
imagick-3.4.3.tgz 官方下载地址 (2017-02-01)
mongodb-src-r4.0.0.tar.gz 官方下载地址 (2018-06-21)
redis-4.0.10.tar.gz 官方下载地址 (2018-06-13)
redis-4.1.0.tgz 官方下载地址 (2018-07-10)
phpredis-4.1.0.tar.gz github下载地址 (2018-07-10)
四、安装 PHP(FastCGI模式)
编译安装PHP所需的支持库:
1. 安装 libiconv
对文本进行编码间的转换,用它来处理中文各种编码之间的转换。
#tar zxvf libiconv-1.14.tar.gz
#cd libiconv-1.14/
#./configure --prefix=/usr/local
#make
#make install
cd ../
2. 安装libmcrypt 实现加密功能的库(加解密库,包含libmcypt、mhash、mcrypt)
MCrypt:加密程序(Encryption program)
Libmcrypt:加密库供mcrypt使用(The encryption library used by mcrypt)
MCrypt is a replacement for the old crypt() package and crypt(1) command, with extensions. It allows developers to use a wide range of encryption functions, without making drastic changes to their code. It allows users to encrypt files or data streams without having to be cryptographers. Above all, it allows you to have some really neat code on your machine.
The companion to MCrypt is Libmcrypt, which contains the actual encryption functions themselves, and provides a standardized mechanism for accessing them. Libmcrypt supports the algorithms:
BLOWFISH, TWOFISH, DES, TripleDES, 3-WAY, SAFER-sk64, SAFER-sk128, SAFER+, LOKI97, GOST, RC2, RC6, MARS, IDEA, RIJNDAEL-128 (AES), RIJNDAEL-192, RIJNDAEL-256, SERPENT, CAST-128 (known as CAST5), CAST-256, ARCFOUR and WAKE. Block algorithms can be used in: CBC, ECB, CFB and OFB (8 bit and n bit, where n is the size of the algorithm's block length).
1)yum命令安装
# yum list *mcrypt* *mhash* # 查询yum可安装的服务,可查看服务版本 # yum search mcrypt mhash # 查询yum可安装的服务,可查看服务简介 # yum install mcrypt php-mcrypt libmcrypt libmcrypt-devel libtomcrypt libtomcrypt-devel mhash mhash-devel Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.cloud.aliyuncs.com * extras: mirrors.cloud.aliyuncs.com * updates: mirrors.cloud.aliyuncs.com Package mcrypt-2.6.8-11.el7.x86_64 already installed and latest version Package php-mcrypt-5.4.16-9.el7.x86_64 already installed and latest version Package libmcrypt-2.5.8-13.el7.x86_64 already installed and latest version Package libmcrypt-devel-2.5.8-13.el7.x86_64 already installed and latest version Package libtomcrypt-1.17-26.el7.x86_64 already installed and latest version Package libtomcrypt-devel-1.17-26.el7.x86_64 already installed and latest version Package mhash-0.9.9.9-10.el7.x86_64 already installed and latest version Package mhash-devel-0.9.9.9-10.el7.x86_64 already installed and latest version
2)源文件编译安装
wget https://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
# tar zxvf libmcrypt-2.5.8.tar.gz
# cd libmcrypt-2.5.8/
# ./configure
# make
# make install
# /sbin/ldconfig # 使ld动态库生效,可配置 /etc/ld.so.conf 和 /etc/ld.so.conf.d/xx.conf
# 注:这里不要退出去了。
# cd libltdl/
# ./configure --enable-ltdl-install
# make
# make install# 若在 /etc/profile 已配置 $PATH ,可忽略下面 ls -s 软连接
ln -s /usr/local/bin/libmcrypt_config /usr/bin/libmcrypt_config
# 若在 /etc/profile 已配置 $LD_LIBRARY_PATH,可忽略下面的 export
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
这里分享下米扑科技的 /etc/profile 文件配置项(部分):
## PATH放末尾, 让/usr/local/和自定义安装命令优先 export PATH=/usr/local/bin:$PATH export ImageMagick_ROOT=/usr/local/ImageMagick export PATH=${ImageMagick_ROOT}/bin:$PATH export libgd_ROOT=/usr/local/libgd export PATH=${libgd_ROOT}/bin:$PATH export libiconv_ROOT=/usr/local/libiconv export PATH=${libiconv_ROOT}/bin:$PATH export libevent_ROOT=/usr/local/libevent export PATH=${libevent_ROOT}/bin:$PATH export libmemcached_ROOT=/usr/local/libmemcached export PATH=${libmemcached_ROOT}/bin:$PATH ## 配置pkgconfig 环境变量, 以找到xx.so链接库 # php install: ./configure --prefix=/usr/local/php .... export PKG_CONFIG_PATH="/usr/lib64/pkgconfig":$PKG_CONFIG_PATH export PKG_CONFIG_PATH="/usr/share/pkgconfig":$PKG_CONFIG_PATH export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig":$PKG_CONFIG_PATH export PKG_CONFIG_PATH="/usr/local/lib64/pkgconfig":$PKG_CONFIG_PATH ## 配置lib/lib64 环境变量, 以找到xx.so链接库 # 也可配置 echo "/usr/local/openssl/lib64" > /etc/ld.so.conf.d/openssl.conf && ldconfig -v export LD_LIBRARY_PATH=/usr/lib:/usr/lib64:${LD_LIBRARY_PATH} export LD_LIBRARY_PATH=/usr/local/lib:/usr/local/lib64:${LD_LIBRARY_PATH} export OPENSSL_ROOT=/usr/local/openssl export LD_LIBRARY_PATH=${OPENSSL_ROOT}/lib:${OPENSSL_ROOT}/lib64:${LD_LIBRARY_PATH}
3. 安装mhash (哈稀函数库)
# tar zxvf mhash-0.9.9.9.tar.gz
# cd mhash-0.9.9.9/
# ./configure
# make
# make install
# cd ../
ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1以上的动态链接库 xx.so ,可配置 /etc/ld.so.conf 和 /etc/ld.so.conf.d/xx.conf
这里分享下米扑科技的 /etc/profile 文件配置项(部分):
# vim /etc/ld.so.conf # ## 在末尾添加如下内容: # cat /etc/ld.so.conf include ld.so.conf.d/*.conf /usr/local/lib64 /usr/local/lib /usr/lib /usr/lib64 ## 若是添加单个 lib或lib64,可在 /etc/ld.so.conf.d/ 目录下天机 ## 添加单个 openssl、libevent、pcre2、sqlite3 示例如下: # # cat /etc/ld.so.conf.d/openssl.conf /usr/local/openssl/lib64 # # cat /etc/ld.so.conf.d/libevent.conf /usr/local/libevent/lib/ # # cat /etc/ld.so.conf.d/pcre2.conf /usr/local/pcre2/lib/ # # cat /etc/ld.so.conf.d/sqlite3.conf /usr/local/sqlite3/lib
4. 安装mcrypt
# tar zxvf mcrypt-2.6.8.tar.gz
# cd mcrypt-2.6.8/
# /sbin/ldconfig # 使ld动态库生效
#./configure LD_LIBRARY_PATH=/usr/local/lib # 可配置 /etc/ld.so.conf 和 /etc/ld.so.conf.d/xx.conf
# make
# make install
# cd ../
说明:由于在配置Mcrypt时,会找不到libmcrypt的链接库,导致无法编译,因为Libmcrypt的链接库在/usr/local/文件夹下。因些在配置mcrypt时要加入LD_LIBRARY_PATH=/usr/local导入键接库
如果安装过程中,抛出“configure: error: *** libmcrypt was not found”错误,这是因为环境变量的问题,gcc编译的时候根据自身定义的变量寻找相关函数库等文件,libmcrypt也是刚安装的,在变量中没有定义出来,所以手动添加环境变量使其生效:
# export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
然后 #./configure 过后,继续 # make && make install , 成功!
若安装失败,请详见米扑博客:PHP 对称加密AES算法 (强烈推荐)
备注:PHP 8 编译参数 ./configure 已不支持 mcrypt 模块,通过 phpinfo 不显示 mcrypt 模块
GD库(libgd)是开源的非常强大的图片库,其所需要安装包,可以采用rpm包来安装,减少时间,由于php已经集成GD库,但前提支持包应事先安装,如zlib,png,jpeg,freetype等。如果完全想采用tar包安装的话,请参考lamp相关内容。
libgd Github:https://github.com/libgd/libgd , 下载最新版本 libgd-gd-2.3.3.tar.gz (2021-09-11)
GD库使用yum命令安装方式:
yum install gd gd-devel # 版本号 2.0.35-27.el7_9
GD is an open source code library for the dynamic creation of images by programmers. GD is written in C, and "wrappers" are available for Perl, PHP, ruby and many other bindings.
GD Supported Image Formats:
- BMP (builtin)
- GIF with animation support (builtin)
- TGA (builtin)
- WBMP (builtin)
- WebP via libwebp
- PNG via libpng
- JPEG using external library:
libjpeg-turbo (recommended)
libJpeg-
Does not include JPEG2000 - AVIF via libavif
- HEIF via libheif - This includes AVIF read support if your system's libheif has AV1 decoding.
- TIFF via libtiff
- XPM via libXpm
安装MongoDB
CentOS 安装 MongoDB:
Install MongoDB on CentOS ( 官方 )
CentOS 7 vim /etc/yum.repos.d/mongodb-org.repo
[mongodb-org-3.2] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-3.2.asc
安装命令:
sudo yum install -y mongodb-org
五、编译安装MySQL(安装时间较长)
方式1: CentOS 安装配置MariaDB
MariaDB 官方安装教程:MariaDB 10.2 CentOS repository list
CentOS 7 vim /etc/yum.repos.d/MariaDB.repo
[mariadb] name = MariaDB baseurl = http://yum.mariadb.org/10.2/centos7-amd64 gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB gpgcheck=1
安装命令:
# yum remove MariaDB-server MariaDB-client
yum -y install MariaDB-client MariaDB-server MariaDB-devel
方式2: MySQL 源码安装
建立mysql组,建立mysql用户并且加入到mysql组中
# /usr/sbin/groupadd mysql
# /usr/sbin/useradd mysql -g mysql
更多介绍请见米扑博客:Linux 账户管理命令useradd、groupadd
查看新增用户 mysql:
# cat /etc/passwd | grep "mysql" mysql:x:997:995:MySQL server:/var/lib/mysql:/sbin/nologin
[root@linux lnmp]# tar zxvf mysql-5.1.35.tar.gz
[root@linux lnmp]# cd mysql-5.1.35
# ./configure --prefix=/usr/local/mysql
--without-debug
--with-extra-charsets=gbk
--with-extra-charsets=all
--enable-assembler
--with-pthread
--enable-thread-safe-client
--with-mysqld-ldflags=-all-static /*不带共享库的形式编译mysqld*/
--with-client-ldflags=-all-static
--with-big-tables
--with-readline /*要采用rpm方式安装ncurses或tar包安装*/
--with-ssl /*要采用rpm方式安装openssl*/
--with-embedded-server
--enable-local-infile
--with-plugins=innobase
# make && make install
# /usr/local/mysql/bin/mysql_install_db --user=mysql
#以mysql身份初始化数据库
# cp ./support-files/mysql.server /etc/init.d/mysql
#复制Mysql启动服务至系统
# cp ./support-files/my-medium.cnf /etc/my.cnf
# chmod 755 /etc/init.d/mysql
# cd /usr/local/mysql/ #切换到cd /usr/local/mysql/目录下
# chown -R mysql . #改变当前目录下的所有者为mysql用户
# chown -R mysql var #修改数据库目录的权限
# chgrp -R mysql . #改变当前目录下的mysql用户的文件为mysql组
# /usr/local/mysql/bin/mysqld_safe --user=mysql&
# /usr/local/mysql/bin/mysqladmin -u root password 'admin' #设置管理员密码
[root@linux html]# /usr/local/mysql/bin/mysql -u root -p #测试密码输入
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.1.35-log Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> \q /*退出mysql*/
# chkconfig --add mysql #添加mysqld服务到系统
# chkconfig mysql on #打开myslqd服务
# service mysql start #启动Mysql
#/usr/local/mysql/bin/mysqladmin shutdown #关闭数据库
#查看mysql端口的打开情况
# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/P name
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2936/
#查看是否启动:
#ps -ef | grep mysql
注:在配置过程中,整体的配置思路从上往下,其中的一些功能可以用在实际的配置过程中,主要用于测试环节中。
Mysql的优化部分这里没有给出,需要参考其它资料。
六、编译安装PHP
这个地方是最重要的,因为默认情况下Nginx和PHP之间是一点感觉没有的。
在之前搭建过Apache+PHP,Apache+PHP编译后生成的是模块文件,而Nginx+PHP需要PHP生成可执行文件才可以,所以要利用fastcgi技术来实现Nginx与PHP的整合,这个只要我们安装时启用 FastCGI即可。
此次我们安装PHP不仅使用了FastCGI,而且还使用了PHP-FPM,PHP-FPM说白了是一个管理 FastCGI的一个管理器,它作为PHP的插件存在,在安装PHP时要想使用PHP-FPM就需要把PHP-FPM以补丁的形式安装到PHP中,而且 PHP要与PHP-FPM版本一致,这是必须的,切记!
# tar zxvf php-5.2.10.tar.gz
# gzip -cd php-5.2.10-fpm-0.5.11.diff.gz | patch -d php-5.2.10 -p1
# 将php-5.2.10-fpm-0.5.11.diff.gz以补丁形式加到php-5.2.10里面
# cd php-5.2.10/
# ./configure --prefix=/usr/local/php --with-fpm-user=www --with-fpm-group=www --with-config-file-path=/usr/local/php/etc --enable-maintainer-zts --enable-fastcgi --enable-fpm --with-gd --enable-gd-native-ttf --with-curl --with-xsl --with-gettext --with-freetype-dir --with-iconv-dir --with-jpeg-dir --with-png-dir --with-libdir=lib64 --with-libxml-dir --with-curlwrappers --with-xmlrpc --enable-mbstring --with-mcrypt --with-openssl --with-mhash --with-mysqli --with-pdo-mysql --with-pdo-sqlite --with-pcre-regex --with-pear --with-zlib --enable-safe-mode --enable-bcmath --enable-libxml --enable-mbregex --enable-mbstring --enable-soap --enable-pcntl --enable-shmop --enable-sockets --enable-sysvsem --enable-xml --enable-zip --enable-inline-optimization --enable-opcache
备注:PHP 编译参数 ./configure 已有部分参数不支持,需查看安装参数帮助 ./configure --help
参考命令
# ./configure --prefix=/usr/local/php --enable-maintainer-zts --enable-fastcgi --enable-fpm --with-zlib --with-gd --enable-gd-native-ttf --with-curl --with-curlwrappers --enable-mbstring --with-mcrypt --with-openssl --with-mhash
--with-libxml-dir=/usr
--enable-xml
--disable-rpath
--enable-discard-path
--enable-safe-mode
--enable-bcmath
--enable-shmop
--enable-sysvsem
--enable-inline-optimization
--enable-mbregex
--enable-force-cgi-redirect
--enable-pcntl
--enable-sockets
--with-ldap
--with-ldap-sasl
--with-xmlrpc
--enable-zip
--enable-soap
--without-pear
--with-freetype-dir
--with-jpeg-dir
--with-png-dir
--with-config-file-path=/usr/local/php/etc
--with-mysql=/usr/local/mysql
--with-mysqli=/usr/local/mysql/bin/mysql_config
--with-iconv-dir=/usr/local
注:
--enable-maintainer-zts 是线程安全(pthreads),php编译时生效,不可动态加入
Nginx+PHP整合,在安装时必须启用 --enable-fastcgi和--enable-fpm,这两个选项是做什么的上面已经描述。执行完后系统会提示-–enable-fastcgi是一个未知选项,我们不必理会,原因见php文档:
Fastcgi is the preferred SAPI to connect PHP and Lighttpd. Fastcgi is automagically enabled in php-cgi in PHP 5.3, but for older versions configure PHP with --enable-fastcgi.
上面文档已经说明,PHP 5.3 以上版本已默认自动启用了 php-cgi ,因此不必再添加 --enable-fastcgi
#注:make的时候一定要加上后面的参数,才能成功。否则会提示错误:
/home/data/tool-server/php-5.5.21/ext/iconv/iconv.c:2486: undefined reference to `libiconv_close'
ext/iconv/.libs/iconv.o: In function `php_iconv_stream_filter_append_bucket':
/home/data/tool-server/php-5.5.21/ext/iconv/iconv.c:2637: undefined reference to `libiconv'
/home/data/tool-server/php-5.5.21/ext/iconv/iconv.c:2636: undefined reference to `libiconv'
/home/data/tool-server/php-5.5.21/ext/iconv/iconv.c:2558: undefined reference to `libiconv'
ext/iconv/.libs/iconv.o: In function `php_iconv_stream_filter_ctor':
/home/data/tool-server/php-5.5.21/ext/iconv/iconv.c:2512: undefined reference to `libiconv_open'
collect2: ld returned 1 exit status
make: *** [sapi/cli/php] Error 1
继续,make的时候一定要加上后面的参数
# make ZEND_EXTRA_LIBS='-liconv' (注意: 单引号是中文格式,要转成英文单引号)
# make install
# cp php.ini-dist /usr/local/php/etc/php.ini
# cd ../
注:在安装过程中采用了tar包与rpm混合安装的情况,对于库的指定确实出现了很大的麻烦。如果采用rpm安装的话,不需要指定支持包的位置就可以了,tar安装的话,需要指定安装位置。
安装错误1
yum install php-devel
解决方法:
yum --enablerepo=remi, remi-php54 install php-devel
原文引用:
can't install php-devel on centos
Rather than running yum install php-devel you needed to run yum --enablerepo=remi,remi-php54 install php-devel .
In short, just specifying which php-devel version you wanted from the remi repo. In your case you had php54 installed from remi so you needed to add in "remi-php54" . That would have successfully installed php-devel without the downtime.
安装错误2
make: *** [ext/phar/phar.php] Error 127
解决方法:
vim /etc/ld.so.conf
最后面添加一行
/usr/local/lib
执行命令
/sbin/ldconfig
然后,重新编译
make clean && make
七、编译安装PHP5扩展模块
1. 安装memcache (跳过)
# tar zxvf memcache-3.0.8.tgz
# cd memcache-3.0.8/
# ./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache --with-zlib-dir
# make
# make install
# 说明:memcache库的位置
Installing shared extensions:
/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/
# cd ..
安装提示出错: make: *** [memcache.lo] Error 1
没有安装zlib : yum -y install zlib-devel
继续安装: ./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache --with-zlib-dir
make 编译,再次出错:
分析文件名,发现文件名错误 mysql_driver.loT, mysql_statement.loT
修改文件名
ln -s memcache.loT memcache.lo
ln -s memcache_pool.loT memcache_pool.lo
ln -s memcache_ascii_protocol.loT memcache_ascii_protocol.lo
ln -s memcache_binary_protocol.loT memcache_binary_protocol.lo
ln -s memcache_standard_hash.loT memcache_standard_hash.lo
ln -s memcache_consistent_hash.loT memcache_consistent_hash.lo
ln -s memcache_session.loT memcache_session.lo
2. 安装eaccelerator php加速 (跳过)
# tar jxvf eaccelerator-0.9.5.3.tar.bz2
# cd eaccelerator-0.9.5.3/
# ./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php/bin/php-config
# make
# make install
Installing shared extensions:
/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/
# cd ../
3. 安装PDO_MYSQL (数据库连接的支持)
# tar zxvf PDO_MYSQL-1.0.2.tgz
# cd PDO_MYSQL-1.0.2/
# /usr/local/php/bin/phpize
#./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-mysql=/usr/bin/mysql_config
或者
#./configure --with-php-config=/usr/local/php/bin/php-config
--with-pdo-mysql=/usr/
# make
# make install
Installing shared extensions:
/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/
# cd ../
补充:
如果 php 是通过CentOS系统命令安装的 yum install php ,则安装方法如下:
# tar zxvf PDO_MYSQL-1.0.2.tgz
# cd PDO_MYSQL-1.0.2/
# /usr/bin/phpize
# ./configure --with-php-config=/usr/bin/php-config
# make
# make install
Installing shared extensions: /usr/lib64/php/modules/
# cd ../
如果出现错误“make: *** [pdo_mysql.lo] Error 1”,这是因为在编译安装时需要 MySQL 的头的文件,而它按默认搜索找不到头文件的位置所以才出现这个问题。通过软连接把MySQL头文件对应到/usr/local/include/下就好,软链接命令:
ln -s /usr/include/mysql/* /usr/local/include/
make 出错信息:
checking for mysql_config... not found
configure: error: Cannot find MySQL header files under
原因也是找不到mysql文件头,解决步骤:
1. 搜索mysql文件头 find / -name 'mysql.h'
2. 若搜索没有,则需安装 yum -y install mysql-devel MariaDB-devel
3. 验证mysql头文件存在
# ll /usr/include/mysql/mysql.h
-rw-r--r-- 1 root root 39069 Jun 23 17:24 /usr/include/mysql/mysql.h
4. 建立软链接
ln -s /usr/include/mysql/* /usr/local/include/
5. 重新编译
/usr/bin/phpize 或 /usr/local/php/bin/phpize (推荐)
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install
编译再出错:
checking for mysql_config... not found
checking for mysql install under /usr/local... libs under /usr/local/lib; seems promising
checking for mysql_query in -lmysqlclient... no
configure: error: mysql_query missing!?
原因是找不到 /usr/bin/mysql_config
1. 搜索 mysql_config : find / -name mysql_config
2. 重新make编译:
./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-mysql=/usr/bin/mysql_config
3. make 编译,再次出错:
分析文件名,发现文件名错误 mysql_driver.loT, mysql_statement.loT
修改文件名(每次报错后,需多次编译 make 才会生成 xxx.loT)
ln -s pdo_mysql.loT pdo_mysql.lo
ln -s mysql_driver.loT mysql_driver.lo
ln -s mysql_statement.loT mysql_statement.lo
然后: make && make install
安装成功后,查看:
# ll /usr/local/php/lib/php/extensions/no-debug-non-zts-20151012/
-rwxr-xr-x 1 root root 50910 8月 5 16:43 ctype.so
-rwxr-xr-x 1 root root 307912 8月 5 16:05 curl.so
-rwxr-xr-x 1 root root 3613807 8月 5 16:44 fileinfo.so
-rwxr-xr-x 1 root root 1208680 8月 5 16:10 gd.so
-rwxr-xr-x 1 root root 52941 8月 5 16:11 gettext.so
-rwxr-xr-x 1 root root 171817 8月 5 16:44 iconv.so
-rwxr-xr-x 1 root root 3070736 8月 5 16:08 mbstring.so
-rwxr-xr-x 1 root root 1950316 8月 5 16:12 opcache.a
-rwxr-xr-x 1 root root 910196 8月 5 16:12 opcache.so
-rwxr-xr-x 1 root root 3908168 8月 5 22:38 pdo_mysql.so
-rwxr-xr-x 1 root root 97437 8月 5 16:45 posix.so
-rwxr-xr-x 1 root root 366873 8月 5 16:46 sockets.so
-rwxr-xr-x 1 root root 850224 8月 5 16:44 zip.so
附上CentOS7 MySQL目录:
# find / -name "mysql"
/etc/logrotate.d/mysql
/etc/rc.d/init.d/mysql
/home/data/tool-server/php-7.0.9/travis/ext/mysql
/usr/bin/mysql
/usr/include/mysql
/usr/lib64/mysql
/usr/share/mysql
/var/lib/mysql
/var/lib/mysql/mysql
/run/lock/subsys/mysql
# find / -name "*mysql_config*"
/usr/bin/mysql_config
/usr/share/man/man1/mysql_config.1.gz
4. 安装ImageMagick,它是Linux下非常强大的图象处理函数与GD类似.
ImageMagick® is a free and open-source software suite, used for editing and manipulating digital images. It can be used to create, edit, compose, or convert bitmap images, and supports a wide range of file formats, including JPEG, PNG, GIF, TIFF, PDF, and Ultra HDR.
ImageMagick 官网:https://imagemagick.org
ImageMagick Github:https://github.com/ImageMagick/ImageMagick
1)yum命令安装(不推荐,不是最新版本,无imagick)
yum install ImageMagick ImageMagick-devel ImageMagick-perl php-pecl-imagick
# yum list *magick* # 查询可安装的magick,包含ImageMagick、imagick # yum search magick # 查询可安装的magick,包含ImageMagick、imagick # yum install ImageMagick ImageMagick-devel ImageMagick-perl php-pecl-imagick Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.cloud.aliyuncs.com * extras: mirrors.cloud.aliyuncs.com * updates: mirrors.cloud.aliyuncs.com Package ImageMagick-6.9.10.68-7.el7_9.x86_64 already installed and latest version Package ImageMagick-devel-6.9.10.68-7.el7_9.x86_64 already installed and latest version Package ImageMagick-perl-6.9.10.68-7.el7_9.x86_64 already installed and latest version Package php-pecl-imagick-3.4.4-1.el7.x86_64 already installed and latest version
2)编译指定安装(推荐,可安装最新版本,有imagick)
wget -O ImageMagick-7.1.1-34.tar.gz https://github.com/ImageMagick/ImageMagick/archive/refs/tags/7.1.1-34.tar.gz
# tar zxvf ImageMagick-7.1.1-34.tar.gz
# cd ImageMagick-7.1.1-34/
# ./configure --prefix=/usr/local/ImageMagick # 安装目录供安装 imagick 指定使用
# make
# make install
# cd ../
5. 安装imagick (连接PHP和ImageMagick的通道)
# tar zxvf imagick-3.7.0.tgz
# cd imagick-3.7.0/
# /usr/local/php/bin/phpize # 根据 config.m4 生成 configure 文件
# ./configure --with-php-config=/usr/local/php/bin/php-config --with-imagick=/usr/local/ImageMagick
# make
# make install
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-zts-20230831/
Installing header files: /usr/local/php/include/php/
# cd ../
补充:
如果 php 是通过CentOS系统命令安装的 yum install php ,则安装方法如下:
# tar zxvf imagick-3.4.3.tgz
# cd imagick-3.4.3/
# /usr/bin/phpize
# ./configure --with-php-config=/usr/bin/php-config
# make
# make install
Installing shared extensions: /usr/lib64/php/modules/
Installing header files: /usr/include/php/
# cd ../
6. 修改php.ini文件,已使php支持扩展的功能
cp /home/data/tool-server/LNMP/php-7.0.13/php.ini-development /usr/local/php/etc/php.ini
vim /usr/local/php/etc/php.ini
查找
extension_dir = "./"
修改为
extension_dir="/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/"
并在此行后增加以下几行,然后保存:
extension = "memcache.so"
extension = "pdo_mysql.so"
extension = "imagick.so"
再查找output_buffering = Off
修改为output_buffering = On
7. 配置eAccelerator加速PHP
mkdir -p /usr/local/eaccelerator_cache
vim /usr/local/php/etc/php.ini
到配置文件的最末尾,粘上以下内容:
[eaccelerator]
zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
eaccelerator.shm_size="64"
eaccelerator.cache_dir="/usr/local/eaccelerator_cache"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="3600"
eaccelerator.shm_prune_period="3600"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
八、PHP-fpm配置
1. 创建php-fpm配置文件
php-fpm是为PHP打的一个FastCGI管理补丁,可以平滑变更php.ini配置而无需重启php-cgi:
在/usr/local/php/etc/目录中创建php-fpm.conf文件,
也可以在原有的基础上进行修改,即先复制默认配置文件为:
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf (推荐)
如果您安装 Nginx + PHP 用于程序调试
请将以下的
<value name="display_errors">0</value>改为
<value name="display_errors">1</value>,以便显示PHP错误信息,否则,Nginx 会报状态为500的空白错误页。
说明:创建www用户与组,这里创建了下面就不用创建了。
#/usr/sbin/groupadd www -g 48
#/usr/sbin/useradd -u 48 -g www www
查看新增用户 www:
# cat /etc/passwd | grep "www" www:x:48:48::/home/www:/bin/bash 配置php-fpm.conf
rm -f /usr/local/php/etc/php-fpm.conf
vim /usr/local/php/etc/php-fpm.conf
;;;;;;;;;;;;;;;;;;;;; ; FPM Configuration ; ;;;;;;;;;;;;;;;;;;;;; ; All relative paths in this configuration file are relative to PHP's install ; prefix (/usr/local/php). This prefix can be dynamically changed by using the ; '-p' argument from the command line. ;;;;;;;;;;;;;;;;;; ; Global Options ; ;;;;;;;;;;;;;;;;;; [global] ; Pid file ; Note: the default prefix is /usr/local/php/var ; Default Value: none ;pid = run/php-fpm.pid xxxxxx ....... xxxxxxx ;;;;;;;;;;;;;;;;;;;; ; Pool Definitions ; ;;;;;;;;;;;;;;;;;;;; ; Multiple pools of child processes may be started with different listening ; ports and different management options. The name of the pool will be ; used in logs and stats. There is no limitation on the number of pools which ; FPM can handle. Your system will tell you anyway :) ; Include one or more files. If glob(3) exists, it is used to include a bunch of ; files from a glob(3) pattern. This directive can be used everywhere in the ; file. ; Relative path can also be used. They will be prefixed by: ; - the global prefix if it's been set (-p argument) ; - /usr/local/php otherwise include=/usr/local/php/etc/php-fpm.d/*.conf
其中,
vim /usr/local/php/etc/php-fpm.d/www.conf
; Start a new pool named 'www'. ; the variable $pool can be used in any directive and will be replaced by the ; pool name ('www' here) [www] ; Per pool prefix ; It only applies on the following directives: ; - 'access.log' ; - 'slowlog' ; - 'listen' (unixsocket) ; - 'chroot' ; - 'chdir' ; - 'php_values' ; - 'php_admin_values' ; When not set, the global prefix (or /usr/local/php) applies instead. ; Note: This directive can also be relative to the global prefix. ; Default Value: none ;prefix = /path/to/pools/$pool ; Unix user/group of processes ; Note: The user is mandatory. If the group is not set, the default user's group ; will be used. user = nobody group = nobody ; The address on which to accept FastCGI requests. ; Valid syntaxes are: ; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific IPv4 address on ; a specific port; ; '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on ; a specific port; ; 'port' - to listen on a TCP socket to all addresses ; (IPv6 and IPv4-mapped) on a specific port; ; '/path/to/unix/socket' - to listen on a unix socket. ; Note: This value is mandatory. listen = 127.0.0.1:9000 xxxxxxxxxxx pm = dynamic ; The number of child processes to be created when pm is set to 'static' and the ; maximum number of child processes when pm is set to 'dynamic' or 'ondemand'. ; This value sets the limit on the number of simultaneous requests that will be ; served. Equivalent to the ApacheMaxClients directive with mpm_prefork. ; Equivalent to the PHP_FCGI_CHILDREN environment variable in the original PHP ; CGI. The below defaults are based on a server without much resources. Don't ; forget to tweak pm.* to fit your needs. ; Note: Used when pm is set to 'static', 'dynamic' or 'ondemand' ; Note: This value is mandatory. pm.max_children = 10 ; The number of child processes created on startup. ; Note: Used only when pm is set to 'dynamic' ; Default Value: min_spare_servers + (max_spare_servers - min_spare_servers) / 2 pm.start_servers = 6 ; The desired minimum number of idle server processes. ; Note: Used only when pm is set to 'dynamic' ; Note: Mandatory when pm is set to 'dynamic' pm.min_spare_servers = 2 ; The desired maximum number of idle server processes. ; Note: Used only when pm is set to 'dynamic' ; Note: Mandatory when pm is set to 'dynamic' pm.max_spare_servers = 10 ; The number of seconds after which an idle process will be killed. ; Note: Used only when pm is set to 'ondemand' ; Default Value: 10s ;pm.process_idle_timeout = 10s; ; The number of requests each child process should execute before respawning. ; This can be useful to work around memory leaks in 3rd party libraries. For ; endless request processing specify '0'. Equivalent to PHP_FCGI_MAX_REQUESTS. ; Default Value: 0 ;pm.max_requests = 500 xxxxxxxxxxx
2. php-fpm启动与管理
/usr/local/php/sbin/php-fpm start
注:/usr/local/php/sbin/php-fpm还有其他参数,包括:
start|stop|quit|restart|reload|logrotate,修改php.ini后不重启php-cgi,重新加载配置文件使用reload,就保持了在php的fastcgi进程持续运行的状态下,又重新加载了php.ini。
php-fpm 启动/关闭/重启脚本,需要自己手写,请参考
九、Nginx安装
1. nginx安装
Nginx只是web服务器,配合php技术实现的fastcgi来提高性能。
1、安装rewrite模块支持包pcre库:
pcre是perl所用到的正则表达式,目的是让所装的软件支持正则表达式。
默认情况下,Nginx只处理静态的网页请求,也就是html.
如果是来自动态的网页请求,比如*.php,那么Nginx就要根据正则表达式查询路径,然后把*.PHP交给PHP去处理。
# tar zxvf pcre2-10.30.tar.gz
# cd pcre2-10.30/
# ./configure
# make && make install
cd ../
2. 安装Nginx
说明:创建www用户组及www用户,如果之前php-fpm没有创建,这里要创建。
# /usr/sbin/groupadd www
# /usr/sbin/useradd -g www www
# tar zxvf nginx-1.12.1.tar.gz
# cd nginx-1.12.1/
#./configure --user=www --group=www --prefix=/usr/local/nginx --with-pcre --with-http_stub_status_module --with-http_sub_module --with-http_ssl_module --with-stream_ssl_module --with-http_v2_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-threads --with-stream --with-stream_ssl_module
备注:nginx-1.26.1 编译参数 ./configure 已有部分参数不支持,需查看安装参数帮助 ./configure --help
参考:
./configure \ --prefix=/usr/local/nginx \ --sbin-path=/usr/sbin/nginx \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/run/nginx.lock \ --http-client-body-temp-path=/var/cache/nginx/client_temp \ --http-proxy-temp-path=/var/cache/nginx/proxy_temp \ --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \ --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \ --http-scgi-temp-path=/var/cache/nginx/scgi_temp \ --user=nobody \ --group=nobody \ --with-pcre \ --with-http_v2_module \ --with-http_ssl_module \ --with-http_realip_module \ --with-http_addition_module \ --with-http_sub_module \ --with-http_dav_module \ --with-http_flv_module \ --with-http_mp4_module \ --with-http_gunzip_module \ --with-http_gzip_static_module \ --with-http_random_index_module \ --with-http_secure_link_module \ --with-http_stub_status_module \ --with-http_auth_request_module \ --with-mail \ --with-mail_ssl_module \ --with-file-aio \ --with-ipv6 \ --with-threads \ --with-stream \ --with-stream_ssl_module
运行结果:
Configuration summary
+ using threads
+ using system PCRE library
+ using system OpenSSL library
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
# make && make install
# cd ../
3.Nginx 安装后只有一个程序文件
本身并不提供各种管理程序,它是使用参数和系统信号机制对 Nginx 进程本身进行控制的。
Nginx 的参数包括有如下几个:
-c <path_to_config>:使用指定的配置文件而不是 conf 目录下的 nginx.conf 。
-t:测试配置文件是否正确,在运行时需要重新加载配置的时候,此命令非常重要,用来检测所修改的配置文件是否有语法错误。
-v:显示 nginx 版本号。
-V:显示 nginx 的版本号以及编译环境信息以及编译时的参数。
例如我们要测试某个配置文件是否书写正确,我们可以使用以下命令
sbin/nginx -t -c conf/nginx.conf
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
十、nginx配置
1.在/usr/local/nginx/conf/目录中创建nginx.conf文件:
rm -f /usr/local/nginx/conf/nginx.conf
vim /usr/local/nginx/conf/nginx.conf
======================================= ## nginx.conf才是nginx web服务器的配置文件 ======================================= user www www; /*启动nginx服务的用户与组*/ worker_processes 5; /*启动nginx服务的工作进程*/ error_log logs/nginx_error.log crit; /*错误日志,以及等级*/ pid /usr/local/nginx/nginx.pid; /*nginx服务进程PID*/ worker_rlimit_nofile 51200; events { use epoll; /*工作模式*/ worker_connections 51200; /*每进程允许最大的同时连接数*/ } http { include mime.types; default_type application/octet-stream; #charset gb2312; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; sendfile on; tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; #limit_zone crawler $binary_remote_addr 10m; server { listen 80; /*监听端口*/ server_name localhost; /*服务器名称*/ index index.html index.htm index.php; /*缺省主页名称*/ root /usr/local/nginx/html; /*网站根目录,也可以采用下面内容*/ #也可以采用相对路径,下面注释部分*/ #location / { # root html; # index index.html index.htm; # } #limit_conn crawler 20; #通过FastCGI方式支持PHP,php页面由fastcgi代理处理,这也是反向代理的一个应用,这里可以是jsp/asp等脚本。 #Nginx是通过本机的9000端口将PHP请求转发给PHP的,PHP自己是从本机的9000端口侦听数据,Nginx与PHP通过本机的9000端口完成了数据请求。 location ~ .*\.(php|php5)?$ { #fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_pass 127.0.0.1:9000; /*fastcgi监听端口*/ fastcgi_index index.php; include fcgi.conf; /*fastcgi配置文件,修改为以下内容*/ } #对于某一类型的文件,设置过期时间,静态的页面通常设置长一点。 #静态文件,nginx自己处理 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$ { expires 30d; } #日志的格式 log_format access '$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 access; } }
说明:以上配置文件只是基本配置文件,要实现其它功能的话,需要在此基础上进行修改。
补充:
如果没有安装 php-fpm,则可以直接运行 php-cgi 来启动解析php,命令如下:
/usr/bin/php-cgi -b 127.0.0.1:9000 &
配置php反向代理:
vim /usr/local/nginx/conf/nginx.conf
# 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; # php-cgi 启动的监控IP:端口
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
include fastcgi.conf;
}
配置完nginx配置文件 /usr/local/nginx/conf/nginx.conf 后,检测配置是否正确,以及重载或重启:
1) 检测配置是否正确
[root@mimvp-bj html]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
2) 重载或重启(使用一个即可,推荐重载reload命令)
[root@mimvp-bj html]# /etc/init.d/nginx reload
Reloading nginx: [ OK ]
[root@mimvp-bj html]# /etc/init.d/nginx restart
Stopping nginx: [ OK ]
Starting nginx: [ OK ]
2. 在/usr/local/nginx/conf/目录中创建fcgi.conf文件
说明:可以直接粘贴以下内容。
vim /usr/local/nginx/conf/fcgi.conf
fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE nginx; fastcgi_param QUERY_STRING $query_string; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_param REQUEST_URI $request_uri; fastcgi_param DOCUMENT_URI $document_uri; fastcgi_param DOCUMENT_ROOT $document_root; fastcgi_param SERVER_PROTOCOL $server_protocol; fastcgi_param REMOTE_ADDR $remote_addr; fastcgi_param REMOTE_PORT $remote_port; fastcgi_param SERVER_ADDR $server_addr; fastcgi_param SERVER_PORT $server_port; fastcgi_param SERVER_NAME $server_name; # PHP only, required if PHP was built with --enable-force-cgi-redirect fastcgi_param REDIRECT_STATUS 200;
十一、nginx启动与管理
1.启动nginx
/usr/local/nginx/sbin/nginx
2.测试nginx配置文件
修改/usr/local/nginx/conf/nginx.conf配置文件后,请执行以下命令检查配置文件是否正确:
# /usr/local/nginx/sbin/nginx -t
如果屏幕显示以下两行信息,说明配置文件正确:
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
the configuration file /usr/local/nginx/conf/nginx.conf was tested successfully
3.查看Nginx主进程号
ps -ef | grep "nginx: master process" | grep -v "grep" | awk -F ' ' '{print $2}'
屏幕显示的即为Nginx主进程号,例如:
6302
这时,执行以下命令即可使修改过的Nginx配置文件生效:
kill -HUP 6302
或者无需这么麻烦,找到Nginx的Pid文件:
kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
4.配置开机自动启动Nginx + PHP
vi /etc/rc.local
加入以下内容:
ulimit -SHn 51200
/usr/local/php/sbin/php-fpm start
/usr/local/nginx/sbin/nginx
5. 测试nginx
vim /usr/local/nginx/html/phpinfo.php
<?php echo date("Y-m-d H:i:s"); echo "<h1>hello mimvp.com</h1>"; echo phpinfo(); ?>
6. 测试结果
访问nginx服务器地址:http://123.57.78.100
CentOS7 Nginx 网站访问不了
CentOS 7.2 访问不了Nginx网站,大部分是因为CentOS 7 默认开启了防火墙
关闭防火墙:
systemctl stop firewalld.service # 停止firewall
systemctl disable firewalld.service # 禁止firewall开机启动
firewall-cmd --state # 查看默认防火墙状态(关闭后显示not running,开启后显示running)
# firewall-cmd --state
not running
打印PHP系统环境变量:
vim /usr/local/nginx/html/phpinfo.php
<?php echo phpinfo(); ?>
访问nginx服务器地址:http://123.57.78.100/phpinfo.php
参考推荐:
LNMP(linux+nginx+mysql+php)服务器环境配置
Linux php7安装mongoDB和memcached扩展(推荐)
Linux php7.0 安装redis和PHP扩展(推荐)
版权所有: 本文系米扑博客原创、转载、摘录,或修订后发表,最后更新于 2024-07-05 17:34:49
侵权处理: 本个人博客,不盈利,若侵犯了您的作品权,请联系博主删除,莫恶意,索钱财,感谢!