记录一次 nginx+php+mysql

ssh连接错误用

rm -rf ~/.ssh/known_hosts

1.添加用户等等

adduser ***
passwd ***
以后登陆这个用户



# web 用户和组
groupadd www
useradd -g www www -s /sbin/nologin

# 网站目录
mkdir -p /data/htdocs

# 可写目录进行如下设置
# chown -R www:www /path 自己的根目录

# 日志目录
mkdir -p /data/logs

# 创建软件包下载和编译目录,后续软件都下载到这里
mkdir -p /data/software

# 创建软件安装目录,PHP、Nginx 将安装到这里
mkdir -p /usr/local/webserver

# 如果系统自带 Apache、PHP、MySQL,先卸载
yum remove httpd
yum remove php
yum remove mysql

# 升级所有软件包
yum update -y

2.安装依赖

yum -y install gcc gcc-c++ autoconf libjpeg-turbo-static libjpeg-turbo-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel pcre pcre-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-libs krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss-pam-ldapd openldap-clients openldap-servers bison lrzsz libmcrypt libmcrypt-devel mcrypt mhash ImageMagick ImageMagick-devel libmemcached libmemcached-devel

# libiconv:提供了一个iconv()的函数,以实现一个字符编码到另一个字符编码的转换
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.15.tar.gz
tar zxvf libiconv-1.15.tar.gz
cd libiconv-1.15
./configure --prefix=/usr/local
make
make install

# vim /etc/ld.so.conf 加一行 /usr/local/lib,再执行如下
/sbin/ldconfig

3下载php



获取并解压 PHP :

tar zxvf php-5.6.30.tar.gz
配置并构建 PHP。在此步骤您可以使用很多选项自定义 PHP,例如启用某些扩展等。 运行 ./configure --help 命令来获得完整的可用选项清单。 在本示例中,我们仅进行包含 PHP-FPM 和 MySQL 支持的简单配置。

cd php-5.6.30
./configure --enable-fpm --with-mysql
或者
./configure --prefix=/usr/local/webserver/php --with-config-file-path=/usr/local/webserver/php/etc --with-mysql --with-mysqli --with-curl --with-mcrypt --with-gd --with-openssl --with-mhash --with-xmlrpc --with-gettext --with-bz2 --with-zlib --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-libxml-dir --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-mbregex --enable-mbstring --enable-gd-native-ttf --enable-pcntl --enable-sockets --enable-zip --enable-soap --enable-ftp --enable-exif --enable-opcache --enable-fpm --with-fpm-user=www --with-fpm-group=www --without-pear
---------------------
# 如果遇到 undefined reference to `libiconv_open' 错误
# make 后面增加 ZEND_EXTRA_LIBS='-liconv'
make ZEND_EXTRA_LIBS='-liconv'
---------------------



make
---------------------
在安裝
 PHP 到系统中时要是发生「undefined reference to libiconv_open'」之类的错误信息,那表示在「./configure 」沒抓好一些环境变数值。错误发生点在建立「-o sapi/cli/php」是出错,没給到要 link 的 iconv 函式库参数。 解决方法:vim Makefile 编辑Makefile 大约77 行左右的地方: EXTRA_LIBS = ..... -lcrypt 在最后加上 -liconv,例如: EXTRA_LIBS = ..... -lcrypt -liconv 然后重新再次 make 即可。

或者用另一种办法

make ZEND_EXTRA_LIBS='-liconv'

ln -s /usr/local/lib/libiconv.so.2 /usr/lib64/
--------------------- 

sudo make install
创建配置文件,并将其复制到正确的位置。

# 创建配置文件
cp php.ini-production /usr/local/webserver/php/etc/php.ini

# 复制启动脚本到 init.d 目录并修改权限
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod 700 /etc/init.d/php-fpm

#复制php-fpm.conf
cp /usr/local/webserver/php/etc/php-fpm.conf.default /usr/local/webserver/php/etc/php-fpm.conf

# 设置开机启动
chkconfig php-fpm on

# 查看开机启动服务
chkconfig --list

# 启动服务
service php-fpm start

# 停止服务
service php-fpm stop

# 重启服务
service php-fpm reload

# 创建软连接,方便全局执行 php-fpm start | stop | reload
ln -s /usr/local/webserver/php/bin/php /usr/bin/php
ln -s /etc/init.d/php-fpm /usr/bin/php-fpm

安装 PHP 扩展
# memcache 扩展
# memcached 支持 Binary Protocol,而 memcache 不支持,意味着 memcached 会有更高的性能
# https://pecl.php.net/package/memcached
# 3.x.x 支持 php 7,2.x.x 支持 php 5.2-5.6
wget https://pecl.php.net/get/memcached-2.2.0.tgz
tar zxvf memcached-2.2.0.tgz
cd memcached-2.2.0
/usr/local/webserver/php/bin/phpize
./configure --with-php-config=/usr/local/webserver/php/bin/php-config
make
make install

# 如果需要再本地缓存业务侧数据,安装 apcu
# apc 包含 opcode 缓存和 KV 数据缓存
# PHP 5.5.0 及后续版本中已经绑定了 OPcache 扩展,所以不需要安装 apc
# apcu 只包含 KV 数据缓存
# https://pecl.php.net/package/APCu
wget https://pecl.php.net/get/apcu-4.0.11.tgz
tar zxvf apcu-4.0.11.tgz
cd apcu-4.0.11
/usr/local/webserver/php/bin/phpize
./configure --with-php-config=/usr/local/webserver/php/bin/php-config
make
make install

# 图片处理 ImageMagick 扩展
# https://pecl.php.net/package/imagick
wget https://pecl.php.net/get/imagick-3.4.3.tgz
tar zxvf imagick-3.4.3.tgz
cd imagick-3.4.3
/usr/local/webserver/php/bin/phpize
./configure --with-php-config=/usr/local/webserver/php/bin/php-config
如果出现错误
 yum install libmemcached libmemcached-devel
继续
make
make install

4.配置php

vim /usr/local/webserver/php/etc/php.ini
; 该选项设置为 On 时,将在所有的脚本中使用输出控制
output_buffering = On

; 将 PHP 所能打开的文件限制在指定的目录树
open_basedir = /data/htdocs/:/tmp/

; 禁用函数
disable_functions = system,passthru,shell_exec,escapeshellarg,escapeshellcmd,proc_close,proc_open,proc_get_status,dl,chroot,show_source,syslog,readlink,symlink,popepassthru,stream_socket_server

; 禁止暴露 PHP 被安装在服务器上
expose_php = Off

; 禁止错误信息输出
display_errors = Off

; 错误信息记录到服务器错误日志
log_errors = On

; 设置脚本错误将被记录到的文件
; 该文件必须是web服务器用户可写的
; mkdir /data/logs
; touch /data/logs/php_error.log
; chown www:www /data/logs/php_error.log
error_log = /data/logs/php_error.log

; 传递给存储处理器的参数
session.save_path = "/tmp"

; 防止 Nginx 文件类型错误解析漏洞
cgi.fix_pathinfo = 0

; 文件上传临时目录
upload_tmp_dir = /tmp

; 时区
date.timezone = "Asia/Shanghai"

; 允许使用 PHP 代码开始标志的缩写形式
short_open_tag = On

; php 扩展目录
; php 5.2
; extension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/"
; php 5.4
; extension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20100525/"
; php 5.5
; extension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20121212/"
extension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20131226/"

; 文件结尾添加扩展配置,按需添加
[apcu]
extension = "apcu.so"
apc.enabled = on
apc.shm_size = 128M
; apc.enable_cli = on
[memcached]
extension = "memcached.so"
[imagick]
extension = "imagick.so"
[opcache]
zend_extension="opcache.so"
opcache.enable=1
; 具体参数配置根据实际情况,默认不配置
; opcache.memory_consumption=128
; opcache.interned_strings_buffer=8
; opcache.max_accelerated_files=4000
; opcache.revalidate_freq=60
; opcache.fast_shutdown=1
; opcache.enable_cli=1
修改 php-fpm.conf 配置文件
 cp /usr/local/webserver/php/etc/php-fpm.conf.default /usr/local/webserver/php/etc/php-fpm.conf
vim /usr/local/webserver/php/etc/php-fpm.conf
; 查找并修改如下配置,其他保持默认

; 错误日志的位置
error_log = /data/logs/php-fpm.log
; 错误级别
log_level = error
; 如果子进程在设定的时间内收到该参数设定次数的 SIGSEGV 或者 SIGBUS退出信息号,则FPM会重新启动
emergency_restart_threshold = 10
; 用于设定平滑重启的间隔时间
emergency_restart_interval = 1m
; 设置子进程接受主进程复用信号的超时时间
process_control_timeout = 5s
; 设置 FPM 在后台运行
daemonize = yes
; 设置允许连接到 FastCGI 的服务器 IPV4 地址
listen.allowed_clients = 127.0.0.1
; 子进程的数量是固定的
pm = static
; pm 设置为 static 时表示创建的子进程的数量
pm.max_children = 64
; 设置启动时创建的子进程数目。仅在 pm 设置为 dynamic 时使用
pm.start_servers = 10
; 设置空闲服务进程的最低数目。仅在 pm 设置为 dynamic 时使用
pm.min_spare_servers = 10
; 设置空闲服务进程的最大数目。仅在 pm 设置为 dynamic 时使用
pm.max_spare_servers = 30
; 设置每个子进程重生之前服务的请求数
pm.max_requests = 500
; FPM 状态页面的网址
pm.status_path = /phpfpm_status
; 设置单个请求的超时中止时间
request_terminate_timeout = 30
; 设置文件打开描述符的 rlimit 限制
rlimit_files = 65535
; 禁止输出错误信息
php_flag[display_errors] = off

启动 php
# 修改文件句柄数为 65535
ulimit -SHn 65535

# 启动 php-cgi 进程
# 监听 127.0.0.1 的 9000 端口
# 进程数为 64(如果服务器内存小于3GB,可以只开启64个进程)
# 用户为www
php-fpm start

3.下载nginx并解压

wget http://nginx.org/download/nginx-1.14.0.tar.gz

4.安装nginx

tar zxvf nginx-1.14.0.tar.gz
cd nginx-1.14.0/
# --prefix=PATH:指定nginx的安装目录
# --user=name:设置nginx工作进程的用户
# --group=name:设置nginx工作进程的组
# --with-pcre:设置PCRE库的源码路径,如果已通过 yum 方式安装,使用 --with-pcre 自动找到库文件
# --with-http_stub_status_module:用来监控 Nginx 的当前状态
# --with-http_ssl_module:使用https协议模块
# --with-http_realip_module:通过这个模块允许我们改变客户端请求头中客户端IP地址值
# 更多配置参考 https://tengine.taobao.org/nginx_docs/cn/docs/install.html

./configure --prefix=/usr/local/webserver/nginx --user=www --group=www --with-pcre --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module
make
make install

ln -s /usr/local/webserver/nginx/sbin/nginx /usr/bin/

升级 nginx
# make 之后不要 make install
mv /usr/local/webserver/nginx/sbin/nginx /usr/local/webserver/nginx/sbin/nginx.old
cp objs/nginx /usr/local/webserver/nginx/sbin/
nginx -t
kill -USR2 `cat /usr/local/webserver/nginx/logs/nginx.pid`
kill -QUIT `cat /usr/local/webserver/nginx/logs/nginx.pid.oldbin`
nginx -v

5.配置nginx

vim /usr/local/nginx/conf/nginx.conf 

网上
# vi /usr/local/webserver/nginx/conf/nginx.conf
# 查找并修改如下配置,其他保持默认

# 运行的用户和用户组
user  www www;
# 全局错误日志和级别
error_log  /data/logs/nginx_error.log  error;
# 进程文件
pid  /usr/local/webserver/nginx/logs/nginx.pid;
# 单个进程打开的最多文件描述符数目
worker_rlimit_nofile 65535;

events 
{
    # 事件模型,epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型
    use epoll;
    # 单个进程可以处理的最大连接数
    worker_connections 65535;
}

http {
    # 日志名称和格式
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    # 允许客户端请求的最大单文件字节数
    client_max_body_size 8m;
    # 防止网络阻塞
    tcp_nopush on;
    # 开启gzip压缩
    gzip on;
    # 隐藏 nginx 的版本信息
    server_tokens off;
    # 包含其它自定义虚拟主机配置文件
    # mkdir /usr/local/webserver/nginx/conf/vhost
    include vhost/*.conf;

    # 注释掉 server { } 部分的默认配置
}


#按i可以插入 。。。
#找到这里 修改
server {
        listen       80;
        server_name  www.zuoer.xin;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.php index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
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;
        }

}
下面非必须配置
server {
        listen       443 ssl;
        server_name  www.zuoer.xin;

        ssl_certificate      cert.pem;
        ssl_certificate_key  cert.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location / {
            root   html;
            index  index.php index.html index.htm;
        }
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;
        }
    }

#上传证书到服务器 scp -p ./*****zuoer.xin_nginx.zip root@*****:/usr/local/nginx/conf
#解压 unzip *****zuoer.xin_nginx.zip

拷贝
cp cert.key /usr/local/nginx/conf/cert.key
cp cert.pem /usr/local/nginx/conf/cert.pem

如果出现错误
[root@ sbin]# ./nginx -t
nginx: [emerg] unknown directive "ssl" in /usr/local/nginx/conf/nginx.conf:98
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
错误解决步骤

既然在安装的时候没有编译ssl,难道把nginx卸载重新安装一次?不不不,我们只需要在原有的基础上添加ssl模块就行了。

步骤一:我们先来到当初下载nginx的包压缩的解压目录,如果你是看小编写的教程安装的,解压目录应该在“/usr/loacl/src/”,绝大多数应该都是在这个目录下的,已经是一种规范了。

步骤二:来到解压目录下后,按顺序执行一下命令:

               命令1、./configure --with-http_ssl_module  //重新添加这个ssl模块

                  注意如果没有出现错误,则直接看命令2即可 

               执行以上一条命令出现这个错误(./configure:错误:SSL模块需要OpenSSL库。),原因是因为缺少了OpenSSL,那我们再来安装一个即可执行:yum -y install openssl openssl-devel

                等待OpenSSL的安装完成后,再执行./configure ,最后在执行” 命令1" 即可。

               命令2、执行make命令,但是不要执行make install,因为make是用来编译的,而make install是安装,不然你整个nginx会重新覆盖的。

               命令3、在我们执行完做命令后,我们可以查看到在nginx解压目录下,objs文件夹中多了一个nginx的文件,这个就是新版本的程序了。首先我们把之前的nginx先备份一下,然后把新的程序复制过去覆盖之前的即可。

               cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak

               cp objs/nginx /usr/local/nginx/sbin/nginx

               命令4,最后我们来到Nginx安装目录下,来查看是否有安装ssl模块成功。执行./sbin/nginx -V即可看到

6.开端口 


firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=443/tcp --permanent
firewall-cmd --zone=public --add-port=3306/tcp --permanent

7.启动nginx


nginx 开机启动
# vi /etc/rc.local
/usr/local/webserver/nginx/sbin/nginx

# 测试
# nginx -t
# 重启和停止
# nginx -s reload | stop

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

重启命令
/usr/local/nginx/sbin/nginx -s reload 

Nginx 站点加密
创建脚本 vi /usr/local/sbin/htpasswd.pl,输入
#!/usr/bin/perl
use strict;
my $pw=$ARGV[0] ;
print crypt($pw,$pw)."\n";
生成密码
chmod +x /usr/local/sbin/htpasswd.pl
# passwd 是要生成的密码
/usr/local/sbin/htpasswd.pl passwd
# 创建完成后删除 htpasswd.pl
创建存放用户名和密码的文件 vi /usr/local/webserver/nginx/conf/.htpasswd
输入 user:passwd,user 是用户名,passwd 是刚才生成的密码
在需要加密的 server 或者 location 中增加如下配置
auth_basic	"login...";
auth_basic_user_file	/usr/local/webserver/nginx/conf/.htpasswd

8.80定向443 (可选)


server {
        listen       80;
        server_name  zuoer.xin;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.php index.html index.htm;
        }
        rewrite ^(.*)$  https://$server_name$1 permanent;

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
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;
        }
}

9.安装mysql

wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum update
yum install mysql-server

权限设置:

chown mysql:mysql -R /var/lib/mysql
初始化 MySQL:

su 普通用户
mysqld --initialize
启动 MySQL:

systemctl start mysqld
查看 MySQL 运行状态:

systemctl status mysqld



service mysql start

mysqladmin -u root password "new_password";

#开远程 mysql -u root -p
use mysql
select t.host from user t where t.user='root';
update user set host='%' where user='root';
出错无碍
flush privileges;

php 双版本(没成功 记录一下)

./configure --prefix=/usr/local/php5 --with-config-file-path=/usr/local/php5/etc  --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-bz2 --with-openssl --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-mbstring --enable-sockets --enable-exif


yum install -y libxml2-devel 


yum install -y openssl-devel 


yum install -y bzip2-devel 


yum install -y libjpeg-devel 


yum install -y libpng-devel 


yum install -y freetype-devel 


yum install -y epel-release –需要先安装epel扩展源。借助第三方源安装 
yum install -y libmcrypt-devel 


./configure到这个提示完成

再make && make install

cp php.ini-production  /usr/local/php/etc/php.ini
#查看是否加载配置文件
/usr/local/php/bin/php -i |less

首先:find命令找下php在哪里先

#find / -name php



第一步:ln命令(百度下大把资源)主要用来创建软连接

现在主要是想将5版本换7版本:
ln -s /usr/local/php5/bin/php /usr/local/bin

或者
#ln -s /usr/local/php-5.5/bin/php(你想要换成的php版本的路径) /usr/sbin/php(最后一个php可以换成你自己喜欢的名字,最好php吧)

第二步:export命令将软连接加到PATH路径中

#export PATH="$PATH:/usr/sbin/php"(将上面你准备好的连接加进去就可以了,中间:冒号别漏了,用来做分割的)


最后:看下效果

#php -v


/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
 
重启命令
/usr/local/nginx/sbin/nginx -s reload 
 
server
{
	listen 80 default;
	server_name	_;
	access_log  off;
	deny all;
}

server
{
	listen 8080 default;
	server_name	_;
	access_log  off;
	deny all;
}

server {
	listen 80;
	server_name example.com;
	index index.html index.htm index.php;
	root  /data/htdocs/example.com;

	location / {
		rewrite ^(.*) https://$server_name$1 permanent;
	}
	access_log  off;
}

server
{
	listen	443;
	server_name  example.com;
	if ($host != 'example.com') {
		rewrite	^/(.*)$	http://www.example.com/	permanent;
	}
	index index.html index.htm index.php;
	root  /data/htdocs/example.com;

	ssl on;
	ssl_certificate ssl/example.com.crt;
	ssl_certificate_key ssl/example.com.key;
	
	auth_basic	"login...";
	auth_basic_user_file  /usr/local/webserver/nginx/conf/.htpasswd;

	location /status {
		stub_status on;
	}

	location /phpfpm_status {
		fastcgi_pass  127.0.0.1:9000;
		fastcgi_index index.php;
		fastcgi_param  HTTPS on;
		include fastcgi.conf;	
	}

	location ~ \.(php|php5)?$
	{      
		fastcgi_pass  127.0.0.1:9000;
		fastcgi_index index.php;
		fastcgi_param  HTTPS on;
		include fastcgi.conf;
	}
	
	location ~ \.(gif|jpg|jpeg|png|bmp|swf|js|css)$
	{
		expires      30d;
	}

	access_log  off;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值