1、安裝nginx
下載鏈接 http://nginx.org/en/download.html
(1)下載,解壓
wget http://nginx.org/download/nginx-1.15.8.tar.gz tar zxf nginx-1.15.8.tar.gz cd nginx-1.15.8
(2)在編譯安裝之前先安裝需要的依賴庫和編譯軟件安裝
yum install gcc gcc-c++ glibc -y yum install pcre-devel -y yum install zlib-devel -y yum install openssl-devel -y
(3)nginx編譯參數解析
–prefix #nginx安裝目錄,默認在/usr/local/nginx –pid-path #pid問件位置,默認在logs目錄 –lock-path #lock問件位置,默認在logs目錄 –with-http_ssl_module #開啟HTTP SSL模塊,以支持HTTPS請求。 –with-http_dav_module #開啟WebDAV擴展動作模塊,可為文件和目錄指定權限 –with-http_flv_module #支持對FLV文件的拖動播放 –with-http_realip_module #支持顯示真實來源IP地址 –with-http_gzip_static_module #預壓縮文件傳前檢查,防止文件被重復壓縮 –with-http_stub_status_module #取得一些nginx的運行狀態 –with-mail #允許POP3/IMAP4/SMTP代理模塊 –with-mail_ssl_module #允許POP3/IMAP/SMTP可以使用SSL/TLS –with-pcre=../pcre-8.11 #注意是未安裝的pcre路徑 –with-zlib=../zlib-1.2.5 #注意是未安裝的zlib路徑 –with-debug #允許調試日志 –http-client-body-temp-path #客戶端請求臨時文件路徑 –http-proxy-temp-path #設置http proxy臨時文件路徑 –http-fastcgi-temp-path #設置http fastcgi臨時文件路徑 –http-uwsgi-temp-path=/var/tmp/nginx/uwsgi #設置uwsgi 臨時文件路徑 –http-scgi-temp-path=/var/tmp/nginx/scgi #設置scgi 臨時文件路徑
(4)編譯安裝
在linux 上通過yum安裝nginx默認使用nobody用戶和用戶組。 http://www.runoob.com/linux/linux-user-manage.html
# grep '#user' nginx.conf.default #user bobody;
為了防止黑客猜到這個Web服務的用戶,我們需要將其更改成特殊的用戶名;有兩種方式
(1)在編譯的時候可以手動指定用戶和組。
(2)將默認的"#user nobody;" 改為如下內容
並在nginx編譯安裝完成之后,修改nginx.conf user nginx nginx; //用戶 用戶組
手動創建nginx屬主和nginx屬組
1、增加一個新的用戶組使用groupadd命令。其格式如下: groupadd 選項 用戶組 可以使用的選項有: -g GID 指定新用戶組的組標識號(GID)。 -o 一般與-g選項同時使用,表示新用戶組的GID可以與系統已有用戶組的GID相同。 例如: # groupadd group1 說明:此命令向系統中增加了一個新組group1,新組的組標識號是在當前已有的最大組標識號的基礎上加1。 2、添加新的用戶賬號使用useradd命令,其語法如下: useradd 選項 用戶名 選項參數說明: -c comment 指定一段注釋性描述。 -d 目錄 指定用戶主目錄,如果此目錄不存在,則同時使用-m選項,可以創建主目錄。 -g 用戶組 指定用戶所屬的用戶組。 -G 用戶組,用戶組 指定用戶所屬的附加組。 -s Shell文件 指定用戶的登錄Shell。 -u 用戶號 指定用戶的用戶號,如果同時有-o選項,則可以重復使用其他用戶的標識號。 用戶名: 指定新賬號的登錄名。 例如: # useradd –d /usr/sam -m sam 說明:此命令創建了一個用戶sam,其中-d和-m選項用來為登錄名sam產生一個主目錄/usr/sam(/usr為默認的用戶主目錄所在的父目錄)。 //添加nginx用戶組 groupadd nginx //添加nginx用戶 useradd nginx -g nginx -s /sbin/nologin -M 說明: -s表示指定用戶所用的shell,此處為/sbin/nologin,表示不登錄。 -M表示不創建用戶主目錄。 -g表示指定用戶的組名為nginx。
《執行編譯》
./configure --prefix=/opt/local/nginx\ --sbin-path=/opt/local/nginx/sbin/nginx\ --conf-path=/opt/local/nginx/conf/nginx.conf\ --error-log-path=/var/log/nginx/error.log\ --http-log-path=/var/log/nginx/access.log\ --pid-path=/var/run/nginx/nginx.pid\ --lock-path=/var/lock/nginx.lock\ --user=nginx\ --group=nginx\ --with-http_ssl_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
或 執行時格式這樣子
./configure --prefix=/opt/local/nginx --sbin-path=/opt/local/nginx/sbin/nginx --conf-path=/opt/local/nginx/conf/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_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
執行完成的樣子
最后執行如下
make && make install
查看如下
(5)修改查看配置
修改啟動軟連
ln -s /opt/local/nginx/sbin/nginx /usr/bin/
啟動nginx
/opt/local/nginx/sbin/nginx
or
nginx -t //已經軟連接過了
nginx
如圖所示,報錯沒有找到nginx文件夾,沒關系,創建一個即可。再重新啟動查看端口號
(6)查看網絡狀態 http://www.runoob.com/linux/linux-comm-netstat.html
netstat -lntup
到這里nginx的安裝就非常完美的告一段落了。
[root@localhost www]# nginx -s reload nginx: [alert] kill(17615, 1) failed (3: No such process)
當nginx 重啟的時候遇到這個問題,意思沒有這樣的進程,懷疑沒有啟動nginx
(1)執行命令,定位nginx: whereis nginx
(2)查找配置文件 find / -name nginx.conf
(3)指定配置文件地址/usr/bin/nginx -c /opt/local/nginx/conf/nginx.conf
最后在重啟即可:nginx -s reload
[root@localhost conf]# nginx -s reload nginx: [error] open() "/var/run/nginx/nginx.pid" failed (2: No such file or directory)
2、安裝PHP
wget http://cn2.php.net/distributions/php-7.2.8.tar.gz
tar -xzf php-7.2.8.tar.gz
(1)先安裝編譯依賴的組件
yum -y install gcc gcc-c++ gcc-g77 make libtool autoconf patch unzip automake libxml2 libxml2-devel ncurses ncurses-devel libtool-ltdl-devel libtool-ltdl libmcrypt libmcrypt-devel libpng libpng-devel libjpeg-devel openssl openssl-devel gd-devel curl curl-devel libxml2 libxml2-devel ncurses ncurses-devel libtool-ltdl-devel libtool-ltdl autoconf automake libaio*
說明:
nginx本身不能處理PHP,它只是個web服務器,當接收到請求后,如果是php請求,則發給php解釋器處理,並把結果返回給客戶端。
nginx一般是把請求發fastcgi管理進程處理,fascgi管理進程選擇cgi子進程處理結果並返回給nginx
(2)什么是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的,具體選項含義可以查看這里。
(3) 編譯安裝 (修改對應的配置)
./configure --prefix=/opt/local/php7 --with-curl --with-mysql-sock=/var/tmp/mysql/mysql.sock --with-jpeg-dir --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libxml-dir --with-mysqli=mysqlnd --with-openssl --with-pcre-regex --with-pdo-mysql=mysqlnd --with-pdo-sqlite --with-pear --with-png-dir --with-xmlrpc --with-xsl --with-zlib --with-pdo-mysql --with-fpm-user=nginx --with-fpm-group=nginx --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-gd-native-ttf --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-xml --enable-zip --enable-mysqlnd --enable-maintainer-zts
如果編譯遇到一個問題:-bash: --with-freetype-dir 沒有那個文件或目錄
先安裝 freetype
yum install freetype
編譯時遇到的問題,安裝即可,最后在執行上面的編譯安裝
yum install libcurl-devel
yum -y install libxslt libxslt-devel
最后的樣子
然后執行如下命令
make && make install
以上php就安裝好了,接下來進行配置
1).在執行configure的地方執行如下命令
cp php.ini-production /opt/local/php7/etc/php.ini cd /opt/local/php7/etc/ //查看是否有了 php.ini
2).在接着執行如下命令
cp php-fpm.conf.default php-fpm.conf
3).在進入這個目錄 cd php-fpm.d/ 在繼續執行如下命令
cp www.conf.default www.conf
4).最后在這個目錄 /opt/local/php7/sbin 執行如下命令
./php-fpm
5).查看一下端口號是否啟動了 netstat -lntup
由圖片可見,php-fpm 已經成功啟動了。
設置全局php訪問
ln -s /opt/local/php7/bin/php /usr/bin/php
添加永久環境變量,影響所有用戶
vim /etc/profile 在文檔最后,添加如下命令;說明(sbin 為php-fpm 所在,bin 為php客戶端所在)
export PATH = "/opt/local/php7/sbin:/opt/local/php7/bin:$PATH" 保持退出 然后運行 source /etc/profile
具體資源可看這里,環境變量設置
其他說明:
因為沒有安裝mysql,所以查看/opt/local/php7/lib/php/extensions/no-debug-zts-20170718/下只有opcache.a opcache.so 兩個擴展,
而如果安裝了mysql則需要確保存在mysqli.so、pdo_mysql.so這兩個動態庫文件,否則無法與mysql通信成功;
解決重啟php-fpm時遇到的一些問題;
[root@localhost /]# service php-fpm start Redirecting to /bin/systemctl start php-fpm.service Failed to start php-fpm.service: Unit not found.
上面的問題是說 php-fpm.service: Unit not found
解決思路:因為php-fpm是編譯安裝的,所以,那個腳本文件不存在。在service的時候會自動訪問 init.d/目錄下的php-fpm腳本,沒有這個腳本就會報那個錯誤。
所以這個時候需要將編譯目錄下的這個腳本/root/php-7.2.8/sapi/fpm/init.d.php-fpm cp 到 init.d/的目錄,
cp /root/php-7.2.8/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm 這樣就可以執行了,並修改這個文件的可執行權限
解決方案
1)find / -name 'init.d.php-fpm'
/root/php-7.2.8/sapi/fpm/init.d.php-fpm
2)cp /root/php-7.2.8/sapi/fpm/init.d.php-fpm /etc/init.d/php-f
3)chmod a+x /etc/init.d/php-fpm //修改為可執行權限
4)service php-fpm start
又遇到問題了:
Starting php-fpm [26-Jan-2019 05:01:49] ERROR: unable to bind listening socket for address '127.0.0.1:9000': Address already in use (98)
[26-Jan-2019 05:01:49] ERROR: FPM initialization failed
遇到這個問題也是頭大,先看一下進程
netstat -lntup | grep 9000
killall php-fpm
5)開啟php: service php-fpm start|restart|stop
6) 查看:ps aux | grep php
7) 查看版本: php -v
以上就完成了php-fpm的安裝流程和啟動流程
6)修改nginx.conf 支持可執行php
略
7)通過瀏覽器訪問虛擬機的ip
1、修改mac的hosts,
2、關閉虛擬機的防火牆,訪問即可,參考 https://blog.csdn.net/sshuidajiao/article/details/82594504
3、安裝php擴展
curl.so || mysqli.so
1).切換到php源碼目錄下
root@localhost ext]# pwd /root/php-7.2.8/ext [root@localhost ext]# ls bcmath curl exif ftp iconv ldap oci8 pcre pdo_oci phar reflection snmp sqlite3 tidy xmlrpc zlib bz2 date ext_skel gd imap libxml odbc pdo pdo_odbc posix session soap standard tokenizer xmlwriter calendar dba ext_skel_win32.php gettext interbase mbstring opcache pdo_dblib pdo_pgsql pspell shmop sockets sysvmsg wddx xsl com_dotnet dom fileinfo gmp intl mysqli openssl pdo_firebird pdo_sqlite readline simplexml sodium sysvsem xml zend_test ctype enchant filter hash json mysqlnd pcntl pdo_mysql pgsql recode skeleton spl sysvshm xmlreader zip [root@localhost ext]#
2).調用Configure生成Makefile文件
/opt/local/php7/bin/phpize
./configure --with-php-config=/opt/local/php7/bin/php-config
3).編譯、安裝
make && make install
4).配置php.ini 開啟curl.so
extension=curl.so 去掉 ";"
5).重啟服務
service php-fpm restart
6).查看 php -m 即可
4、安裝mysql5.7
1)下載mysql安裝包並解壓且編譯安裝
cd 到root目錄
說明:由於MySQL5.7必須使用boost.1.59及以上版本,需要安裝boost
[root@localhost ~]# wget https://jaist.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz [root@localhost ~]# tar -zxvf boost_1_59_0.tar.gz [root@localhost ~]# cd boost_1_59_0 [root@localhost boost_1_59_0]# ./bootstrap.sh [root@localhost boost_1_59_0]# ./b2 [root@localhost boost_1_59_0]# ./b2 install [root@localhost boost_1_59_0]# cd ..
編譯安裝mysql
[root@localhost ~]# cd mysql-5.7.20 [root@localhost mysql-5.7.20]# cmake -DCMAKE_INSTALL_PREFIX=/opt/local/mysql -DMYSQL_DATADIR=/opt/local/mysql/mydata -DSYSCONFDIR=/opt/local/mysql/conf -DMYSQL_USER=mysql -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DMYSQL_UNIX_ADDR=/opt/local/mysql/mysql.sock -DMYSQL_TCP_PORT=3306 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_DEBUG=0 -DMYSQL_MAINTAINER_MODE=0 -DWITH_SSL:STRING=bundled -DWITH_ZLIB:STRING=bundled -DDOWNLOAD_BOOST=1 -DWITH_BOOST=./boost [root@localhost mysql-5.7.20]# make && make install ##耗時比較長 注: -DCMAKE_INSTALL_PREFIX=/opt/local/mysql 設置安裝目錄 -DMYSQL_DATADIR=/opt/local/mysql/mydata 設置數據庫存放目錄 -DMYSQL_UNIX_ADDR=/opt/local/mysql/mysql.sock 設置UNIX socket 目錄 -DMYSQL_USER=mysql 設置運行用戶 -DDEFAULT_CHARSET=utf8 設置默認字符集,默認latin1 -DEFAULT_COLLATION=utf8_general_ci 設置默認校對規則,默認latin1_general_ci -DWITH_INNOBASE_STORAGE_ENGINE=1 添加InnoDB引擎支持 -DENABLE_DOWNLOADS=1 自動下載可選文件,比如自動下載谷歌的測試包 -DMYSQL_TCP_PORT=3306 設置服務器監聽端口,默認3306 -DSYSCONFDIR=/opt/local/mysql/conf 設置my.cnf所在目錄,默認為安裝目錄 更多參數執行 # cmake . -LH 或者查看官方說明
2) 為了安全,需要創建mysql用戶組 和mysql用戶來運行mysql
[root@localhost ~]# groupadd mysql [root@localhost ~]# useradd -r -g mysql -s /bin/false mysql //不用登錄
3)創建用戶授權
[root@localhost ~]# useradd -M -s /sbin/nologin mysql [root@localhost ~]# mkdir -p /opt/local/mysql/mydata [root@localhost ~]# mkdir -p /opt/local/mysql/conf [root@localhost ~]# chown -R mysql:mysql /opt/local/mysql
4)將 mysql 的 bin 目錄加入環境變量
[root@localhost mysql-5.7.20]# echo "export PATH=$PATH:/opt/local/mysql/bin" >> /etc/profile [root@localhost mysql-5.7.20]# source /etc/profile
5)修改 /opt/local/mysql/目錄的所屬組和所屬用戶
[root@localhost mysql-5.7.20]# chown -R root:mysql /opt/local/mysql/
6) 初始化mysql數據庫 會生成一個root用戶的密碼,需要記住,以后也可以修改
[root@localhost mysql-5.7.20]# mysqld --initialize --user=mysql --basedir=/opt/local/mysql --datadir=/opt/local/mysql/mydata
//上面那句我沒有執行,因為它提示我 /opt/local/mysql/data 文件夾不存在,我執行的是下面這句
[root@localhost mysql-5.7.20]# mysqld --initialize --user=mysql
說明:報了一個錯誤,如下
[root@localhost mysql-5.7.20]# mysqld --initialize --user=mysql --basedir=/opt/local/mysql --datadir=/opt/local/mysql/mydata 2019-02-28T12:21:34.967498Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting. 2019-02-28T12:21:34.967640Z 0 [ERROR] Aborting
意思是 data 這個目錄必須是空,初始化的時候會自動創建data 目錄,可以直接刪掉它 ,我的目錄是 /opt/local/mysql/data
我是怎樣錯誤操作了一波:
但是如果繼續執行 mysqld --initialize --user=mysql --basedir=/opt/local/mysql --datadir=/opt/local/mysql/mydata 這句話會提示我 data 文件夾不存在,所以我自己創建了一個文件夾,並且附上了mysql:mysql的權限,執行完沒有問題,但是這樣自己創建的文件夾並沒有用,會報很多錯誤。
所以還是要直接刪掉 data 文件夾,讓初始化自己創建目錄 ,執行這句即可 mysqld --initialize --user=mysql
7) 由於MySQL5.7 需要自己創建 my.cnf 文件夾, mysql 默認會啟動 /etc/my.cnf 這里的文件。但是呢,我可以換一個位置, 自己創建 my.cnf
[root@localhost conf]# pwd /opt/local/mysql/conf [root@localhost conf]# ll 總用量 4 -rw-r--r--. 1 root root 259 2月 28 06:57 my.cnf [root@localhost conf]#
編輯內容如下
[mysqld] basedir=/opt/local/mysql datadir=/opt/local/mysql/data pid-file=/opt/local/mysql/data/mysqld.pid socket=/opt/local/mysql/mysql.sock log_error=/opt/local/mysql/data/mysql.err user=mysql explicit_defaults_for_timestamp=true skip-grant-tables
8) 復制啟動腳本
[root@localhost conf]# cp /opt/local/mysql/support-files/mysql.server /etc/init.d/mysql
增加權限:chmod +x /etc/init.d/mysql
9) 啟動mysql 時報錯了
[root@localhost data]# service mysql start Starting MySQL.. ERROR! The server quit without updating PID file (/opt/local/mysql/data/mysqld.pid). [root@localhost data]#
查看日志/opt/local/mysql/data/mysql.err
2019-02-28T12:42:35.944689Z 0 [ERROR] Can't start server: Bind on TCP/IP port: Address already in use 2019-02-28T12:42:35.944693Z 0 [ERROR] Do you already have another mysqld server running on port: 3306 ?
端口號被使用了
[root@localhost data]# netstat -anp | grep 3306 tcp6 0 0 :::3306 :::* LISTEN 97682/mysqld [root@localhost data]#
殺死進程,重新啟動
[root@localhost data]# kill -s 9 97682 [root@localhost data]# /opt/local/mysql/bin/mysqld_safe: line 198: 97682 Killed nohup /opt/local/mysql/bin/mysqld --basedir=/opt/local/mysql --datadir=/opt/local/mysql/data --plugin-dir=/opt/local/mysql/lib/plugin --user=mysql --log-error=/opt/local/mysql/data/mysql.err --pid-file=/opt/local/mysql/data/mysqld.pid --socket=/opt/local/mysql/mysql.sock < /dev/null > /dev/null 2>&1 [root@localhost data]# [root@localhost data]# service mysql start Starting MySQL.. SUCCESS! [root@localhost data]#
成功進入mysql
[root@localhost data]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.20 Source distribution 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>
到此lnmp安裝結束咯~