ÔõÑùÔÚLinuxÉÏ×°ÖúÍÉèÖÃNginxЧÀÍÆ÷
ÔõÑùÔÚlinuxÉÏ×°ÖúÍÉèÖÃnginxЧÀÍÆ÷
NginxÊÇÒ»¿î¸ßÐÔÄܵĿªÔ´WebЧÀÍÆ÷Èí¼þ£¬Ëü±»ÆÕ±éÓ¦ÓÃÓÚ¹¹½¨¸ßÐÔÄܵÄÍøÕ¾ºÍÓ¦ÓóÌÐò¡£±¾ÎĽ«ÏòÄúÏÈÈÝÔõÑùÔÚLinux²Ù×÷ϵͳÉÏ×°ÖúÍÉèÖÃNginxЧÀÍÆ÷¡£
°ì·¨1£º¸üÐÂϵͳ
ÔÚ×°ÖÃÈκÎÈí¼þ֮ǰ£¬Ê×ÏÈҪȷ±£ÄúµÄϵͳÊÇ×îеġ£ÔÚÖÕ¶ËÖÐÔËÐÐÒÔÏÂÏÂÁîÀ´¸üÐÂϵͳµ½×îа汾£º
sudo apt update
sudo apt upgrade
°ì·¨2£º×°ÖÃNginx
ÔÚÖÕ¶ËÖÐÔËÐÐÒÔÏÂÏÂÁîÒÔ×°ÖÃNginx£º
sudo apt install nginx
×°ÖÃÀú³Ì¿ÉÄÜÐèҪһЩʱ¼ä£¬´ý×°ÖÃÍê³Éºó£¬ÔÚä¯ÀÀÆ÷ÖÐÊäÈëÄúµÄЧÀÍÆ÷IPµØµã£¬Äú½«¿´µ½NginxµÄĬÈϽӴýÒ³Ã棬ÌåÏÖNginx×°ÖÃÀֳɡ£
°ì·¨3£ºÉèÖÃNginx
NginxµÄÉèÖÃÎļþλÓÚ/etc/nginxĿ¼Ï¡£Äú¿ÉÒÔʹÓÃÈκÎÎı¾±à¼Æ÷À´±à¼¸ÃÎļþ¡£ÀýÈ磺
sudo nano /etc/nginx/nginx.conf
ÔÚÉèÖÃÎļþÖУ¬¿ÉÒÔ¸ü¸ÄÒÔϼ¸¸ö³£¼ûµÄÑ¡Ï
Server_name£ºÕâÊÇЧÀÍÆ÷µÄÓòÃû»òIPµØµã¡£Ä¬ÈÏÇéÐÎÏ£¬NginxÉèÖÃÎļþÖÐÒѾָ¶¨ÁËÒ»¸öserver_name²ÎÊý£¬Äú¿ÉÒÔ½«ÆäÐÞ¸ÄΪÄú×Ô¼ºµÄÓòÃû»òIPµØµã¡£
ÀýÈ磺
server_name example.com;
Server_tokens£ºÄ¬ÈÏÇéÐÎÏ£¬Nginx»áÔÚHTTPÏìӦͷÖзµ»ØЧÀÍÆ÷µÄÏêϸÐÅÏ¢¡£ÎªÁËÔöǿЧÀÍÆ÷µÄÇå¾²ÐÔ£¬½¨Ò齫ÆäÉèÖÃΪoffÒÔ½ûÓÃЧÀÍÆ÷ÏêϸÐÅÏ¢µÄÏÔʾ¡£
ÀýÈ磺
server_tokens off;
Location£ºNginxµÄlocation¿éÓÃÓÚ½ç˵²î±ðURL·¾¶µÄÉèÖá£Äú¿ÉÒÔƾ֤ÐèÒªÌí¼Ó»òÐ޸IJî±ðµÄlocation¿é¡£ÀýÈ磬ÒÔÏ´úÂ뽫Öض¨Ïò/example·¾¶µ½http://example.com£º
location /example {
return 301 http://example.com;
µÇ¼ºó¸´ÖÆ
}
ÉúÑIJ¢¹Ø±ÕÉèÖÃÎļþºó£¬Ê¹ÓÃÒÔÏÂÏÂÁîÀ´ÖØмÓÔØNginxÉèÖÃÎļþ£º
sudo systemctl reload nginx
°ì·¨4£ºÉèÖÃNginxÐéÄâÖ÷»ú
ÐéÄâÖ÷»úÔÊÐíÄúÔÚͳһ̨ЧÀÍÆ÷ÉÏÍйܶà¸öÍøÕ¾¡£¿ÉÒÔͨ¹ý½¨ÉèеÄÉèÖÃÎļþÀ´ÉèÖÃNginxÐéÄâÖ÷»ú¡£
½¨ÉèеÄÉèÖÃÎļþ£º
ʹÓÃÒÔÏÂÏÂÁîÔÚ/etc/nginx/sites-availableĿ¼Ï½¨ÉèÒ»¸öеÄÉèÖÃÎļþ£¬²¢ÎªÆäÃüÃû£º
sudo nano /etc/nginx/sites-available/example.com.conf
±àдÉèÖÃÎļþ£º
ÔÚеÄÉèÖÃÎļþÖУ¬ÄúÐèÒª½ç˵ÐéÄâÖ÷»úµÄЧÀÍÆ÷Ãû³Æ¡¢¸ùĿ¼ºÍÆäËûÏà¹ØÑ¡Ïî¡£ÒÔÏÂÊÇÒ»¸öʾÀýÉèÖÃÎļþµÄʾÀý£º
server {
listen 80; server_name example.com; root /var/www/example.com; location / { index index.html; }
µÇ¼ºó¸´ÖÆ
}
ÆôÓÃÐéÄâÖ÷»ú£º
½¨ÉèºÃÉèÖÃÎļþºó£¬Ê¹ÓÃÒÔÏÂÏÂÁîÀ´ÆôÓÃËü£º
sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/
È»ºóÖØмÓÔØNginxÉèÖÃÎļþ£º
sudo systemctl reload nginx
°ì·¨5£ºÉèÖÃHTTPS
ΪÁ˱£»¤ÍøÕ¾µÄÊý¾ÝÇå¾²£¬½¨ÒéΪÍøÕ¾ÆôÓÃHTTPS¡£ÒÔÏÂÊÇһЩ¹ØÓÚÔõÑùÉèÖÃHTTPSµÄʾÀý´úÂ룺
ÌìÉúSSLÖ¤Ê飺
ʹÓÃÒÔÏÂÏÂÁîÌìÉú×ÔÊðÃûµÄSSLÖ¤Ê飺
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/cert.key -out /etc/nginx/cert.crt
ÉèÖÃÐéÄâÖ÷»ú£º
±à¼ÄãµÄÐéÄâÖ÷»úÉèÖÃÎļþ£¬ÔÚserver¿éÖÐÌí¼ÓÒÔÏ´úÂ룺
listen 443 ssl;
ssl_certificate /etc/nginx/cert.crt;
ssl_certificate_key /etc/nginx/cert.key;
ÉúÑIJ¢¹Ø±ÕÉèÖÃÎļþ£¬ÖØмÓÔØNginxÉèÖÃÎļþ¡£
°ì·¨6£ºÆô¶¯Nginx
¸ü¸ÄÍêÉèÖúó£¬Ê¹ÓÃÒÔÏÂÏÂÁîÀ´Æô¶¯NginxЧÀÍ£º
sudo systemctl start nginx
Äú¿ÉÒÔʹÓÃÒÔÏÂÏÂÁî¼ì²éNginxÊÇ·ñÕýÔÚÔËÐУº
systemctl status nginx
ÏÖÔÚ£¬ÄúÒѾÀֳɵØÔÚLinuxÉÏ×°ÖúÍÉèÖÃÁËNginxЧÀÍÆ÷¡£Äú¿ÉÒÔƾ֤ÐèÒª¾ÙÐиü¶àµÄ¶¨ÖƺÍÓÅ»¯¡£×£ÄúʹÓÃNginx¹¹½¨¸ßÐÔÄܵÄÍøÕ¾ºÍÓ¦ÓóÌÐò£¡
ÒÔÉϾÍÊÇÔõÑùÔÚLinuxÉÏ×°ÖúÍÉèÖÃNginxЧÀÍÆ÷µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡