centos搭建Nginx+PHP+MySql+Redis環境詳細步驟(CentOS7環境)
阿新 • • 發佈:2019-01-01
一、安裝Nginx
1、下載nginx-release包
2、匯入GPG signing key
3、安裝1中下載的軟體包。
yum install /home/develop/nginx-release-centos-7-0.el7.ngx.noarch.rpm
其中“/home/develop/”為軟體包的下載路徑。
4、安裝nginx伺服器
yum install nginx
二、安裝PHP
1、安裝gcc及libxml2
yum install gcc -y
yum install libxml2* -y
2、下載最新PHP5.4.16安裝包(阿里雲推薦版本)
3、解壓安裝包
tar zxvf php-5.4.16.tar.gz
4、安裝php
#cd php-5.4.16
#./configure --prefix=/usr/local/php --enable-fpm
#make
#make install
三、關聯Nginx及PHP
1、修改nginx配置檔案
vim /etc/nginx/nginx.conf
[javascript] view plain copy- <span style="font-size: 14px;"><span style="font-family:Microsoft YaHei;">user nginx;
- worker_processes 1;
- error_log /var
- pid /var/run/nginx.pid;
- events {
- worker_connections 1024;
- }
- http {
- include /etc/nginx/mime.types;
- default_type application/octet-stream;
- log_format main '$remote_addr - $remote_user [$time_local] "$request" '
- '$status $body_bytes_sent "$http_referer" '
- '"$http_user_agent" "$http_x_forwarded_for"';
- access_log /var/log/nginx/access.log main;
- sendfile on;
- #tcp_nopush on;
- keepalive_timeout 65;
- 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 256k;
- #gzip on;
- server
- {
- listen 80 default_server;
- #listen [::]:80 default_server ipv6only=on;
- server_name www.yourserver.com;
- index index.html index.htm index.php;
- root /usr/share/nginx/html;
- #error_page 404 /404.html;
- include enable-php.conf;
- }
- include /etc/nginx/conf.d/*.conf;
- }</span></span>
2、生成一個enable-php.conf檔案
[javascript] view plain copy- <span style="font-size: 14px;"><span style="font-family:Microsoft YaHei;"> location ~ [^/]\.php(/|$)
- {
- try_files $uri =404;
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.php;
- include fastcgi.conf;
- }</span></span>
3、生成一個fastcgi.conf檔案
[javascript] view plain copy- <span style="font-size: 14px;"><span style="font-family:Microsoft YaHei;">fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- fastcgi_param QUERY_STRING $query_string;
- fastcgi_param REQUEST_METHOD $request_method;
- fastcgi_param CONTENT_TYPE $content_type;
- fastcgi_param CONTENT_LENGTH $content_length;
- fastcgi_param SCRIPT_NAME $fastcgi_script_name;
- fastcgi_param REQUEST_URI $request_uri;
- fastcgi_param DOCUMENT_URI $document_uri;
- fastcgi_param DOCUMENT_ROOT $document_root;
- fastcgi_param SERVER_PROTOCOL $server_protocol;
- fastcgi_param REQUEST_SCHEME $scheme;
- fastcgi_param HTTPS $https if_not_empty;
- fastcgi_param GATEWAY_INTERFACE CGI/1.1;
- fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
- fastcgi_param REMOTE_ADDR $remote_addr;
- fastcgi_param REMOTE_PORT $remote_port;
- fastcgi_param SERVER_ADDR $server_addr;
- fastcgi_param SERVER_PORT $server_port;
- fastcgi_param SERVER_NAME $server_name;
- # PHP only, required if PHP was built with --enable-force-cgi-redirect
- fastcgi_param REDIRECT_STATUS 200;</span></span>
4、拷貝生產環境的PHP配置檔案
cp /home/develop/php-5.4.16/php.ini-production /usr/local/php/etc/php.ini
5、拷貝php-fpm配置檔案 (如果沒有特殊要求,則直接使用預設配置即可)
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
四、分別啟動nginx和php-fpm服務
1、啟動nginx
/usr/sbin/nginx
2、啟動php
/usr/local/php/sbin/php-fpm -c /usr/local/php/etc/php.ini -y /usr/local/php/etc/php-fpm.conf
五、測試
1、建立phpinfo.php檔案
vim /usr/share/nginx/html/phpinfo.php
輸入以下內容
<?php
phpinfo();
2、在瀏覽器中開啟頁面(http://yourserver/phpinfo.php),觀察是否顯示正常.
六、安裝php-mysql擴充套件
1、安裝autoconf
yum install autoconf -y
2、安裝mysql-devel開發包(不是完整的mysql-server,僅包含mysql二次開發使用的各種庫及標頭檔案)
yum install mysql-devel -y
注:如果是64位的作業系統的話,還需要做個軟連結,否則在配置(configure)的時候會報錯找不到lib檔案,即:
ln -s /usr/lib64/mysql /usr/lib/mysql
3、編譯php-mysql擴充套件庫
cd php-5.4.16/ext/mysql
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-mysql=/usr
make
make install
4、修改php配置檔案
上一步安裝完畢之後,系統會提示生成 .so 檔案的所在路徑。我的是 /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/
1> 開啟php.ini,新增部分內容
vim php.ini
2> 明確指定php擴充套件庫路徑
extension_dir="/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/"
3> 新增mysql擴充套件
extension=mysql.so
5、重啟php-fpm及nginx服務即可
killall php-fpm
killall nginx
/usr/sbin/nginx
/usr/local/php/sbin/php-fpm -c /usr/local/php/etc/php.ini -y /usr/local/php/etc/php-fpm.conf
六-二 同理安裝curl擴充套件
1、安裝curl-devel開發包
yum install curl curl-devel
2、編譯並安裝curl擴充套件
cd php-5.4.16/ext/curl
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-curl=/usr
make
make install
3、php配置檔案中新增curl擴充套件
extension=curl.so
4、同理可按需自行安裝其他擴充套件
六-三 安裝zlib擴充套件
cd php-5.4.16/ext/zlib
mv config0.m4 config.m4
/usr/local/php/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config --with-zlib=/usr make make install
六-四 安裝openssl擴充套件
yum install openssl
cd php-5.4.16/ext/openssl
mv config0.m4 config.m4
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-openssl=/usr --with-libdir=lib64
make
make install
六-五 安裝mcrypt、mhash擴充套件
yum install libmcrypt libmcrypt-devel mcrypt mhash
cd php-5.4.16/ext/mcrypt
/usr/local/php/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config --with-mcrypt=/usr make make install
六-六 安裝mbstring擴充套件
cd php-5.4.16/ext/mbstring
/usr/local/php/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config --with-mbstring=/usr make make install
七、安裝mysql服務
1、安裝libaio
yum install libaio # 安裝依賴包
2、安裝mysql服務
wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
yum localinstall mysql-community-release-el7-5.noarch.rpm
yum install mysql-community-server
3、啟動和關閉 MySQL Server
啟動 MySQL Server
systemctl start mysqld
檢視 MySQL Server 狀態
systemctl status mysqld
關閉 MySQL Server
systemctl stop mysqld
4、防火牆設定 遠端訪問 MySQL, 需開放預設埠號 3306. 執行 firewall-cmd --permanent --zone=public --add-port=3306/tcp firewall-cmd --permanent --zone=public --add-port=3306/udp 這樣就開放了相應的埠。 執行 firewall-cmd --reload 使最新的防火牆設定規則生效。
5、建立root外的其他賬號
建立一個普通使用者 sa ,密碼是 some_pass
CREATE USER 'sa'@'%' IDENTIFIED BY 'some_pass';
給這個使用者授予 SELECT,INSERT,UPDATE,DELETE 的遠端訪問的許可權,這個賬號一般用於提供給實施的系統訪問
GRANT SELECT,INSERT,UPDATE,DELETE ON *.* TO 'sa'@'%';
建立一個管理員使用者 admin 賬號 ,密碼是 some_pass
CREATE USER 'admin'@'%' IDENTIFIED BY 'some_pass';
給這個使用者授予所有的遠端訪問的許可權。這個使用者主要用於管理整個資料庫、備份、還原等操作。
GRANT ALL ON *.* TO 'admin'@'%';
使授權立刻生效
flush privileges;
八、安裝redis服務
1、下載redis原始碼並編譯(截至到2016.8.11,redis最新穩定版本為3.2.3)
tar -zxvf redis-3.2.3.tar.gz
cd redis-3.2.3
make
make install
2、提取並整理程式包
mkdir /usr/redis
cd redis-3.2.3
cp redis.conf /usr/rediscd src
cp redis-server /usr/redis
cp redis-benchmark /usr/redis
cp redis-cli /usr/redis
cd /usr/redis
3、啟動redis
redis-server redis.conf
4、redis安全配置
vim redis.conf
1> 配置外網訪問
bind 127.0.0.1 --------> bind 0.0.0.0
protected-mode yes -------> protected-mode no
2> 變更預設監聽埠
port 6379 -------> port 自定義埠
3> 設定密碼訪問
requirepass foobared ----------> requirepass yourpassword(越長越好)