linux 安裝nginx+php+mysql

nginx安裝

本文是介紹使用源碼編譯安裝,包括具體的編譯參數信息。

正式開始前,編譯環境gcc g++ 開發庫之類的需要提前裝好。

安裝make:

yum -y install gcc automake autoconf libtool make

安裝g++:

yum install gcc gcc-c++

 


一般我們都需要先裝pcre, zlib,前者爲了重寫rewrite,後者爲了gzip壓縮。

1.選定源碼目錄

可以是任何目錄,本文選定的是/usr/local/src

cd /usr/local/src

2.安裝PCRE庫

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(某些vps默認沒裝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

1、添加nginx用戶和用戶組

groupadd -r nginx
useradd -r -g nginx nginx

你可能收到提示已經有了nginx 用戶和組這個無所謂有了更好。

 

2、配置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 目錄下如下:

 

3、啓動

確保系統的 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

一、編譯安裝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 源

# rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

安裝依賴

# yum install gcc bison bison-devel zlib-devel libmcrypt-devel mcrypt mhash-devel openssl-devel libxml2-devel libcurl-devel bzip2-devel readline-devel libedit-devel sqlite-devel

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運行用戶進行設置

1、爲php提供配置文件

1 cp php.ini-production /usr/local/php/etc/php.ini

2、爲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 

 

配置文件

# cp php.ini-production /usr/local/php/etc/php.ini

# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf

# cp -R ./sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

 

需要注意的是php7中www.conf這個配置文件配置phpfpm的端口號等信息,如果你修改默認的9000端口號需在這裏改,再改nginx的配置

重啓

# killall php-fpm

#  service php-fpm restart

 

使用如下命令來驗正(如果此命令輸出有中幾個php-fpm進程就說明啓動成功了):

1 ps aux | grep php-fpm

3、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;
    server_name _;
    #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

4、測試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
# Disabling symbolic-links is recommended to prevent assorted security risks
#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就徹底配置完成了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章