编译安装php/nginx/nginx虚拟主机

  • 来源:网络
  • 更新日期:2020-07-22

摘要:系统运维 来源:https://blog.csdn.net/Powerful_Fy php官网:https://www.php.net进入官网后点击downloads选择需要下载的版本即可 本文以p

系统运维 来源:https://blog.csdn.net/Powerful_Fy


php官网:https://www.php.net
进入官网后点击downloads选择需要下载的版本即可

本文以php 7.3.10版本为例
安装包存放路径 /usr/local/src/

下载:

[root@linux src]# wget https://www.php.net/distributions/php-7.3.10.tar.bz2

解压:

[root@linux src]# tar -jxvf php-7.3.10.tar.bz2

编译安装:
进入目录 cd php-7.3.10.tar.bz2

配置编译参数:
./configure --prefix=/usr/local/php-fpm --with-config-file-path=/usr/local/php-fpm/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/Mysql --with-MYSQLi=/usr/local/mysql/bin/mysql_config --with-pdo-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --with-pear --with-curl --with-openssl

编译: make

安装:make install

拷贝配置文件php.ini-development到php程序安装目录:

[root@linux php-7.3.10]# cp php.ini-development /usr/local/php-fpm/etc/php.ini

拷贝启动脚本到/etc/init.d/目录:

[root@linux php-7.3.10]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

进入php安装目录下的/etc目录重命名配置文件:

[root@linux php-7.3.10]# cd /usr/local/php-fpm/etc/
[root@linux etc]# cp php-fpm.conf.default php-fpm.conf
[root@linux etc]# cd php-fpm.d/
[root@linux php-fpm.d]# cp www.conf.default www.conf

添加php启动脚本到系统服务:

[root@linux php-fpm.d]# chkconfig --add php-fpm
[root@linux php-fpm.d]# chkconfig php-fpm on
[root@linux php-fpm.d]# chmod 755 /etc/init.d/php-fpm

创建php用户:

[root@linux php-fpm.d]# useradd php-fpm

启动php:

[root@linux php-fpm.d]# service php-fpm start

编译安装php报错解决方法:

报错1:checking for gcc... no
解决:yum -y install gcc

报错2:error: libxml2 not found
解决:yum install -y libxml2-devel

报错3:error: Cannot find OpenSSL\'s
解决:yum install -y openssl-devel

报错4:error: cURL version 7.15.5 or later is required
解决:yum install -y libcurl-devel

报错5:configure: error: jpeglib.h not found
解决:yum install -y libjpeg-turbo-devel

报错6:configure: error: png.h not found
解决:yum install -y libpng-devel

报错7:configure: error: freetype-config not found
解决:yum install -y freetype-devel

报错8:configure: error: wrong mysql library version or lib not found

该错误表示当前安装的MySQL/MariaDB版本过高,下载个低版本的MySQL包将编译参数含有MySQL/MariaDB的路径改为低版本MySQL程序目录即可,无需安装低版本MySQL

解决:
1.下载MySQL5.6二进制包:

[root@linux ~]# cd /usr/local/src/
[root@linux src]# wget http://mirrors.163.com/mysql/Downloads/MySQL-5.6/mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz

2.解压:

[root@linux src]# tar -zxvf mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz 

3.移动到/usr/local/目录下重命名为mysql5.6:

[root@linux src]# mv mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz /usr/local/mysql5.6

4.更改编译参数中 - -with-mysql,- -with-mysqli,- -with-pdo-mysql指向的路径为MySQL5.6目录:
./configure --prefix=/usr/local/php-fpm --with-config-file-path=/usr/local/php-fpm/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql5.6 --with-mysqli=/usr/local/mysql5.6/bin/mysql_config --with-pdo-mysql=/usr/local/mysql5.6 --with-mysql-sock=/tmp/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --with-pear --with-curl --with-openssl

报错9:ERROR: [pool www] cannot get uid for user \'php-fpm\'
创建编译参数中指定的php用户即可
解决:useradd php-fpm


nginx官网:http://nginx.org/

编译安装nginx:

安装包存放路径/usr/local/src

下载:

[root@linux src]# wget http://nginx.org/download/nginx-1.16.1.tar.gz

解压:

[root@linux src]# tar -zxvf nginx-1.16.1.tar.gz 

进入nginx源码包目录:

[root@linux src]# cd nginx-1.16.1/

配置编译参数:

[root@linux nginx-1.16.1]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module

编译安装:

[root@linux nginx-1.16.1]# make && make install

启动nginx:

[root@linux ~]# /usr/local/nginx/sbin/nginx 
[root@linux ~]# netstat -lntp | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      11494/nginx: master

补充:
如果不想使用绝对路径启动nginx,可以将nginx启动脚本软连接到环境变量目录下:

[root@linux ~]# ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx

启动nginx:

[root@linux ~]# nginx 
[root@linux ~]# ps -ef|grep nginx
root     11580     1  0 21:58 ?        00:00:00 nginx: master process nginx
nobody   11581 11580  0 21:58 ?        00:00:00 nginx: worker process
root     11584  7705  0 21:59 pts/0    00:00:00 grep --color=auto nginx

关闭nginx:

[root@linux ~]# nginx -s stop

yum安装nginx:

官方说明:http://nginx.org/en/linux_packages.html

配置nginx仓库:

[root@linux ~]# vi /etc/yum.repos.d/nginx.repo

添加以下内容:

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

yum安装nginx:

[root@linux ~]# yum -y install nginx

配置文件路径:

[root@linux ~]# ls /etc/nginx/
conf.d          koi-utf  mime.types  nginx.conf   uwsgi_params
fastcgi_params  koi-win  modules     scgi_params  win-utf

启动脚本路径:

[root@linux ~]# which nginx
/usr/sbin/nginx

启动nginx:

[root@linux ~]# systemctl start nginx
[root@linux ~]# ps -ef|grep nginx
root     12410     1  0 22:42 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/ngin/nginx.conf
nginx    12411 12410  0 22:42 ?        00:00:00 nginx: worker process
root     12413  7705  0 22:42 pts/0    00:00:00 grep --color=auto nginx

关闭nginx:

[root@linux ~]# systemctl stop nginx

通过修改nginx配置文件配置nginx虚拟主机,一台nginx服务器(web服务器)可以通过虚拟主机配置多个站点

配置单个虚拟主机:

编辑nginx虚拟主机配置文件,由于上一篇文章使用的是yum安装的nginx,所以nginx虚拟主机配置文件路径为:/etc/nginx/conf.d/default.conf

编辑配置文件:

[root@linux ~]# vi /etc/nginx/conf.d/default.conf 

定义server_name项对应的站点域名:

验证配置文件:

[root@linux ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重新加载nginx使其生效:

[root@linux ~]# nginx -s reload

由于www.test.com为测试使用的自定义域名,并不能解析到测试的nginx服务器,需要修改Windows配置文件hosts:

打开配置文件:C:\\Windows\\System32\\drivers\\etc\\hosts

添加web服务器地址和对应的域名:

在浏览器访问www.test.com即可:

配置多个虚拟主机:

拷贝虚拟主机配置文件:

[root@linux ~]# cd /etc/nginx/conf.d/
[root@linux conf.d]# ls
default.conf
[root@linux conf.d]# cp default.conf testbaidu.conf

#虚拟主机文件命名为需要配置的域名便于维护

编辑文件:

[root@linux conf.d]# vi testbaidu.conf 

定义server_name项对应的站点域名和root项对应的站点目录:

验证并重载nginx:

[root@linux ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@linux ~]# nginx -s reload

创建站点对应目录以及index.html文件:

[root@linux conf.d]# mkdir -p /tmp/testbaidu
[root@linux conf.d]# touch /tmp/testbaidu/index.html
[root@linux conf.d]# echo nginx_test > $?

修改Windows系统host文件:

访问www.testbaidu.com即可:

默认虚拟主机:

默认虚拟主机的作用就是:如果有多个访问的域名指向这台web服务器,但某个域名未添加到nginx虚拟主机中,就会访问默认虚拟主机(泛解析)

查看虚拟主机配置文件目录:/etc/nginx/conf.d/

[root@linux conf.d]# ls
default.conf  testbaidu.conf

#当该目录中存在多个虚拟主机配置文件时,排在最前面的就是默认虚拟主机

上文修改Windows系统hosts文件时,多添加了一个域名www.abc.com,该域名未配置虚拟主机,访问就会访问默认虚拟主机

测试:

#加载的结果为默认虚拟主机default.conf指向的页面

设置testbaidu.conf为默认虚拟主机,编辑testbaidu.conf:

[root@linux conf.d]# vi testbaidu.conf 

在端口号后面添加default即可:

验证并重启nginx:

[root@linux conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@linux conf.d]# nginx -s reload

再次访问www.abc.com:

#显示结果为testbaidu.conf的虚拟主机页面

禁用nginx默认虚拟主机:

#在默认虚拟主机配置文件中添加一行:deny all; 即可

禁用nginx默认虚拟主机后,访问的域名如未在nginx虚拟主机配置文件server_name项定义的话就会提示403错误:

新网虚拟主机