目錄
- 準備作業
- 安裝wget
- 安裝net-tools
- 安裝vim
- 配置顯示行號
- 關閉防火墻
- 安裝Nginx
- 安裝依賴
- 編譯安裝Nginx
- 配置環境變數
- Systemd管理
- 安裝MySQL
- 安裝依賴
- 下載boost
- 編譯安裝MySQL
- 配置環境變數
- 修改組態檔
- Systemd管理
- 登錄MySQL
- 安裝PHP
- 安裝依賴
- 編譯安裝PHP
- 配置Systemd服務
- 關聯Nginx和PHP
- 安裝Redis
- 編譯安裝
- 配置環境變數
- 配置后臺運行
- 配置Systemd服務
- 參考資料
作為一名PHP開發者,我們一定要懂得如何搭建PHP開發環境,目前主流的PHP開發環境組合是LAMP和LNMP,本文將介紹如何在CentOS7.*上搭建LNMP開發環境,
各項版本說明:
CentOS7: 7.7
Nginx: 1.16.1
MySQL:5.7.28
PHP:7.2.25
安裝所需所有的資源我都放在了/usr/local/src目錄下
準備作業
安裝wget
wget 是一個從網路上自動下載檔案的自由工具,支持通過 HTTP、HTTPS、FTP 三個最常見的TCP/IP協議下載,并可以可以使用HTTP代理,
sudo yum -y install wget
安裝net-tools
最小化安裝CentOS7時如果無法使用ifconfig命令,則需要安裝net-tools,如果是安裝的CentOS6版本則無需安裝
sudo yum -y install net-tools
安裝vim
sudo yum -y install vim
配置顯示行號
vim ~/.vimrc # 編輯.vimrc組態檔
set nu # 輸入set nu 后退出保存
關閉防火墻
systemctl stop firewalld.service #令關閉防火墻
systemctl disable firewalld.service #關閉防火墻開機自啟動
通過瀏覽器輸入IP測驗是否成功
安裝Nginx
安裝依賴
(1) 安裝 nginx 需要先將官網下載的原始碼進行編譯,編譯依賴 gcc 環境,如果沒有 gcc 環境,則需要安裝gcc-c++,
yum -y install gcc gcc-c++
(2) PCRE是一個Perl庫,中文"Perl兼容的正則運算式庫",安裝Nginx是為了使Nginx支持具備URI重寫功能的rewrite模塊,如果不安裝pcre庫,則Nginx無法使用rewrite模塊功能,Nginx的Rewrite模塊功能幾乎是企業應用必須,
yum -y install pcre pcre-devel
(3) zlib 庫提供了很多種壓縮和解壓縮的方式, nginx 使用 zlib 對 http 包的內容進行 gzip ,所以需要在 Centos 上安裝 zlib 庫,
yum -y install zlib zlib-devel
(4) OpenSSL是一個強大的安全套接字層密碼庫,囊括主要的密碼演算法、常用的密鑰和證書封裝管理功能及 SSL 協議,并提供豐富的應用程式供測驗或其它目的使用, nginx 不僅支持 http 協議,還支持 https(即在ssl協議上傳輸http),所以需要安裝 OpenSSL 庫 ,
yum -y install openssl openssl-devel
說明: yum安裝方式安裝的pcre版本比較低,不過基本不影響使用,但是最好還是手動編譯安裝官網最新穩定版的openssl,
檢查基礎依賴包
上面的依賴安裝完成后可以通過如下命令檢查各個依賴安裝是否成功
rpm -qa pcre pcre-devel
rpm -qa zlib zlib-devel
rpm -qa pcre pcre-devel
編譯安裝Nginx
# 這里我們把安裝包都放到了/usr/local/src目錄下,便于統一管理
cd /usr/local/src #切換到軟體包目錄
wget http://nginx.org/download/nginx-1.16.1.tar.gz #下載nginx原始碼包
useradd nginx -s /sbin/nologin -M #創建nginx用戶用于管理nginx程式
tar -zxvf nginx-1.16.1.tar.gz #解壓nginx原始碼包
cd nginx-1.16.1
#預編譯
./configure \
--user=nginx \
--group=nginx \
--prefix=/usr/local/nginx-1.16.1 \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_stub_status_module
make && make install #編譯 和 安裝
cd /usr/local
ln -s nginx-1.16.1 nginx #創建nginx的軟鏈接
安裝說明
--prefix=PATH #設定安裝路勁
--user=USER #行程用戶權限
--group=GROUP #行程用戶組權限
--with-http_v2_module # HTTP2
--with-http_stub_status_module #激活狀態資訊
--with-http_ssl_module #激活ssl功能
配置環境變數
vim /etc/profile
export PATH=/usr/local/nginx/sbin:$PATH
source /etc/profile
Systemd管理
新建并編輯/usr/lib/systemd/system/nginx.service 檔案
vim /usr/lib/systemd/system/nginx.service
并添加如下內容(這里的配置是根據自己安裝Nginx的路徑來配置的,Nginx安裝在了/usr/local目錄下)
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
通過yum安裝的nginx,默認的nginx.service配置如下,可以作為參考
# /usr/lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
[Install]
WantedBy=multi-user.target
多載daemon
執行下面的命令重新載入 systemd,掃描新的或有變動的單元即可
systemctl daemon-reload
設定開機自啟
systemctl enable nginx.service # 設定開機自啟
systemctl disable nginx.service # 取消開機自啟服務
Nginx服務管理常用命令
systemctl status nginx.service # 查看Nginx狀態
systemctl start nginx.service # 開啟Nginx
systemctl stop nginx.service # 關閉Nginx
systemctl reload nginx.service # 多載配置
systemctl restart nginx.service # 重啟Nginx(相當于stop&start)
服務啟動檢查
可以通過該命令查詢80埠被誰占用
lsof -i :80
如果無法識別該命令,需要安裝lsof
sudo yum -y install lsof
安裝MySQL
安裝依賴
(1)cmake是新版MySQL的編譯工具,必須安裝
sudo yum -y install gcc gcc-c++ cmake ncurses-devel perl perl-devel autoconf bison bison-devel libtirpc libtirpc-devel
下載boost
如果安裝的MySQL5.7及以上的版本,在編譯安裝之前需要安裝boost,因為高版本mysql需要boots庫的安裝才可以正常運行,否則會報CMake Error at cmake/boost.cmake:81 錯誤
切換到/usr/local/src目錄,然后在這個目錄下下載boost
MySQL5.7.27要求boost的版本是1.59,更高版本的不適用MySQL5.7.28
wget http://www.sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
百度網盤鏈接: boost_1_59_0.tar.gz
編譯安裝MySQL
# 添加MySQL用戶
useradd -s /sbin/nologin -M mysql
# 切換到/usr/src目錄
cd /usr/local/src
# 下載MySQL
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.28.tar.gz
# 解壓MySQL
tar -zxvf mysql-5.7.28.tar.gz
#解壓boost,并移至mysql/boost
tar -zxvf boost_1_59_0.tar.gz
mv boost_1_59_0 mysql-5.7.28/boost
# 進到MySQL目錄
cd mysql-5.7.28
# 預編譯
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.7.28 \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1 \
-DWITH_SSL=system \
-DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \
-DMYSQL_DATADIR=/var/lib/mysql/data \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_INNODB_MEMCACHED=1 \
-DWITH_DEBUG=OFF \
-DWITH_ZLIB=bundled \
-DENABLED_LOCAL_INFILE=1 \
-DENABLED_PROFILING=ON \
-DMYSQL_MAINTAINER_MODE=OFF \
-DMYSQL_TCP_PORT=3306
# 編譯&安裝
make && make install
# 創建軟鏈接
cd /usr/local
ln -s mysql-5.7.28 mysql
配置環境變數
# 添加到環境變數
vim /etc/profile
export PATH=/usr/local/mysql/bin:$PATH
source /etc/profile
修改組態檔
-
在
/var/lib目錄下創建一個mysql檔案夾mkdir -p /var/lib/{mysql,mysql/data} touch /var/lib/mysql/mysqld.pid chown mysql.mysql -R /var/lib/mysql/ -
修改
/etc/my.cnf檔案# 修改/etc/my.cnf檔案,編輯組態檔如下 [mysqld] character-set-server=utf8mb4 collation-server=utf8mb4_general_ci datadir=/var/lib/mysql/data socket=/var/lib/mysql/mysql.sock [mysqld_safe] log-error=/var/log/mysql/mysqld.log pid-file=/var/lib/mysql/mysqld.pid [client] default-character-set=utf8mb4 -
創建
mysqld.log和mysqld.pid檔案,并修改檔案權限# 創建mysqld.log 和 mysqld.pid檔案 mkdir /var/log/mysql touch /var/log/mysql/mysqld.log chown mysql.mysql -R /var/log/mysql/ -
初始化資料庫
# 初始化資料庫, –initialize 表示默認生成一個安全的密碼,–initialize-insecure 表示不生成密碼 mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/var/lib/mysql/data
Systemd管理
創建一個/usr/lib/systemd/system/mysqld.service檔案,然后編輯內容如下
vim /usr/lib/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
Type=forking
PIDFile=/var/lib/mysql/mysqld.pid
# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0
# Execute pre and post scripts as root
PermissionsStartOnly=true
# Needed to create system tables
ExecStartPre=/usr/local/mysql/bin/mysqld_pre_systemd
# Start main service
ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/var/lib/mysql/mysqld.pid $MYSQLD_OPTS
# Use this to switch malloc implementation
EnvironmentFile=/etc/my.cnf
# Sets open_files_limit
LimitNOFILE = 5000
Restart=on-failure
RestartPreventExitStatus=1
PrivateTmp=true
多載daemon
執行下面的命令重新載入 systemd,掃描新的或有變動的單元即可
systemctl daemon-reload
啟動MySQL
systemctl start mysqld.service # 啟動MySQL
systemctl stop mysqld.service # 關閉MySQL
systemctl status mysqld.service # 查看MySQL狀態
開機自啟
systemctl enable mysqld.service # 設定開機自啟
systemctl disable mysqld.service # 取消開機自啟
登錄MySQL
mysql -u root -p #第一次登陸不需要密碼,回車即可
set password for root@localhost = password('root'); #修改密碼
安裝PHP
安裝依賴
sudo yum -y install gcc gcc-c++ zip unzip libxml2 libxml2-devel curl-devel autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel gd-devel bzip2 bzip2-devel libzip libzip-devel libwebp libwebp-devel
編譯安裝PHP
cd /usr/local/src
tar -zxvf php-7.2.25.tar.gz
cd php-7.2.25
./configure \
--prefix=/usr/local/php-7.2.25 \
--enable-fpm \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--enable-mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-mysql-sock=/var/lib/mysql/mysql.sock \
--with-gd \
--with-webp-dir \
--with-png-dir \
--with-gettext \
--with-jpeg-dir \
--with-freetype-dir \
--with-iconv-dir \
--with-zlib-dir \
--with-bz2 \
--with-openssl \
--with-curl \
--enable-mbstring \
--enable-static \
--enable-zip \
--enable-bcmath \
--enable-ftp \
--enable-pcntl \
--enable-soap \
--enable-calendar \
--enable-sockets \
--enable-exif \
--enable-xml
make && make install
編譯引數詳解
./configure \
--prefix=/usr/local/php-7.2.25 \ # 指定安裝路徑
--enable-fpm \ # 表示激活PHP-FPM方式服務,即FactCGI方式運行PHP服務,
--with-fpm-user=nginx \ # 指定PHP-FPM行程管理的用戶為nginx,此處最好和Nginx服務用戶統一,
--with-fpm-group=nginx \ # 指定PHP-FPM行程管理用戶組為nginx,此處最好和Nginx服務用戶組統一,
--enable-mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-mysql-sock=/var/lib/mysql/mysql.sock \
--with-gd \ # 打開gd庫的支持
--with-webp-dir \
--with-png-dir \
--with-gettext \ # 實作了NLS (Native Language Support) API,他可以用來國際化您的PHP程式
--with-jpeg-dir \
--with-freetype-dir \
--with-iconv-dir \ # 包含了 iconv 字符集轉換功能的介面,
--with-zlib-dir \ # 打開zlib庫的支持,用于http壓縮傳輸
--with-bz2 \ # 用于透明地讀寫 bzip2(.bz2)壓縮檔案,
--with-openssl \ # 打開openssl,加密傳輸時用到
--with-curl \ # 打開curl瀏覽工具的支持
--enable-mbstring \ # 多位元組,字串的支持
--enable-static \ # 生成靜態鏈接庫
--enable-zip \ # 打開對zip的支持
--enable-bcmath \
--enable-ftp \ # 通過檔案傳輸協議 (FTP) 提供對檔案服務器的客戶端訪問
--enable-pcntl \ # 多行程
--enable-soap \
--enable-calendar \
--enable-sockets \ # 打開 sockets 支持
--enable-exif \ # 可交換影像資訊
--enable-xml
配置
cd /usr/local
ln -s php-7.2.25 php
cp /usr/local/src/php-7.2.25/php.ini-development /usr/local/php-7.2.25/lib/php.ini
vim /usr/local/php/lib/php.ini
date.timezone = PRC (大約在934行)
expose_php = Off #避免PHP資訊暴露在http頭中(大約369行)
display_errors = Off(生產環境設定為off,開發環境就設定為On,便于除錯)
說明:設定了dispaly_errors為off后,需要在php-fpm.conf中開啟錯誤日志記錄路徑error_log = log/php-fpm.log
cd /usr/local/php
cp etc/php-fpm.conf.default etc/php-fpm.conf
cd /usr/local/php/etc/php-fpm.d/
cp www.conf.default www.conf
cd /usr/local/php
sbin/php-fpm
ps -e | grep php-fpm
如果在編譯PHP時指定了--with-mysql=mysqlnd和--with-pdo-mysql=mysqlnd的引數,那么在生產中可能會遇到socket連接問題,解決辦法是在php.ini里加入命令: pdo_mysql.default_socket=/var/lib/mysql/mysql.sock
最好是在編譯PHP的時候,指定mysql.socket的位置:
--with-mysql-sock=/var/lib/mysql/mysql.sock
管理PHP-FPM
vim /usr/local/php/etc/php-fpm.conf
pid = run/php-fpm.pid
error_log = log/php-fpm.log #24行這個在php.ini設定display_errors = Off時啟用
設定完之后重啟服務器
向行程發送信號,就可以完成行程管理
停止: kill -INT `cat /usr/local/php/var/run/php-fpm.pid`
平滑停止: kill -QUIT `cat /usr/local/php/var/run/php-fpm.pid`
重啟:kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`
重新打開日志:kill -USR1 `cat /usr/local/php/var/run/php-fpm.pid`
配置環境變數
vim /etc/profile
export PATH=/usr/local/php/bin:$PATH
source /etc/profile
配置Systemd服務
其實php-fpm.service檔案php已經幫我們配置好了,只需要我們復制到指定位置,并啟用就行了,
cp /usr/local/src/php-7.2.25/sapi/fpm/php-fpm.service /usr/lib/systemd/system/
編輯/usr/lib/systemd/system/php-fpm.service檔案并修改為如下內容:
# It's not recommended to modify this file in-place, because it
# will be overwritten during upgrades. If you want to customize,
# the best way is to use the "systemctl edit" command.
[Unit]
Description=The PHP FastCGI Process Manager
After=network.target
[Service]
Type=simple
PIDFile=/usr/local/php/var/run/php-fpm.pid
ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
多載daemon
執行下面的命令重新載入 systemd,掃描新的或有變動的單元即可
systemctl daemon-reload
開機自啟
systemctl enable php-fpm.service
啟動php-fpm
systemctl start php-fpm.service
關聯Nginx和PHP
nginx.conf配置
#user nobody;
# 有一個作業的子行程,可以自行修改,但太大無益,因為要爭奪CPU
# 一般設定CPU數 * 核數
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
#一般是配置Nginx行程與連接的特性
#若幾個同時作業
multi_accept on; #打開同時接受多個新網路連接請求的功能,
use epoll; #使用epoll事件驅動,因為epoll的性能相比其他事件驅動要好很多
worker_connections 10240; #這是指一個子行程最大允許連接10240個連接
}
http { # 這是配置http服務器的主要段
include mime.types;
default_type application/octet-stream;
#隱藏Nginx軟體版本號
server_tokens off;
#激活tcp_nodelay功能,提高I/O性能
tcp_nodelay on;
# 設定讀取客戶端請求頭資料的超時時間,此處的數值為15,其單位是秒,為經驗參考值
client_header_timeout 15;
# 設定讀取客戶端請求體的超時時間
client_body_timeout 15;
# 指定回應客戶端的超時時間
send_timeout 25;
# 上傳檔案大小限制
client_max_body_size 8m;
#壓縮配置
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain text/css text/xml application/javascript;
gzip_vary on;
#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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#include extra/*.conf;
server {
listen 80;
server_name www.blog.com;
root html;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
index index.php index.html index.htm;
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php/$1 last;
}
}
#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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
}
安裝Redis
編譯安裝
# 解壓原始碼檔案
tar -zxvf redis-5.0.7.tar.gz
# 切換到解壓目錄
cd redis-5.0.7
# 編譯安裝
make PREFIX=/usr/local/redis-5.0.7 install
mkdir /usr/local/redis-5.0.7/etc
cp redis.conf /usr/local/redis-5.0.7/etc/
# 創建軟鏈接
cd /usr/local
ln -s redis-5.0.7 redis
配置環境變數
vim /etc/profile
export PATH=/usr/local/redis/bin:$PATH
source /etc/profile # 使修改立即生效
配置后臺運行
讓redis以后臺行程的形式運行
vim /usr/local/redis/etc/redis.conf
# daeonize no(大約136行)
# 改為 ->
daemonize yes
配置Systemd服務
在 /usr/lib/systemd/system/添加一個redis.service檔案,并添加如下內容
[Unit]
Description=Redis
After=network.target
[Service]
Type=forking
PIDFile=/var/run/redis_6379.pid
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
ExecStop=/usr/local/redis/bin/redis-cli shutdown
PrivateTmp=true
[Install]
WantedBy=multi-user.target
多載daemon
執行下面的命令重新載入 systemd,掃描新的或有變動的單元即可
systemctl daemon-reload
開機自啟
systemctl enable redis.service
啟動redis服務
systemctl start redis.service
參考資料
centos7 原始碼編譯安裝 mysql5.7
mysql在linux7下systemd的相關配置
Managing MySQL Server with systemd
centos7 7.3php編譯安裝
centos7下編譯安裝php7.3
cmake安裝配置及入門指南
編譯CMake 3.15 和 gcc 5.3.0
CentOS7升級OpenSSL到1.1.1
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/95106.html
標籤:PHP
下一篇:冒泡排序
