LNMP環境搭建與配置(一)

和LAMP不同的是,LNMP中的N指的是Nginx(類似于Apache的一種web服務軟件),并且php是作為一個獨立服務存在的,這個服務叫做php-fpm,Nginx直接處理靜態請求,動態請求會轉發給php-fpm。

目前LNMP環境的應用非常多。在靜態頁面的處理上,Nginx比Apache更強;但在動態頁面的處理上,Nginx并不占優勢。

12.1 安裝MySQL

LNMP中MySQL的安裝步驟和LAMP一樣。

下載軟件包:

# cd /usr/local/src/# wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz #下載mysql二進制包

初始化:

# tar zxf mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz #解壓二進制包# [ -d /usr/local/mysql ] && mv /usr/local/mysql /usr/local/mysql_old# mv mysql-5.6.36-linux-glibc2.5-x86_64 /usr/local/mysql # useradd -s /sbin/nologin mysql #創建用戶mysql# cd /usr/local/mysql # mkdir -p data/mysql #創建datadir,數據庫文件會放到這里 # chown -R mysql:mysql data/mysql #更改權限,否則后面會出問題# ./scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql/data/mysql #這里datadir盡量使用絕對路徑,不然后面可能報錯FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db:Data::Dumper #有報錯,安裝所缺包# yum list |grep -i dumperperl-Data-Dumper.x86_64 2.145-3.el7 @base perl-XML-Dumper.noarch 0.81-17.el7 base # yum install -y perl-Data-Dumper.x86_64# ./scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql/data/mysqlInstalling MySQL system tables..../bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory #再次報錯,安裝所缺包# yum install -y libaio-devel# ./scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql/data/mysql #有兩個OK,就說明初始化成功# echo $?0 #檢驗上條命令是否執行成功,0表示執行成功

配置MySQL:

# cp support-files/my-default.cnf /etc/my.cnfcp:是否覆蓋"/etc/my.cnf"? y# vim /etc/my.cnf #修改配置文件如下[mysqld]# Remove leading # and set to the amount of RAM for the most important data# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. innodb_buffer_pool_size = 128M# Remove leading # to turn on a very important data integrity option: logging# changes to the binary log between backups.# log_bin = # These are commonly set, remove the # and set as required. basedir = /usr/local/mysql #這里做3處修改,basedir 是MySQL包所在的路徑,datadir 是定義的存放數據的地方,port 定義MySQL服務監聽的端口,如果不定義默認就是3306 datadir = /usr/local/mysql/data/mysql port = 3306# server_id = .....# socket = .....# Remove leading # to set options mainly useful for reporting servers.# cp support-files/mysql.server /etc/init.d/mysqld# chmod 755 /etc/init.d/mysqld# vim /etc/init.d/mysqldbasedir=/usr/local/mysql datadir=/usr/local/mysql/data/mysql #修改成這樣# chkconfig --add mysqld #將mysqld加入系統服務項# chkconfig mysqld on #設置開機啟動

啟動MySQL:

# service mysqld start #啟動mysqld服務Starting MySQL.Logging to '/usr/local/mysql/data/mysql/localhost.localdomain.err'.. SUCCESS! #mysqld服務啟動成功# netstat -lntp |grep 3306 #檢驗mysqld服務是否啟動成功,查看是否在監聽3306端口tcp6 0 0 :::3306 :::* LISTEN 3655/mysqld12.2 安裝PHP

下載源碼包:

# cd /usr/local/src/# wget http://cn2.php.net/distributions/php-5.6.36.tar.gz

解壓源碼包,創建賬號:

# tar zxf php-5.6.36.tar.gz# useradd -s /sbin/nologin php-fpm

配置編譯選項:

# ./configure > --prefix=/usr/local/php-fpm > --with-config-file-path=/usr/local/php-fpm/etc > --enable-fpm > --with-fpm-user=php-fpm > --with-fpm-group=php-fpm > --with-mysql=/usr/local/mysql > --with-mysql-sock=/tmp/mysql.sock > --with-libxml-dir > --with-gd > --with-jpeg-dir > --with-png-dir > --with-freetype-dir > --with-iconv-dir > --with-zlib-dir > --with-mcrypt > --enable-soap > --enable-gd-native-ttf > --enable-ftp > --enable-mbstring > --enable-exif > --disable-ipv6 > --with-pear > --with-curl > --with-openssl #多了--enable-fpm,如果不加該參數,則不會有php-fpm執行文件生成,更不能啟動php-fpm服務

錯誤1:

checking for cc... nochecking for gcc... noconfigure: error: in `/usr/local/src/php-5.6.36':configure: error: no acceptable C compiler found in $PATHSee `config.log' for more details# yum install -y gcc #這里提示安裝gcc,是因為我用了新的虛擬機# ./configure --prefix=/usr/local/php-fpm --with-config-file-path=/usr/local/php-fpm/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --disable-ipv6 --with-pear --with-curl --with-openssl #繼續執行這一步

錯誤2:

checking for xml2-config path... configure: error: xml2-config not found. Please check your libxml2 installation.# yum list |grep libxml2libxml2.x86_64 2.9.1-6.el7_2.3 @anacondalibxml2.i686 2.9.1-6.el7_2.3 base libxml2-devel.i686 2.9.1-6.el7_2.3 base libxml2-devel.x86_64 2.9.1-6.el7_2.3 base libxml2-python.x86_64 2.9.1-6.el7_2.3 base libxml2-static.i686 2.9.1-6.el7_2.3 base libxml2-static.x86_64 2.9.1-6.el7_2.3 base # yum install -y libxml2-devel.x86_64 #安裝libxml-devel# ./configure --prefix=/usr/local/php-fpm --with-config-file-path=/usr/local/php-fpm/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --disable-ipv6 --with-pear --with-curl --with-openssl #繼續執行這一步

錯誤3:

configure: error: Cannot find OpenSSL's <evp.h># yum install -y openssl openssl-devel #安裝openssl和openssl-devel# ./configure --prefix=/usr/local/php-fpm --with-config-file-path=/usr/local/php-fpm/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --disable-ipv6 --with-pear --with-curl --with-openssl #繼續執行這一步

錯誤4:

checking for cURL in default path... not foundconfigure: error: Please reinstall the libcurl distribution - easy.h should be in <curl-dir>/include/curl/# yum install -y libcurl-devel #安裝libcurl-devel# ./configure --prefix=/usr/local/php-fpm --with-config-file-path=/usr/local/php-fpm/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --disable-ipv6 --with-pear --with-curl --with-openssl #繼續執行這一步

錯誤5:

configure: error: jpeglib.h not found.# yum -y install libjpeg-devel #安裝libjpeg-devel# ./configure --prefix=/usr/local/php-fpm --with-config-file-path=/usr/local/php-fpm/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --disable-ipv6 --with-pear --with-curl --with-openssl #繼續執行這一步

錯誤6:

configure: error: png.h not found.# yum install -y libpng libpng-devel #安裝libpng-devel# ./configure --prefix=/usr/local/php-fpm --with-config-file-path=/usr/local/php-fpm/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --disable-ipv6 --with-pear --with-curl --with-openssl #繼續執行這一步

錯誤7:

configure: error: freetype-config not found.# yum install -y freetype freetype-devel #安裝freetype-devel# ./configure --prefix=/usr/local/php-fpm --with-config-file-path=/usr/local/php-fpm/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --disable-ipv6 --with-pear --with-curl --with-openssl #繼續執行這一步

錯誤8:

configure: error: mcrypt.h not found. Please reinstall libmcrypt.# yum install -y epel-release# yum install -y libmcrypt-devel #安裝libmcrypt-devel(安裝之前要安裝epel-release這個擴展源)# ./configure --prefix=/usr/local/php-fpm --with-config-file-path=/usr/local/php-fpm/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --disable-ipv6 --with-pear --with-curl --with-openssl #繼續執行這一步

終于不再提示錯誤,有這樣的信息:

+--------------------------------------------------------------------+| License: || This software is subject to the PHP License, available in this || distribution in the file LICENSE. By continuing this installation || process, you are bound by the terms of this license agreement. || If you do not agree with the terms of this license, you must abort || the installation process at this point. |+--------------------------------------------------------------------+Thank you for using PHP.config.status: creating php5.specconfig.status: creating main/build-defs.hconfig.status: creating scripts/phpizeconfig.status: creating scripts/man1/phpize.1config.status: creating scripts/php-configconfig.status: creating scripts/man1/php-config.1config.status: creating sapi/cli/php.1config.status: creating sapi/fpm/php-fpm.confconfig.status: creating sapi/fpm/init.d.php-fpmconfig.status: creating sapi/fpm/php-fpm.serviceconfig.status: creating sapi/fpm/php-fpm.8config.status: creating sapi/fpm/status.htmlconfig.status: creating sapi/cgi/php-cgi.1config.status: creating ext/phar/phar.1config.status: creating ext/phar/phar.phar.1config.status: creating main/php_config.hconfig.status: executing default commands

這就說明PHP配置編譯參數完成。

編譯php:

# makeBuild complete.Don't forget to run 'make test'.# echo $?0

編譯完成(在這一步,也有可能會遇到問題)。

安裝php:

# make installWrote PEAR system config file at: /usr/local/php-fpm/etc/pear.confYou may want to add: /usr/local/php-fpm/lib/php to your php.ini include_path/usr/local/src/php-5.6.36/build/shtool install -c ext/phar/phar.phar /usr/local/php-fpm/binln -s -f phar.phar /usr/local/php-fpm/bin/pharInstalling PDO headers: /usr/local/php-fpm/include/php/ext/pdo/# echo $?0

安裝完成。

修改配置文件:

# cp php.ini-production /usr/local/php-fpm/etc/php.ini# vim /usr/local/php-fpm/etc/php-fpm.conf #這里是新文件,直接復制粘貼下面的腳本內容即可 [global]pid = /usr/local/php-fpm/var/run/php-fpm.piderror_log = /usr/local/php-fpm/var/log/php-fpm.log[www]listen = /tmp/php-fcgi.socklisten.mode = 666user = php-fpmgroup = php-fpmpm = dynamicpm.max_children = 50pm.start_servers = 20pm.min_spare_servers = 5pm.max_spare_servers = 35pm.max_requests = 500rlimit_files = 1024# /usr/local/php-fpm/sbin/php-fpm -t [01-Jul-2018 21:08:57] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful #顯示 test is successful ,說明配置沒有問題

啟動php-fpm:

# cp /usr/local/src/php-5.6.36/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm# chmod 755 /etc/init.d/php-fpm # useradd -s /sbin/nologin php-fpmuseradd:用戶“php-fpm”已存在 #如果之前進行過這一步,那這里就可以省略# service php-fpm startStarting php-fpm done# ps aux |grep php-fpm #檢測php-fpm是否啟動root 30222 0.0 0.1 123452 4812 ? Ss 21:14 0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)php-fpm 30223 0.0 0.1 123452 4580 ? S 21:14 0:00 php-fpm: pool wwwphp-fpm 30224 0.0 0.1 123452 4580 ? S 21:14 0:00 php-fpm: pool wwwphp-fpm 30225 0.0 0.1 123452 4580 ? S 21:14 0:00 php-fpm: pool wwwphp-fpm 30226 0.0 0.1 123452 4580 ? S 21:14 0:00 php-fpm: pool wwwphp-fpm 30227 0.0 0.1 123452 4584 ? S 21:14 0:00 php-fpm: pool wwwphp-fpm 30228 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool wwwphp-fpm 30229 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool wwwphp-fpm 30230 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool wwwphp-fpm 30231 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool wwwphp-fpm 30232 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool wwwphp-fpm 30233 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool wwwphp-fpm 30234 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool wwwphp-fpm 30235 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool wwwphp-fpm 30236 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool wwwphp-fpm 30237 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool wwwphp-fpm 30238 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool wwwphp-fpm 30239 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool wwwphp-fpm 30240 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool wwwphp-fpm 30241 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool wwwphp-fpm 30242 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool wwwroot 30248 0.0 0.0 112720 980 pts/0 S+ 21:16 0:00 grep --color=auto php-fpm #說明php-fpm成功啟動# chkconfig php-fpm on #設置php-fpm開機啟動12.3 安裝Nginx

下載和解壓Nginx:

# cd /usr/local/src/# wget http://nginx.org/download/nginx-1.12.1.tar.gz# tar zxf nginx-1.12.1.tar.gz

配置編譯選項:

# cd nginx-1.12.1# ./configure --prefix=/usr/local/nginxConfiguration summary + using system PCRE library + OpenSSL library is not used + using system zlib library nginx path prefix: "/usr/local/nginx" nginx binary file: "/usr/local/nginx/sbin/nginx" nginx modules path: "/usr/local/nginx/modules" nginx configuration prefix: "/usr/local/nginx/conf" nginx configuration file: "/usr/local/nginx/conf/nginx.conf" nginx pid file: "/usr/local/nginx/logs/nginx.pid" nginx error log file: "/usr/local/nginx/logs/error.log" nginx http access log file: "/usr/local/nginx/logs/access.log" nginx http client request body temporary files: "client_body_temp" nginx http proxy temporary files: "proxy_temp" nginx http fastcgi temporary files: "fastcgi_temp" nginx http uwsgi temporary files: "uwsgi_temp" nginx http scgi temporary files: "scgi_temp"# echo $?0

編譯和安裝Nginx:

# make# echo $?0# make install# echo $?0

編寫Nginx啟動腳本,并加入系統服務:

# vim /etc/init.d/nginx #寫入下面內容#!/bin/bash# chkconfig: - 30 21# description: http service.# Source Function Library. /etc/init.d/functions# Nginx SettingsNGINX_SBIN="/usr/local/nginx/sbin/nginx"NGINX_CONF="/usr/local/nginx/conf/nginx.conf"NGINX_PID="/usr/local/nginx/logs/nginx.pid"RETVAL=0prog="Nginx"start() { echo -n $"Starting $prog: " mkdir -p /dev/shm/nginx_temp daemon $NGINX_SBIN -c $NGINX_CONF RETVAL=$? echo return $RETVAL}stop() { echo -n $"Stopping $prog: " killproc -p $NGINX_PID $NGINX_SBIN -TERM rm -rf /dev/shm/nginx_temp RETVAL=$? echo return $RETVAL}reload(){ echo -n $"Reloading $prog: " killproc -p $NGINX_PID $NGINX_SBIN -HUP RETVAL=$? echo return $RETVAL}restart(){ stop start}configtest(){ $NGINX_SBIN -c $NGINX_CONF -t return 0}case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) restart ;; configtest) configtest ;; *) echo $"Usage: $0 {start|stop|reload|restart|configtest}" RETVAL=1esacexit $RETVAL# chmod 755 /etc/init.d/nginx #更改啟動腳本權限# chkconfig --add nginx #將Nginx加入系統服務項# chkconfig nginx on #設置Nginx開機啟動

更改Nginx的配置文件:

# > /usr/local/nginx/conf/nginx.conf # > 表示重定向,單獨使用時,可以把一個文本文檔快速清空# vim /usr/local/nginx/conf/nginx.conf #寫入下面內容user nobody nobody;worker_processes 2;error_log /usr/local/nginx/logs/nginx_error.log crit;pid /usr/local/nginx/logs/nginx.pid;worker_rlimit_nofile 51200;events{ use epoll; worker_connections 6000;}http{ include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 3526; server_names_hash_max_size 4096; log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]' ' $host "$request_uri" $status' ' "$http_referer" "$http_user_agent"'; sendfile on; tcp_nopush on; keepalive_timeout 30; client_header_timeout 3m; client_body_timeout 3m; send_timeout 3m; connection_pool_size 256; client_header_buffer_size 1k; large_client_header_buffers 8 4k; request_pool_size 4k; output_buffers 4 32k; postpone_output 1460; client_max_body_size 10m; client_body_buffer_size 256k; client_body_temp_path /usr/local/nginx/client_body_temp; proxy_temp_path /usr/local/nginx/proxy_temp; fastcgi_temp_path /usr/local/nginx/fastcgi_temp; fastcgi_intercept_errors on; tcp_nodelay on; gzip on; gzip_min_length 1k; gzip_buffers 4 8k; gzip_comp_level 5; gzip_http_version 1.1; gzip_types text/plain application/x-javascript text/css text/htm application/xml; server { listen 80; server_name localhost; index index.html index.htm index.php; root /usr/local/nginx/html; location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/tmp/php-fcgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; } }}# /usr/local/nginx/sbin/nginx -t #檢驗配置文件是否有問題nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful #顯示上面兩行說明配置正確

啟動Nginx:

# service nginx start #啟動Nginx服務Starting nginx (via systemctl): [ 確定 ] #如果不能啟動,可以查看/usr/local/nginx/logs/error.log文件# ps aux |grep nginx #檢驗Nginx服務是否啟動root 32821 0.0 0.0 20540 624 ? Ss 21:47 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.confnobody 32822 0.0 0.0 22984 3204 ? S 21:47 0:00 nginx: worker processnobody 32823 0.0 0.0 22984 3204 ? S 21:47 0:00 nginx: worker processroot 32825 0.0 0.0 112720 984 pts/0 S+ 21:48 0:00 grep --color=auto nginx

可以看到,Nginx服務成功啟動。

測試是否正確解析PHP:

# vim /usr/local/nginx/html/1.php<?phpecho "php解析正常";?># curl localhost/1.phpphp解析正常[[email protected] nginx-1.12.1]#

說明PHP解析正常。

免責聲明:本文僅代表文章作者的個人觀點,與本站無關。其原創性、真實性以及文中陳述文字和內容未經本站證實,對本文以及其中全部或者部分內容文字的真實性、完整性和原創性本站不作任何保證或承諾,請讀者僅作參考,并自行核實相關內容。

http://www.uswqb.club/style/images/nopic.gif
分享
評論
首頁
高速公路之王电子游艺
散户炒股 手机网上赚兼职大全 心悦麻将鞍山版下载 红包麻将微信 千炮捕鱼电玩城 贵州捉鸡麻将下载安 黑龙江十一选五前三走势图 15选5浙走势图 2019年四中四精准平码网 福建十一选五彩票app 怎么理财收益最大 310v大赢家比分篮 内蒙古11选5几点结束 夹江二鬼有手机版么 湖北十一选五遗漏 云南11选5稳赚高手