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就完全配置完成了