前言:
作為PHP開發者,我們常用的線上環境就是LNMP,合理的搭建也是必須掌握的技能,下面就利用源碼的方式詳細介紹下LNMP環境Nginx+PHP+Mysql的詳細搭建步驟:
版本說明:
Nginx:nginx-1.14.1(最新版本)
PHP:php-7.2.12(最新版本)
Mysql:mysql-5.7.24
一、編譯環境的安裝:GCC,make,C++編譯器
yum -y install gcc automake autoconf libtool make
yum -y install gcc gcc-c++
二、類庫的安裝:PCRE庫、Zlib庫、openssl
1.選定源碼下載目錄
可以是任何目錄,建議目錄為/usr/local/src
cd /usr/local/src
2.安裝PCRE庫,官方地址:https://ftp.pcre.org/pub/pcre/
cd /usr/local/src wget https://ftp.pcre.org/pub/pcre/pcre-8.42.tar.gz tar -zxvf pcre-8.42.tar.gz cd pcre-8.42 ./configure make && make install
3.安裝Zlib庫,官方地址:http://www.zlib.net/
cd /usr/local/src wget http://www.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.安裝openssl官方地址:https://www.openssl.org/source/
cd /usr/local/src wget https://www.openssl.org/source/openssl-1.0.2t.tar.gz tar -zxvf openssl-1.0.2t.tar.gz cd openssl-1.0.2t ./config make && make install
備注:prefix 是安裝目錄,openssldir是配置文件目錄
三、Nginx的安裝
官方地址:http://nginx.org/en/download.html
1.下載Nginx官方穩定包並解壓:
cd /usr/local/src wget http://nginx.org/download/nginx-1.14.1.tar.gz tar -zxvf nginx-1.14.1.tar.gz cd nginx-1.14.1
2.添加nginx用戶和用戶組
groupadd -r nginx
useradd -r -g nginx nginx
3.配置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.42 \ --with-zlib=/usr/local/src/zlib-1.2.11 \ --with-openssl=/usr/local/src/openssl-1.0.2t
注意:當前所在目錄為/usr/local/src/nginx-1.14.1
--with-pcre=/usr/local/src/pcre-8.42 指的是pcre-8.42 的源碼路徑。
--with-zlib=/usr/local/src/zlib-1.2.11 指的是zlib-1.2.11 的源碼路徑。
--with-openssl=/usr/local/src/openssl-1.0.2t指的是openssl-1.0.2t的源碼路徑。
4.編譯安裝
make && make install
5.啟動
/usr/local/nginx/sbin/nginx
注意:若mkdir /var/tmp/nginx/client failed的提示,請手動創建:
mkdir -p /var/tmp/nginx/client
打開瀏覽器訪問此服務器的 IP,如果瀏覽器出現 Welcome to nginx! 則表示 Nginx 已經安裝並運行成功!
6.查看nginx版本
nginx -v
輸入以上登陸Nginx命令可能出現的問題:-bash: nginx: command not found
解決:添加軟連接即可
ln -s /usr/local/nginx/sbin/nginx /usr/bin/
四、PHP安裝
官方地址:http://www.php.net/downloads.php
1.添加
yum -y install gcc gcc-c++ glibc
2.添加
yum install -y libxml2-devel openssl-devel libcurl-devel libjpeg-devel libpng-devel libicu-devel openldap-devel
3.下載PHP官方穩定包並解壓:
cd /usr/local/src wget https://www.php.net/distributions/php-7.1.32.tar.gz tar -zvxf php-7.1.32.tar.gz cd php-7.1.32
4.添加nginx用戶和用戶組
groupadd -r www
useradd -r -g www www
5.配置PHP安裝參數
./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-libdir=lib64 \
--enable-fpm \
--enable-mysqlnd \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--enable-opcache \
--enable-pcntl \
--enable-mbstring \
--enable-soap \
--enable-zip \
--enable-calendar \
--enable-bcmath \
--enable-exif \
--enable-ftp \
--enable-intl \
--with-openssl \
--with-zlib \
--with-curl \
--with-gd \
--with-gettext \
--with-mhash \
--with-ldap
注意:不同PHP版本,配置參數有部分不同,僅供參考
6.編譯安裝
make && make install
7.為php-fpm提供配置文件
cp php.ini-production /usr/local/php/etc/php.ini
cd /usr/local/php/etc/
cp php-fpm.conf.default php-fpm.conf
cd /usr/local/php/etc/php-fpm.d/
cp www.conf.default www.conf
8.把PHP加入環境變量
執行命令 #vi /etc/profile在文件末尾加上如下兩行代碼
PATH=$PATH:/usr/local/php/bin export PATH
然后執行命令 #source /etc/profile ,執行完可通過 #echo $PATH命令查看是否添加成功
9.查看php版本
php -v
10.啟動
/usr/local/php/sbin/php-fpm
驗證是否啟動成功(如果此命令輸出有幾個php-fpm進程就說明啟動成功了)
ps -aux | grep php-fpm
五、Nginx服務器解析PHP文件之Nginx.conf簡易配置
1.編輯Nginx.conf
vim /usr/local/nginx/nginx.conf
#user nobody; 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 { worker_connections 1024; } http { include 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 logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name 127.0.0.1; #charset koi8-r; #access_log logs/host.access.log main; location / { root /var/local/www; index index.php index.html index.htm; } #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 /var/local/www; } # 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$ { root /var/local/www; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; #fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 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; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
注意:
- 紅色標記部分為修改部分;
- 原文件目錄存放默認位置為:/usr/local/nginx/html,現將文件存放目錄修改為:/var/local/www;
2.重啟Nginx服務器
/usr/loca/nginx/sbin/nginx -s reload
3.測試php文件
創建index.php文件
vi /var/local/www/index.php
4.輸入內容,保存
<?php phpinfo(); ?>
5.瀏覽器訪問http://服務器ip/index.php,看到如下信息即表示成功。
六、Mysql安裝
官方地址:https://dev.mysql.com/downloads/mysql/
1.下載源碼包,並解壓
wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz tar -zxvf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz
2.添加Mysql用戶和用戶組
groupadd mysql useradd -r -g mysql mysql
3.復制
cp -r mysql-5.7.28-linux-glibc2.12-x86_64 /usr/local/mysql
4.修改Mysql目錄所屬者
cd /usr/local/mysql chown -R mysql:mysql ./
5.初始化,安裝Mysql數據庫,並生成臨時密碼(注意:版本不同可能格式不同)
bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
如圖:
注意點:
a.若輸入以上命令可能出現的問題:bin/mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory
原因:缺少numactl軟件包
解決:安裝numactl軟件包
yum -y install numactl
再次執行初始化命令,即可成功;
b.若輸入以上命令可能出現的問題:bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
原因:缺少libaio軟件包
解決:安裝libaio軟件包
yum -y install libaio
再次執行初始化命令,即可成功;
6.配置my.cnf(簡易配置)
vi /etc/my.cnf
[mysqld] basedir=/usr/local/mysql datadir=/usr/local/mysql/data socket=/tmp/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Settings user and group are ignored when systemd is used. # If you need to run mysqld under a different user or group, # customize your systemd unit file for mariadb according to the # instructions in http://fedoraproject.org/wiki/Systemd character_set_server=utf8 init_connect='SET NAMES utf8 lower_case_table_names = 1 [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/usr/local/mysql/data/mysqld.pid # # include all files from the config directory # !includedir /etc/my.cnf.d
注意:紅色為修改部分;
7.添加開機啟動
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
8.修改 vim /etc/init.d/mysqld
vim /etc/init.d/mysqld
在46行 修改代碼:
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
9.啟動mysql服務
service mysqld start
10.通過以下腳本可實現對mysql服務的管理
/etc/init.d/mysqld start /etc/init.d/mysqld stop /etc/init.d/mysqld restart /etc/init.d/mysqld reload /etc/init.d/mysqld status
chkconfig --add /etc/init.d/mysqld
12.設置終端模式開機啟動:
chkconfig mysqld on
13.執行完上述命令后,就可以使用service對nginx服務的管理
service mysqld start service mydqld stop service mysqld restart service mysqld reload service mysqld status
14.根據生成的原始密碼,登陸Mysql
mysql -u root -p
輸入以上登陸Mysql命令可能出現的問題:-bash: mysql: command not found
解決:添加軟連接
ln -s /usr/local/mysql/bin/mysql /usr/bin
15.修改原密碼為root,並刷新權限(注意:要在登陸Mysql情況下修改)
alter user 'root'@'localhost' identified by 'root';
flush privileges; //刷新權限
至此全部成功安裝了😊
七、設置nginx和php-fpm的開機自啟動
具體實現步驟,請點擊查看:
八、常見問題解決辦法
1.CentOS下如何完全卸載php文件
注意:當我們使用命令:yum remove php,是無法完全卸載php文件的,輸入查看php版本命令:php -v,還是可以看到php版本。
正確做法如下:(強制刪除全部php軟件包)
a.查看全部php軟件包
rpm -qa|grep php
php-cli-5.3.3-22.el6.x86_64
php-pdo-5.3.3-22.el6.x86_64
php-gd-5.3.3-22.el6.x86_64
php-fpm-5.3.3-22.el6.x86_64
php-common-5.3.3-22.el6.x86_64
php-5.3.3-22.el6.x86_64
php-xml-5.3.3-22.el6.x86_64
php-pear-1.9.4-4.el6.noarch
b.以此卸載軟件包(注意:卸載要先卸載沒有依賴的)
rpm -e php-fpm-5.3.3-22.el6.x86_64 rpm-e php-pdo-5.3.3-22.el6.x86_64 rpm -e php-pear-1.9.4-4.el6.noarch rpm-e php-cli-5.3.3-22.el6.x86_64 rpm -e php-5.3.3-22.el6.x86_64 rpm-e php-xml-5.3.3-22.el6.x86_64 rpm -e php-gd-5.3.3-22.el6.x86_64 rpm-e php-common-5.3.3-22.el6.x86_64
再次輸入查看php版本命令:php -v,版本信息已經沒有提示,卸載成功。
2.CentOS 7.x更改Hostname
a.查看當前的Hostname
hostnamectl status
b.設置新的Hostname
hostnamectl set-hostname XXX //XXX為新設置的Hostname
c.重啟,運行a命令查看即修改成功