linux 安装nginx+php+mysql
nginx安装javascript
本文是介绍使用源码编译安装,包括具体的编译参数信息。php
正式开始前,编译环境gcc g++ 开发库之类的须要提早装好。css
安装make:html
yum -y install gcc automake autoconf libtool make
安装g++:java
yum install gcc gcc-c++node
通常咱们都须要先装pcre, zlib,前者为了重写rewrite,后者为了gzip压缩。mysql
1.选定源码目录linux
能够是任何目录,本文选定的是/usr/local/srcnginx
cd /usr/local/src
2.安装PCRE库c++
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz 下载最新的 PCRE 源码包,使用下面命令下载编译和安装 PCRE 包:
复制代码
cd /usr/local/src
wget 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
make && make install
复制代码
3.安装zlib库
http://zlib.net/zlib-1.2.11.tar.gz 下载最新的 zlib 源码包,使用下面命令下载编译和安装 zlib包:
复制代码
cd /usr/local/src
wget http://zlib.net/zlib-1.2.11.tar.gz
tar -zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make && make install
复制代码
4.安装ssl(某些×××默认没装ssl)
cd /usr/local/src
wget https://www.openssl.org/source/openssl-1.1.0b.tar.gz
tar -zxvf openssl-1.1.0b.tar.gz
./config
make && make install
5.安装nginx
以安装最新稳定版ngixn-1.10.2为例子,其余版本也同样。
下面是把 Nginx 安装到 /usr/local/nginx 目录下的详细步骤:
cd /usr/local/src
wget http://nginx.org/download/nginx-1.10.2.tar.gz
tar -zxvf nginx-1.10.2.tar.gz
cd nginx-1.10.2
一、添加nginx用户和用户组
groupadd -r nginx
useradd -r -g nginx nginx
你可能收到提示已经有了nginx 用户和组这个无所谓有了更好。
二、配置nginx安装参数
复制代码
./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/nginx.conf \
--pid-path=/usr/local/nginx/nginx.pid \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-pcre=/usr/local/src/pcre-8.39 \
--with-zlib=/usr/local/src/zlib-1.2.11 \
--with-openssl=/usr/local/src/openssl-1.1.0b \
复制代码
注:当前所在目录必定要是/usr/local/src/nginx-1.10.2
--with-pcre=/usr/local/src/pcre-8.39 指的是pcre-8.39 的源码路径。
--with-zlib=/usr/local/src/zlib-1.2.11 指的是zlib-1.2.11 的源码路径。
--with-openssl=/usr/local/src/openssl-1.1.0b 指的是openssl-1.1.0b 的源码路径。
编译安装
1 make && make install
安装成功后 /usr/local/nginx 目录下以下:
三、启动
确保系统的 80 端口没被其余程序占用,运行/usr/local/nginx/sbin/nginx 命令来启动 Nginx
netstat -ano|grep 80
/usr/local/nginx/sbin/nginx
可能会出现mkdir /var/tmp/nginx/client failed的提示
这时手动建立就能够了 mkdir -p /var/tmp/nginx/client就能够了
打开浏览器访问此机器的 IP,若是浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功
ps:
若nginx成功运行后,经过ip地址没法访问服务器 则多是防火墙的问题 须要开放80端口或关闭防火墙
开启80端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
命令含义:
--zone #做用域
--add-port=80/tcp #添加端口,格式为:端口/通信协议
--permanent #永久生效,没有此参数重启后失效
重启防火墙
firewall-cmd --reload
或者永久关闭防火墙
systemctl stop firewalld.service //中止防火墙
systemctl disable firewalld.service //再也不开机中止运行防火墙
nginx编译选项
make是用来编译的,它从Makefile中读取指令,而后编译。
make install是用来安装的,它也从Makefile中读取指令,安装到指定的位置。
configure命令是用来检测你的安装平台的目标特征的。它定义了系统的各个方面,包括nginx的被容许使用的链接处理的方法,好比它会检测你是否是有CC或GCC,并非须要CC或GCC,它是个shell脚本,执行结束时,它会建立一个Makefile文件。nginx的configure命令支持如下参数:
--prefix=path 定义一个目录,存放服务器上的文件 ,也就是nginx的安装目录。默认使用 /usr/local/nginx。
--sbin-path=path 设置nginx的可执行文件的路径,默认为 prefix/sbin/nginx.
--conf-path=path 设置在nginx.conf配置文件的路径。nginx容许使用不一样的配置文件启动,经过命令行中的-c选项。默认为prefix/conf/nginx.conf.
--pid-path=path 设置nginx.pid文件,将存储的主进程的进程号。安装完成后,能够随时改变的文件名 , 在nginx.conf配置文件中使用 PID指令。默认状况下,文件名 为prefix/logs/nginx.pid.
--error-log-path=path 设置主错误,警告,和诊断文件的名称。安装完成后,能够随时改变的文件名 ,在nginx.conf配置文件中 使用 的error_log指令。默认状况下,文件名 为prefix/logs/error.log.
--http-log-path=path 设置主请求的HTTP服务器的日志文件的名称。安装完成后,能够随时改变的文件名 ,在nginx.conf配置文件中 使用 的access_log指令。默认状况下,文件名 为prefix/logs/access.log.
--user=name 设置nginx工做进程的用户。安装完成后,能够随时更改的名称在nginx.conf配置文件中 使用的 user指令。默认的用户名是nobody。
--group=name 设置nginx工做进程的用户组。安装完成后,能够随时更改的名称在nginx.conf配置文件中 使用的 user指令。默认的为非特权用户。
--with-select_module --without-select_module 启用或禁用构建一个模块来容许服务器使用select()方法。该模块将自动创建,若是平台不支持的kqueue,epoll,rtsig或/dev/poll。
--with-poll_module --without-poll_module 启用或禁用构建一个模块来容许服务器使用poll()方法。该模块将自动创建,若是平台不支持的kqueue,epoll,rtsig或/dev/poll。
--without-http_gzip_module — 不编译压缩的HTTP服务器的响应模块。编译并运行此模块须要zlib库。
--without-http_rewrite_module 不编译重写模块。编译并运行此模块须要PCRE库支持。
--without-http_proxy_module — 不编译http_proxy模块。
--with-http_ssl_module — 使用https协议模块。默认状况下,该模块没有被构建。创建并运行此模块的OpenSSL库是必需的。
--with-pcre=path — 设置PCRE库的源码路径。PCRE库的源码(版本4.4 - 8.30)须要从PCRE网站下载并解压。其他的工做是Nginx的./ configure和make来完成。正则表达式使用在location指令和 ngx_http_rewrite_module 模块中。
--with-pcre-jit —编译PCRE包含“just-in-time compilation”(1.1.12中, pcre_jit指令)。
--with-zlib=path —设置的zlib库的源码路径。要下载从 zlib(版本1.1.3 - 1.2.5)的并解压。其他的工做是Nginx的./ configure和make完成。ngx_http_gzip_module模块须要使用zlib 。
--with-cc-opt=parameters — 设置额外的参数将被添加到CFLAGS变量。例如,当你在FreeBSD上使用PCRE库时须要使用:--with-cc-opt="-I /usr/local/include。.如须要须要增长 select()支持的文件数量:--with-cc-opt="-D FD_SETSIZE=2048".
--with-ld-opt=parameters —设置附加的参数,将用于在连接期间。例如,当在FreeBSD下使用该系统的PCRE库,应指定:--with-ld-opt="-L /usr/local/lib".
php-fpm安装配置
nginx自己不能处理PHP,它只是个web服务器,当接收到请求后,若是是php请求,则发给php解释器处理,并把结果返回给客户端。
nginx通常是把请求发fastcgi管理进程处理,fascgi管理进程选择cgi子进程处理结果并返回被nginx
本文以php-fpm为例介绍如何使nginx支持PHP
1、编译安装php-fpm
什么是PHP-FPM
PHP-FPM是一个PHP FastCGI管理器,是只用于PHP的,能够在 http://php-fpm.org/download下载获得.
PHP-FPM实际上是PHP源代码的一个补丁,旨在将FastCGI进程管理整合进PHP包中。必须将它patch到你的PHP源代码中,在编译安装PHP后才可使用。
新版PHP已经集成php-fpm了,再也不是第三方的包了,推荐使用。PHP-FPM提供了更好的PHP进程管理方式,能够有效控制内存和进程、能够平滑重载PHP配置,比spawn-fcgi具备更多优势,因此被PHP官方收录了。在./configure的时候带 –enable-fpm参数便可开启PHP-FPM,其它参数都是配置php的,具体选项含义能够查看这里。
安装前准备
添加 epel 源
安装依赖
1 yum -y install gcc gcc-c++ glibc
yum -y install libmcrypt-devel mhash-devel libxslt-devel \
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
复制代码
cd /usr/local/src
wget http://au1.php.net/get/php-7.1.10.tar.gz/from/this/mirror
tar -zvxf php-7.1.10.tar.gz
cd php-7.1.10
./configure --prefix=/usr/local/php --enable-fpm --with-mcrypt \
--enable-mbstring --enable-pdo --with-curl --disable-debug --disable-rpath \
--enable-inline-optimization --with-bz2 --with-zlib --enable-sockets \
--enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex \
--with-mhash --enable-zip --with-pcre-regex --with-pdo-mysql --with-mysqli \
--with-gd --with-jpeg-dir --with-freetype-dir --enable-calendar
make && make install
复制代码
以上就完成了php-fpm的安装
下面是对php-fpm运行用户进行设置
一、为php提供配置文件
1 cp php.ini-production /usr/local/php/etc/php.ini
二、为php-fpm提供配置文件
1 cd /usr/local/php
2 cp etc/php-fpm.conf.default etc/php-fpm.conf
3 vim etc/php-fpm.conf
修改
user = www
group = www
若是www用户不存在,那么先添加www用户
1 groupadd www
2 useradd -g www www
修改
pm.max_children = 150
pm.start_servers = 8
pm.min_spare_servers = 5
pm.max_spare_servers = 10
pid = /usr/local/php/var/run/php-fpm.pid
配置文件
须要注意的是php7中www.conf这个配置文件配置phpfpm的端口号等信息,若是你修改默认的9000端口号需在这里改,再改nginx的配置
重启
使用以下命令来验正(若是此命令输出有中几个php-fpm进程就说明启动成功了):
1 ps aux | grep php-fpm
三、nginx和php-fpm整合
编辑/usr/local/nginx/nginx.conf
1 vim /usr/local/nginx/nginx.conf
复制代码
user www www;
worker_processes auto;
#error_log /usr/local/wwwlogs/error_nginx.log crit;
pid /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 51200;
events {
use epoll;
worker_connections 51200;
multi_accept on;
}
http {
include mime.types;
default_type application/octet-stream;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 1024m;
sendfile on;
tcp_nopush on;
keepalive_timeout 120;
server_tokens off;
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 Compression
gzip on;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_proxied any;
gzip_vary on;
gzip_types
text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
text/javascript application/javascript application/x-javascript
text/x-json application/json application/x-web-app-manifest+json
text/css text/plain text/x-component
font/opentype application/x-font-ttf application/vnd.ms-fontobject
image/x-icon;
gzip_disable "MSIE [1-6].(?!.*SV1)";
#If you have a lot of static files to serve through Nginx then caching of the files' metadata (not the actual files' contents) can save some latency.
open_file_cache max=1000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
######################## default ############################
server {
listen 80;
servername ;
#access_log /usr/local/wwwlogs/access_nginx.log combined;
root /home/www/default;
index index.html index.htm index.php;
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
location ~ [^/].php(/|$) {
#fastcgi_pass remote_php_ip:9000;
fastcgi_pass unix:/dev/shm/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
#location ~ .php$ {
# root /home/www;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# include fastcgi_params;
#}
location ~ .*.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
expires 30d;
access_log off;
}
location ~ .*.(js|css)?$ {
expires 7d;
access_log off;
}
}
########################## vhost #############################
include vhost/*.conf;
}
复制代码
项目路径
1 mkdir /usr/local/nginx/vhost
2 touch www.qyi.com.conf
3 vim www.qyi.com.conf
复制代码
server {
charset utf-8;
client_max_body_size 200M;
listen 80;
#listen [::]:80 default_server ipv6only=on; ## listen for ipv6
listen 443 ssl;
# server_name localhost;
# ssl on;
ssl_certificate /usr/local/ssl/beta-v2.crt;
ssl_certificate_key /usr/local/ssl/beta-v2.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ALL:!DH:!EXPORT:!RC4:+HIGH:+MEDIUM:-LOW:!aNULL:!eNULL;
ssl_prefer_server_ciphers on;
server_name www.qyi.com;
root /home/www/www.qyi.com;
#access_log /home/www/www.qyi.com/advanced.access.log main buffer=50k;
error_log /usr/local/nginx/logs/bella.error.log warn;
location / {
root /home/www/www.qyi.com/frontend/web;
try_files $uri /frontend/web/index.php?$args;
# avoiding processing of calls to non-existing static files by Yii
location ~ .(js|css|ttf|woff|woff2|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
access_log off;
expires 360d;
try_files $uri =404;
}
}
location /admin {
alias /home/www/www.qyi.com/backend/web;
rewrite ^(/admin)/$ $1 permanent;
try_files $uri /backend/web/index.php?$args;
}
# avoiding processing of calls to non-existing static files by Yii
location ~ ^/admin/(.+.(js|css|ttf|woff|woff2|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar))$ {
access_log off;
expires 360d;
rewrite ^/admin/(.+)$ /backend/web/$1 break;
rewrite ^/admin/(.+)/(.+)$ /backend/web/$1/$2 break;
try_files $uri =404;
}
location /v1 {
alias /home/www/www.qyi.com/api/v1/web;
rewrite ^(/v1)/$ $1 permanent;
try_files $uri /api/v1/web/index.php?$args;
}
location /v2 {
alias /home/www/www.qyi.com/api/v2/web;
rewrite ^(/v2)/$ $1 permanent;
try_files $uri /api/v2/web/index.php?$args;
}
location ~ .php$ {
include fastcgi_params;
# check your /etc/php5/fpm/pool.d/www.conf to see if PHP-FPM is listening on a socket or port
#fastcgi_pass unix:/var/run/php5-fpm.sock; ## listen for socket
#fastcgi_pass 127.0.0.1:9000; ## listen for port
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#try_files $uri =404;
fastcgi_pass unix:/dev/shm/php-cgi.sock;
fastcgi_index index.php;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
include fastcgi.conf;
}
#error_page 404 /404.html;
location = /requirements.php {
deny all;
}
location = /palmobo.net.html{
try_files $uri =404;
}
location ~ .(ht|svn|git) {
deny all;
}
}
复制代码
phpmyadmin路径
1 touch www.mysql.com.conf
2 vim www.mysql.com.conf
复制代码
server {
listen 80;
server_name www.mysql.com;
access_log off;
index index.html index.htm index.php;
#include /usr/local/nginx/vhost/www.mysql.com.conf;
root /home/www/www.mysql.com;
location ~ [^/].php(/|$) {
#fastcgi_pass remote_php_ip:9000;
fastcgi_pass unix:/dev/shm/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ ..(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
expires 30d;
access_log off;
}
location ~ ..(js|css)?$ {
expires 7d;
access_log off;
}
}
复制代码
修改以下:
红框中“#”号去掉
红框中“#”号去掉
添加红框中内容
改为以下
从新载入nginx的配置文件:
1 /usr/loca/nginx/sbin/nginx -s reload
四、测试php文件
在/usr/local/nginx/html下建立index.php文件,输入以下内容
1 <?php
2 phpinfo();
3 ?>
安装Mysql5.6.37(二进制安装)
下载地址:
https://dev.mysql.com/downloads/mysql/5.6.html
1.下载并上传到服务器中,进行解压,解压到/usr/local下
[root@zhang ~]# tar xvf mysql-5.6.37-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
改下文件名,这个太长了
[root@zhang local ]# mv mysql-5.6.37-linux-glibc2.12-x86_64 mysql
2.建立mysql的用户和组
建立组:
[root@zhang ~]# groupadd -r -g 306 mysql
建立用户:
[root@zhang ~]# useradd -g 306 -r -u 306 mysql
3.修改目录的全部者和所属组:
[root@zhang ~]# chown -R mysql.mysql /usr/local/mysql/*
建立存储数据文件的目录(若是有的话就不用建立了)
[root@zhang ~]# mkdir -p /usr/local/mysql/mydata/data
修改全部者和所属组
[root@zhang ~]# chown -R mysql.mysql /usr/local/mysql/mydata
ll mydata
4.初始化MySQL:
[root@zhang mysql]# scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql/mydata/data/
5.建立初始化进程,并添加到/etc/init.d/下
[root@zhang mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@zhang mysql]# chkconfig --add mysqld
6.配置mysql的配置文件
[root@zhang mysql]# vim /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/mydata/data
#socket=/var/lib/mysql/mysql.sock
#user=mysql
#symbolic-links=0
basedir=/usr/local/mysql
port=3306
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
#[mysqld_safe]
#log-error=/var/log/mysqld.log
#pid-file=/var/run/mysqld/mysqld.pid
7.启动mysql进程
[root@zhang mysql]# service mysqld start
8.查看进程是否启动:
[root@zhang mysql]# lsof -i:3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 26961 mysql 10u IPv6 67261 0t0 TCP *:mysql (LISTEN)
9.配置环境变量:
[root@zhang ~]# vim .bash_profile
修改下PATH变量
PATH=$PATH:$HOME/bin:/usr/local/mysql/bin
export PATH
10.给mysql添加密码
[root@zhang ~]# mysqladmin -u root password 'zhang'
11.登陆mysql
[root@zhang ~]# mysql -uroot -p
Enter password: zhang
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.02 sec)
基本就可使用了,这样mysql就完全配置完成了